2017-04-10 62 views
1

我试图在销售订单模块的树视图中添加一个按钮,旁边的创建导入按钮。该按钮将执行一个python方法。如何在树形视图标题(创建和导入按钮旁边)中创建按钮并为其提供功能?在odoo 9

我创建了我的自定义模块,扩展销售订单模块,然后,我按照下列步骤操作:

第1步:创建my_module /静态/ 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> 

第2步:将文件添加到qweb节__openerp.py__我模块:

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

现在,出现按钮。

步骤3:创建蟒方法在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): 
    ... 

注:蛇皮方法已odoo以外测试和工作正常。

如何链接此python方法与按钮?

回答

2

您需要延长“ListView的”小部件添加点击侦听器。 另外,“@ api.model”装饰添加到您的方法,这样你就可以从与JS调用它称之为“方法。 事情是这样的:

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 > 
      }) 
}) 
+0

我把它放在update_sales_button.js文件,现在在浏览器控制台就说明这一点: 未捕获的ReferenceError:实例没有在update_sales_button.js定义:1 – MouTio

+1

我认为这取决于其中js包含您正在使用的方法。如果您使用的是旧方法:*'openerp.your_object_name = function(instance,local){}'*您可以使用*'instance'*,否则以新样式*'odoo.define('your_widget_name',function require){})'*您必须通过*'require('web.ListView')'*获取ListView小部件。你将它赋值给一个变量,如:* var ListView = require('web.ListView')*,然后简单地调用* ListView.include({<所有的东西>})*。尝试查看*'addons/web/static/src/js/views/list_view.js'*文件。 –