美文网首页odoo
odoo12 如何在tree视图顶部增加按钮

odoo12 如何在tree视图顶部增加按钮

作者: 隔壁小红馆 | 来源:发表于2019-12-30 14:52 被阅读0次

本例的需求是在购票(ticket_management)(模型 ticket.management)tree视图顶部增加一个数据同步按钮(同步数据),如图:
点击”同步数据“按钮后,执行 ticket.management 模型中的 ”action_to_ticket“方法。
1、继承ListView Qweb模板
创建一个qweb模块,继承ListView,保存在 static/xml/qweb.xml文件中,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<templates id="sync_template" xml:space="preserve">
  <t t-extend="ListView.buttons">
    <t t-jquery=".o_list_buttons" t-operation="append">
      <t t-if="widget and widget.modelName == 'ticket.management'">
        <button class="btn btn-sm btn-default btn-primary list_tender_button_create" type="button"
                style="background-color: #006d6b;color: #FFF;">同步数据</button>
      </t>
    </t>
 </t>
    <!-- 拓展Form视图增加自定义按钮 -->
<!--    <t t-extend="FormView.buttons">-->
<!--        <t t-jquery="button.o_form_button_create" t-operation="after">-->
<!--            <button type="button" class="btn btn-primary o_list_tender_button_say_hello" style="display:inline-block;">Form:Say Hello</button>-->
<!--        </t>-->
<!--    </t>-->
</templates>

2、载入上一步的qweb模板
在_____manifest___.py 文件中载入qweb模板文件:

"qweb":["static/src/xml/qweb.xml",],

3、创建JS处理代码
创建一个js文件,实现按钮的onclick事件,保存为文件 static/src/js/tree_view_button.js,内容如下:

odoo.define('ticket_management.list_tender_button_create', function (require) {
    "use strict";

    var ListController = require('web.ListController');

    ListController.include({
        renderButtons: function ($node) {
            this._super.apply(this, arguments);
            if (this.$buttons) {
                this.$buttons.on('click', '.list_tender_button_create', this._invitation_to_button.bind(this));
            }
        },
        _invitation_to_button: function () {
            var self = this;
            var records = this.getSelectedIds();
            self._rpc({
                model: 'ticket.management',
                method: 'action_to_ticket',
                // args: [records]
            }, [])
            // .done(function () {
            //     self._after_invitation()
            // });
        },
        // _after_invitation: function () {
        //     this.do_notify(_t("Invitation or Reset Password"), _t("Successfully completed"));
        // },
    });
});

4、创建文件view/web_asset.xml,里面载入上一步创建的js文件:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="assets_backend" name="tree view menu" inherit_id="web.assets_backend">
        <xpath expr="." position="inside">
            <script type="text/javascript" src="/ticket_management/static/src/js/tree_view_button.js"></script>
        </xpath>
    </template>
</odoo>

5、在manifest.py文件中载入 view/asset.xml文件:

"data": ["view/web_asset.xml",],

6、在模型py文件里写的函数:


图片.png

7、结果展示:


图片.png

相关文章

网友评论

    本文标题:odoo12 如何在tree视图顶部增加按钮

    本文链接:https://www.haomeiwen.com/subject/cxkmoctx.html