简体   繁体   中英

Odoo 9 How to Create Action on a Custom Button

I have been able to create custom button Add Bro using xml.

在此处输入图片说明

Here's the 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>

My question is, how can I create action to this button that will be invoked when I hit the button. I have tried create method with name action so it mathes the name attribute of the button but nothing happened.

@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

You need to extend ListView widget and add a trigger to your button:

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'
                );
        }
    });
}

Create a js file ( script.js ) containing the above code under /static/src/js/ and an xml file (`module_view.xml) containing the following code:

<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",

    ...
],

...

in your XML code look's like that way because this template not call the any method directly so you can use the xpath

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

     <!-- put your button here -->

</xpath>

example:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM