Jeenit khatri
Jeenit khatri

Reputation: 318

How to add button in odoo?

I want to add a button before or after the "create" Button in Tree view That calls the action of another view.

But as I tried header tag is not working in xml to add the button in header of the odoo.

Upvotes: 1

Views: 4662

Answers (1)

Kenly
Kenly

Reputation: 26768

You need to extend ListView.buttons QWEB template.

Define a QWEB template under static/src/xml which adds the button:

<?xml version="1.0" encoding="utf-8"?>

<template xml:space="preserve">
    <t t-extend="ListView.buttons">
        <t t-jquery="button.oe_list_add" t-operation="after">
            <button t-if="widget.dataset.model == 'model_name'" class="oe_button oe_my_button oe_highlight" type="button">My Button</button>
        </t>
    </t>
</template>

Use JavaScript to define the logic of the button ( create a file under static/src/js):

openerp.module_name = function(instance){

instance.web.ListView.include({
    load_list: function(data) {
        this._super(data);
        if (this.$buttons) {
            this.$buttons.find('.oe_my_button').off().click(this.proxy('do_the_job')) ;
        }
    },
    do_the_job: function () {

        this.do_action({
            name: _t("View name"),
            type: "ir.actions.act_window",
            res_model: "object",
            domain : [],
            views: [[false, "list"],[false, "tree"]],
            target: 'new',
            context: {},
            view_type : 'list',
            view_mode : 'list'
        });
    }
});
}

Define a new view that will add module assets (module_name_view.xml):

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
      <template id="assets_backend_module_name" name="module_name assets" inherit_id="web.assets_backend">
        <xpath expr="." position="inside">
            <script type="text/javascript" src="/module_name/static/src/js/script.js"></script>
        </xpath>
    </template>
</data>

Edit __openerp__.py and add the following sections:

'data': [
    'module_name_view.xml',
    ...
],
'qweb': ['static/src/xml/*.xml'],

Look at Building Interface Extensions for more details.

Upvotes: 2

Related Questions