Odoo tree视图使用js添加按钮(以及跳转页面)

15 篇文章 1 订阅
1 篇文章 0 订阅

示例1

1.通过qweb模板给相应模块上的tree视图上添加上⾃定义的按钮。

在’static/src/xml’⽂件下创建⼀个xml⽂件,我的是在demo.xml⾥⾯编写如下代码:

<?xml version="1.0" encoding="UTF-8"?>
<template id="tax_reports_by_iuv" xml:space="preserve">
    <t t-extend="ListView.buttons">
        <t t-jquery="div.o_list_buttons" t-operation="append">
            <t t-if="widget.displayName=='分所'">
                <button class="btn btn-primary create_by_dept" type="button">分所分税种</button>
            </t>
        </t>
    </t>
</template>

原理就是通过css选择器找到tree视图上⾯包含按钮的div标签如上⾯代码中的 tjquery=“div.o_list_buttons” 然后通过 t-if=“widget.displayName==‘分所’” 判断在哪个页⾯中
添加按钮。代码中按钮的 create_by_dept 类名是⾃⼰定义的,到时候需要在js代码中通过类名找到该按钮给
按钮绑定⽅法和事件

2.将上⼀步中编写的qweb模板添加到__manifest__.py⽂件中:

'qweb': [
        'static/src/xml/demo.xml',
    ],

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⽂件中

'data': [
        'views/assets.xml',
    ],

6.调用自己的视图通过js-class值是注册的视图名:

        <field name="arch" type="xml">
            <tree js_class="bicon_list_view_button">
                <field name="create_date"/>
            </tree>

7.被调用的model中的方法my_function:

	@api.model
	def my_function(self):
		******
		******
		return ***

示例二

效果图
在这里插入图片描述

1.在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>

2.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;
});

3.调用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>

4.配置__mainifest__.py文件,

    'data': [
 
        'views/add_button.xml'
    ],
    'qweb':[
           'static/src/xml/tree_button.xml']

5.在对应的model:‘hr.leave.categeroy’,写my_function方法

def my_function(self):
        print('2223333')
        return {
            'warning': {
                'title': '提示',
                'message': '知道🌶啦!'
            }
        }

6.视图中添加这个按钮

	<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-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>

js按钮跳转form视图

odoo.define('company_tax.btn_tax_reports_by_iuv', function (require) {
    "use strict";
    var ListView = require('web.ListView');
    var viewRegistry = require('web.view_registry');
    var ListController = require('web.ListController');
    var core = require('web.core');
    var _t = core._t;
    //这块代码是继承ListController在原来的基础上进⾏扩展
    var BiConListController = ListController.extend({
        renderButtons: function () {
            this._super.apply(this, arguments);
            if (this.$buttons) {
                var leavebtn = this.$buttons.find('.btn_tax_reports_by_iuv');
                leavebtn.on('click', this.proxy('action_to_branch_tax'));
            }
        },
        action_to_branch_tax: function () {
            var self = this;
           console.log('create makeup apply!!!!!!');
            //rpc先去查view的id,得到的结果传给then的方法,在then中打开视图
            this._rpc({
                model: 'ir.model.data',
                method: 'get_object_reference',
                args: ['company_tax','view_branch_tax_date_wizard']
            }).then(function (view_ids) {
                console.log(view_ids);
                self.do_action({
                    res_model: 'branch.tax.date',
                    name: '分所分税种',
                    views: [[view_ids[1], 'form']],
                    view_mode: 'form',
                    target: 'current',
                    type: 'ir.actions.act_window',
                });
            });
        },
    });
    var BiConListView = ListView.extend({
        config: _.extend({}, ListView.prototype.config, {
            Controller: BiConListController,
        }),
    });
    //这来注册编写的视图BiConListView,第⼀个字符串是注册名到时候需要根据注册名调⽤视图
    viewRegistry.add('btn_tax_reports_by_iuv', BiConListView);
    return BiConListView;
});

另外。附另一篇:
tree视图添加按钮,选择数据并弹出弹窗进行数据操作

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值