odoo12 在tree視圖,創建按鈕後面添加自定義按鈕

效果圖:

 

在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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章