1

Basic info:

Odoo version : 10.0

module name : simcard

model name : simcard.simcard

Aim : to add a Sync button in header of the tree view and link it to a python function.

My template file (template.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="replace">

        <t t-if="widget.model=='simcard.simcard'">

            <button class="btn btn-sm btn-default sync_button" type="button" >Sync</button>

        </t>

    </t>

</t>

</templates>

My javascript file(tree_view_button.js):

odoo.define('simcard.tree_view_button', function (require){"use strict";
    var ListView = instance.web.ListView;
    ListView.include({
    render_buttons: function() {

   // GET BUTTON REFERENCE
    this._super.apply(this, arguments)
    if (this.$buttons) {
    var btn = this.$buttons.find('.sync_button')
    }

   // PERFORM THE ACTION
    btn.on('click', this.proxy('do_sync'))

   },
    do_sync: function() {
    new instance.web.Model('simcard.simcard')
    .call('my_function', [[]])
    .done(function(result) {
    alert('done')
    })
    }
    });
   }

My python file function(models.py) :

  def my_function(self): 
    print 'fooooooooooooooo' 

My view file (view.xml):

   <?xml version="1.0" encoding="utf-8"?>
   <odoo>
    <data>
     <template id="assets_backend" name="tree view menu" 
      inherit_id="web.assets_backend">               
        <xpath expr="." position="inside">                   
           <script type="text/javascript" 
               src="simcard/static/js/tree_view_button.js"> 
           </script>               
        </xpath>           
    </template> 
  </data>
 </odoo>

My manifest file :

  # -*- coding: utf-8 -*-
 {
'name': "simcard",

'summary': """
    Store them""",

'description': """
    Store them""",


# Categories can be used to filter modules in modules listing
# Check https://github.com/odoo/odoo/blob/master/odoo/addons/base/module/module_data.xml
# for the full list
'category': 'Uncategorized',
'version': '0.1',

# any module necessary for this one to work correctly
'depends': ['base'],

# always loaded
'data': [
    # 'security/ir.model.access.csv',
    'views/views.xml',
    'views/templates.xml',
],

'qweb': ['static/xml/tree_view_button.xml'],

# only loaded in demonstration mode
'demo': [
    'demo/demo.xml',
],
'installable': True,
'auto_install': False,
'application': True,
}

Sync button appears in the tree view header but i am not able to call my function when i click on that button. Is there anything i am missing??

1 Answer 1

1

You have some errors in your js code, the following code should work as expected

odoo.define('simcard_piavita.tree_view_button', function (require){
"use strict";
    var ListView = require('web.ListView');
    var Model = require('web.DataModel');
    ListView.include({
        render_buttons: function() {
            this._super.apply(this, arguments)
            if (this.$buttons) {
                var btn = this.$buttons.find('.sync_button')
                btn.on('click', this.proxy('do_sync'))
            }
       },
        do_sync: function() {
            new Model('simcard_piavita.simcard_piavita')
                .call('my_function', [[]])
                .done(function(result) {
                    alert('done')
                })
        }
    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

one question : where is this javascript documented? i would like to understand whats happening in javascript code so that i can create more complex code.
One more question : How to pass a variable from this javascript to python function?
Thanks. Arguments for the function call need to be on the line
.call('my_function', [[]])
i tried call('my_function', [[my_argument]]) but didnt work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.