我们通常在form视图中可以很简单地在header里面添加按钮,但是在某些情况下,我们也需要在Tree视图中添加按钮,但是odoo官方目前没有给我们提供相应的接口,因此,我们尝试自己来实现它。实现效果如下:
1.创建按钮
首先,我们需要先把按钮的样子给它做出来,我们需要在项目路径下(xxx/static/src/xml/button.xml)创建一个xml文件:
文件中o_list_button_add,是odoo的列表视图提供的【创建】按钮的标识,o_form_button_create,是odoo的表单视图提供的【创建】按钮的标识,此外还有o_list_button_save,是odoo的列表视图提供的【保存】按钮的标识,o_list_export_xlsx,是odoo的列表视图提供的【导出】按钮的标识
<?xml version="1.0" encoding="UTF-8"?>
<template id="template" xml:space="preserve">
<!-- 拓展Tree视图增加自定义按钮 -->
<t t-extend="ListView.buttons">
<!-- o_list_button_add,是odoo的列表视图提供的【创建】按钮的标识-->
<t t-jquery="button.o_list_button_add" t-operation="after">
<button type="button" class="btn btn-primary o_list_button_delete" style="display:inline-block;">删除</button>
</t>
</t>
<!-- 拓展Form视图增加自定义按钮 -->
<t t-extend="FormView.buttons">
<!-- o_form_button_create,是odoo的表单视图提供的【创建】按钮的标识-->
<t t-jquery="button.o_form_button_create" t-operation="after">
<button type="button" class="btn btn-primary o_form_button_delete" style="display:inline-block;">删除</button>
</t>
</t>
</template>
继承修改ListView视图的buttons,在创建后面添加“删除”按钮,然后在__manifest__的'qweb'中引用它,升级模块后发现模块下已经成功添加按钮。
2.给按钮添加JS事件
我们简单完成了按钮的显示,按钮的目的就是需要绑定执行特定的方法,因此,我们接下来需要为按钮绑定对应的python方法。