3

I am trying to add a button in the tree view of sale order module, next to create and import buttons. That button will execute a python method.

I have created my custom module, extending sale order module and then, I have followed these steps:

Step 1: Create the button in 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>

Step 2: Add the file to the qweb section in __openerp.py__ of my module:

'depends': ['sale'],
'data': [],
'qweb': ['static/src/xml/qweb.xml'],

Now, the button appears.

Step 3: Create the python method to give functionality to the button in 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):
    ...

Note: The python method has been tested outside of odoo and works fine.

How can I link this python method with the button?

MouTio
  • 1,249
  • 23
  • 45

2 Answers2

5

You need to extend the 'ListView' widget adding a click listener. Also add the '@api.model' decorator to your method, so you can call it from js with the 'call' method. Something like this:

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 >
            })
})
  • I put it in the update_sales_button.js file and now in the browser console it shows this: Uncaught ReferenceError: instance is not defined at update_sales_button.js:1 – MouTio Apr 11 '17 at 07:22
  • 1
    I think it depends on which js enclosing method you are using. If you are using the old way: *'openerp.your_object_name = function(instance, local) {}'* you can use *'instance'*, otherwise in the new style *'odoo.define('your_widget_name', function(require) {})'* you have to get the ListView widget through the *'require('web.ListView')'*. You assign it to a variable like: *var ListView = require('web.ListView')* and then simply call *ListView.include({ < all the stuff > })*. Try to have a look to the *'addons/web/static/src/js/views/list_view.js'* file. – Michele Zaccheddu Apr 11 '17 at 08:15
2

I'm using odoo 11 and I had to replace widget.model with widget.modelName in the topic starter's template (the former is an object, the latter is a string). Also, to append the button to the end of row I've changed t-operation to "append" while looking for the parent div:

<t t-extend="ListView.buttons">
    <t t-jquery="div.o_list_buttons" t-operation="append">
        <t t-if="widget.modelName=='sale.order'">
            <button class="btn btn-sm btn-default import_email_button" type="button">
                Import E-mail Order
            </button>
        </t>
    </t>
</t>
MouTio
  • 1,249
  • 23
  • 45
skirill
  • 354
  • 3
  • 7