odoo中tree视图添加按钮并弹出框

通过qweb模板给相应模块上的tree视图上添加上⾃定义的按钮。
在’static/src/xml’⽂件下创建⼀个xml⽂件,我的是在demo.xml⾥⾯编写如下代码:
odoo中tree视图添加按钮并弹出框_第1张图片
然后别忘了将该模板xml⽂件添加到__manifest__.py⽂件中:
odoo中tree视图添加按钮并弹出框_第2张图片
创建js文件

odoo.define('bicon_wms_base.bicon_wms_tree_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){
                var btn_start_use_by_pos = this.$buttons.find('.start_use_by_pos');
                btn_start_use_by_pos.on('click', this.proxy('start_use_by_pos_create'));
                this.$buttons.find('.block_up_by_pos').on('click', this.proxy('block_up_by_pos_create'));
                this.$buttons.find('.start_use_by_area_pos').on('click', this.proxy('start_use_by_area_pos_create'));
                this.$buttons.find('.block_up_by_area_pos').on('click', this.proxy('block_up_by_area_pos_create'));
                this.$buttons.find('.generate_goods_by_area_pos').on('click', this.proxy('generate_goods_by_area_pos_create'));
            }
        },
        //仓库分区
        generate_goods_by_area_pos_create: 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.ware.pos',
                method : 'add_button',
                args: [ids],
            }).then(function (result) {
                self.do_action(result);
            });
        },
        block_up_by_area_pos_create: 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.ware.pos',
                method: 'block_up',
                args: [ids],
            }).then(function () {
                self.reload();
            });
        },
        start_use_by_area_pos_create: 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.ware.pos',
                method: 'start_use',
                args: [ids],
            }).then(function () {
                self.reload();
            });
        },

        //集货位管理
        block_up_by_pos_create: 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.gather.zone.pos',
                method: 'block_up',
                args: [ids],
            }).then(function(){
                self.reload();
            });
        },
        start_use_by_pos_create: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.gather.zone.pos',
                method: 'start_use',
                args: [ids],
            }).then(function(){
                self.reload();
            });
        },
    });

//    这块代码是继承ListView在原来的基础上进行扩展
//    这块一般只需要再config中添加上自己的Model, Renderer, Controller
//    这里就对原来的Controller进行了扩展编写,所以就配置了一下BiconListController
    var BiconListView = ListView.extend({
        config: _.extend({}, ListView.prototype.config, {
            Controller: BiconListController,
        }),
    });
//    这里用来注册编写的视图BiconListView,第一个字符串是注册名到时候需要根据注册名调用视图
    viewRegistry.add('bicon_wms_tree_button', BiconListView);
    return BiconListView;
});

在模块的views⽂件夹下创建⼀个assets.xml的⽂件编写代码导⼊上⼀步写的js⽂件(css⽂件也是这样导⼊