如何在树视图标题中创建一个按钮(在创建和导入按钮旁边)并赋予它功能?在 odoo 9

Mou*_*Tio 3 openerp qweb odoo-9

我正在尝试在销售订单模块的树视图中添加一个按钮,旁边是创建导入按钮。该按钮将执行一个 python 方法。

我已经创建了我的自定义模块,扩展了销售订单模块,然后,我遵循了以下步骤:

第 1 步:在 my_module/static/src/xml/qweb.xml 中创建按钮:

<?xml version="1.0" encoding="UTF-8"?>
<templates id="template" xml:space="preserve">
  <t t-extend="ListView.buttons">
    <t t-jquery="button.o_list_button_add" t-operation="after">
      <t t-if="widget.model=='sale.order'">
        <button class="btn btn-sm btn-primary update_sales_button" type="button">Run my stuff</button>
      </t>
    </t>
  </t>
</templates>
Run Code Online (Sandbox Code Playgroud)

第 2 步:将文件添加到我模块的 __openerp.py__ 中的 qweb 部分:

'depends': ['sale'],
'data': [],
'qweb': ['static/src/xml/qweb.xml'],
Run Code Online (Sandbox Code Playgroud)

现在,按钮出现。

第 3 步:创建 python 方法来为 my_module/my_python_file.py 中的按钮提供功能:

from openerp import api, fields, models, _

class SaleOrderExtended(models.Model):
  _inherit = ['sale.order']

  @api.multi
  def update_sales_button(self):
    ...
Run Code Online (Sandbox Code Playgroud)

注意: python 方法已经在 odoo 之外进行了测试并且工作正常。

如何将这个 python 方法与按钮链接起来?

Mic*_*ddu 5

您需要扩展“ListView”小部件,添加一个点击侦听器。还要将'@api.model'装饰器添加到您的方法中,以便您可以使用'call'方法从 js 调用它。像这样的东西:

ListView = require('web.ListView')

ListView.include({
    render_buttons: function() {

        // GET BUTTON REFERENCE
        this._super.apply(this, arguments)
        if (this.$buttons) {
            var btn = this.$buttons.find('.update_sales_button')
        }

        // PERFORM THE ACTION
        btn.on('click', this.proxy('do_new_button'))

    },
    do_new_button: function() {

        instance.web.Model('sale.order')
            .call('update_sale_button', [[]])
            .done(function(result) {
                < do your stuff, if you don't need to do anything remove the 'done' function >
            })
})
Run Code Online (Sandbox Code Playgroud)