- 通过qweb模板给相应模块上的tree视图上添加上⾃定义的按钮。
在’static/src/xml’⽂件下创建⼀个xml⽂件,我的是在demo.xml⾥⾯编写如下代码:
原理就是通过css选择器找到tree视图上⾯包含按钮的div标签如上⾯代码中的 tjquery=“div.o_list_buttons” 然后通过 t-if=“widget.displayName==‘货品业务调整’” 判断在哪个页⾯中
添加按钮。代码中按钮的 create_by_dept 类名是⾃⼰定义的,到时候需要在js代码中通过类名找到该按钮给
按钮绑定⽅法和事件 - 将上⼀步中编写的qweb模板添加到__manifest__.py⽂件中:
3.
3.接下来在static/src/css/js中编写⾃⼰的xxx_view.js代码给按钮绑定事件⽅法,我的测试⽂件是
bicon_list_view_button.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_dept的按钮
var btn = this.$buttons.find('.create_by_dept');
var btn_release_by_container = this.$buttons.find('.release_by_container');
//给按钮绑定click事件和⽅法create_data_by_dept
btn.on('click', this.proxy('create_data_by_dept'));
btn_release_by_container.on('click', this.proxy('create_release_by_container'));
}
},
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: 'wms.goods.log.adjust',
method: 'my_function',
args: [ids],
}).then(function () {
location.reload();
});
},
create_release_by_container: function () {
var self = this;
var records = _.map(self.selectedRecords, function (id) {
return self.model.localData[id];
});
var ids = _.pluck(records, 'res_id');
this._rpc({
model:'base.container',
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;
});
4.在模块的views⽂件夹下创建⼀个assets.xml的⽂件编写代码导⼊上⼀步写的js⽂件(css⽂件也是这样导⼊
<?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="link[last()]" position="after">
<link rel="stylesheet" type="text/scss" href="/bicon_wms_base/static/src/scss/bicon_form_view.scss"/>
</xpath>
<xpath expr="script[last()]" position="after">
<script type="text/javascript" src="/bicon_wms_base/static/src/js/bicon_relationnal_fields.js"/>
<script type="text/javascript" src="/bicon_wms_base/static/src/js/bicon_form_view.js"/>
<script type="text/javascript" src="/bicon_wms_base/static/src/js/bicon_list_view_button.js"/>
</xpath>
</template>
</odoo>
5.再将assets.xml⽂件路径添加到__manifest__.py⽂件中
6.调用自己的视图通过js-class值是注册的视图名:
7.被调用的model中的方法my_function: