Odoo 9如何在自定义按钮上创建操作

时间:2016-05-19 12:04:52

标签: python openerp odoo-8 odoo-9

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

enter image description here

这里是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

2 个答案:

答案 0 :(得分:3)

您需要扩展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'
                );
        }
    });
}

创建一个js文件(script.js),其中包含/static/src/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",

    ...
],

...

答案 1 :(得分:2)

你的XML代码中的

看起来就是这样,因为这个模板没有直接调用any方法所以你可以使用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>