【问题标题】:Odoo 9 How to Create Action on a Custom ButtonOdoo 9 如何在自定义按钮上创建操作
【发布时间】:2016-05-19 12:04:52
【问题描述】:

我已经能够使用 xml 创建自定义按钮 Add Bro

这是xml

<templates>
  <tr t-extend="ListView.buttons">
    <t t-jquery="button.o_list_button_add" t-operation="after">
        <button id="tahu" name="action" type="object" class="btn btn-sm btn-primary">
            Add Bro
        </button>
    </t>
  </tr>
</templates>

我的问题是,如何为该按钮创建在我点击按钮时将被调用的操作。我尝试使用名称为 action 的创建方法,所以它计算了按钮的 name 属性,但什么也没发生。

@api.multi
def action(self):
    view_ref = self.env['ir.model.data'].get_object_reference('account', 'invoice_form')
    view_id = view_ref[1] if view_ref else False

    res = {
       'type': 'ir.actions.act_window',
       'name': _('Customer Invoice'),
       'res_model': 'purchase.order',
       'view_type': 'form',
       'view_mode': 'form',
       'view_id': view_id,
       'target': 'new',
       # 'context': {'default_partner_id': client_id}
    }

    return res

【问题讨论】:

    标签: python openerp odoo-8 odoo-9


    【解决方案1】:

    您需要扩展ListView 小部件并为您的按钮添加触发器:

    openerp.you_module_name_here = function(instance){
    
        var _t = instance.web._t,
            _lt = instance.web._lt;
        var QWeb = instance.web.qweb;
    
        instance.web.ListView.include({
    
            load_list: function(data) {
                if (this.$buttons) {
                    this.$buttons.find('#tahu').click(this.proxy('action')) ;
                }
            },
    
            action: function () {
                var model_obj = new instance.web.Model('ir.model.data');
                view_id = model_obj.call('get_object_reference', ["account", "invoice_form"]);
    
                this.do_action(
                    name:  _t('Customer Invoice'),
                    type: 'ir.actions.act_window',
                    res_model: 'purchase.order',
                    view_type: 'form',
                    view_mode: 'form',
                    view_id: view_id,
                    target: 'new'
                    );
            }
        });
    }
    

    /static/src/js/ 下创建一个包含上述代码的js 文件(script.js)和一个包含以下代码的xml 文件(`module_view.xml):

    <template id="assets_backend_custom" name="custom assets" inherit_id="web.assets_backend">
        <xpath expr="." position="inside">
            <script type="text/javascript" src="/your_module_name_here/static/src/js/script.js"></script>
            </xpath>
    </template>
    

    __openerp__.py

    ...
    
    'data': [
        ...
    
        "module_view.xml",
    
        ...
    ],
    
    ...
    

    【讨论】:

      【解决方案2】:

      在您的 XML 代码中看起来像这样,因为此模板不直接调用任何方法,因此您可以使用 xpath

      <xpath expr="/form/header/button[@name='invoice-open']" position="after">
      
           <!-- put your button here -->
      
      </xpath>
      

      示例:

      <record id="invoice_form_my" model="ir.ui.view">
      
                  <field name="name">account.invoice.form.my</field>
      
                  <field name="model">account.invoice</field>
      
                  <field name="inherit_id" ref="account.invoice_form"/>
      
                  <field name="arch" type="xml">
      
      
      
                      <xpath expr="/form/header/button[2][@string='Print']" position="after">
      
                          <button name="my_button" string="Print2" class="oe_highlight"/>
      
                      </xpath>
      
                  </field>
      
             </record>
      

      【讨论】:

        猜你喜欢
        • 2017-10-27
        • 2022-11-12
        • 1970-01-01
        • 1970-01-01
        • 2017-08-26
        • 1970-01-01
        • 2017-08-05
        • 2018-06-25
        • 1970-01-01
        相关资源
        最近更新 更多