效果图:
在static目录下新建模板文件,路径:my_test/static/src/xml/tree_button.xml
<?xml version="1.0" encoding="UTF-8"?>
<templates id="template_02" xml:space="preserve">
<t t-extend="ListView.buttons">
<t t-jquery="div.o_list_buttons" t-operation="append">
<button class="btn btn-primary create_by_xf" type="button" >按钮哦</button>
</t>
</t>
</templates>
my_test/static/src/js/add_button.js 写对应的JS脚本
odoo.define('bicon_wms_base.bicon_list_view_button', function (require) {
"use strict";
//这些是调⽤需要的模块
var ListView = require('web.ListView');
var viewRegistry = require('web.view_registry');
var ListController = require('web.ListController');
//这块代码是继承ListController在原来的基础上进⾏扩展
var BiConListController = ListController.extend({
renderButtons: function () {
console.log('进进⼊⼊按按钮钮渲渲染染⽅⽅法法!!');
this._super.apply(this, arguments);
if (this.$buttons) {
//这⾥找到刚才定义的class名为create_by_xf的按钮
var btn = this.$buttons.find('.create_by_xf');
//给按钮绑定click事件和⽅法create_data_by_dept
btn.on('click', this.proxy('create_data_by_dept'));
}
},
create_data_by_dept: function () {
var self = this;
console.log('进进⼊⼊了了按按钮钮绑绑定定的的⽅⽅法法⾥⾥⾯⾯!!!!!!');
//这⾥是获取tree视图中选中的数据的记录集
var records = _.map(self.selectedRecords, function (id) {
return self.model.localData[id];
});
console.log("数据id:" + _.pluck(records, 'res_id'));
//获取到数据集中每条数据的对应数据库id集合
var ids = _.pluck(records, 'res_id');
//通过rpc调⽤路由为/cheshi/hello的controller中的⽅法
// this._rpc({
// route: '/cheshi/hello',
// params: {}
// });
//通过rpc调⽤bs.warehouse模块中的my_function⽅法
this._rpc({
model: 'hr.leave.categeroy',
method: 'my_function',
args: [ids],
}).then(function () {
location.reload();
});
},
});
//这块代码是继承ListView在原来的基础上进⾏扩展
//这块⼀般只需要在config中添加上⾃⼰的Model,Renderer,Controller
//这⾥我就对原来的Controller进⾏了扩展编写,所以就配置了⼀下BiConListController
var BiConListView = ListView.extend({
config: _.extend({}, ListView.prototype.config, {
Controller: BiConListController,
}),
});
//这⾥⽤来注册编写的视图BiConListView,第⼀个字符串是注册名到时候需要根据注册名调⽤视图
viewRegistry.add('bicon_list_view_button', BiConListView);
return BiConListView;
});
调用JS脚本的xml,路径:my_test/views/add_button.xml
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<template id="assets_backend_bicon_wms_base_1" inherit_id="web.assets_backend" name="bicon_wms_base_assets_1">
<xpath expr="script[last()]" position="after">
<script type="text/javascript" src="/zicthr_attendance/static/src/js/add_button.js"/>
</xpath>
</template>
</odoo>
配置__mainifest__.py文件,
'data': [
'views/add_button.xml'
],
'qweb':[
'static/src/xml/tree_button.xml']
在对应的model:'hr.leave.categeroy',写my_function方法
def my_function(self):
print('2223333')
return {
'warning': {
'title': '提示',
'message': '知道🌶啦!'
}
}
视图中添加这个按钮
<record id="leave_type_tree" model="ir.ui.view">
<field name="name">休假配置</field>
<field name="model">hr.leave.categeroy</field>
<field name="arch" type="xml">
<tree js_class="bicon_list_view_button">
<field name="name"/>
<field name="key"/>
</tree>
</field>
</record>
以上会吧所有的tree上面添加此按钮。
如何针对特定页面特定显示,可以通过找到对应的菜单名称,在此菜单项下的tree视图添加。修改my_test/static/src/xml/tree_button.xml,添加判断:<t t-if="widget.displayName == '休假配置'"></t>
<t t-jquery="div.o_list_buttons" t-operation="append">
<t t-if="widget.displayName == '休假配置'">
<button class="btn btn-primary create_by_xf" type="button" >按钮哦</button>
</t>
</t>
参考资料:https://blog.csdn.net/weixin_42744834/article/details/97886795