2016-10-10 24 views
0

我想创建一个模块,在某些条件下控制编辑按钮。我在js中尝试了下面的代码,但没有效果。所以我想知道如何在js中扩展一个函数。如何在Odoo中的JavaScript上扩展此功能(编辑按钮)?

formView.include({ 
    init:function(){ 

     var edits = new Model('sale.order'); 
     edits.query(['validity_date']); 
     console.log(validity_date) 
     }, 
    on_button_edit: function(){ 
     this._super(); 

回答

1

你可以在js文件中写这样的东西。我写了一些例子来帮助你。

openerp.custom_edit_button = function (instance) { 
    var _t = instance.web._t; 

    instance.web.FormView.include({ 
     init: function() { 
      console.log('JS loaded') 
      this._super.apply(this, arguments); 
     }, 

     to_edit_mode: function(){ 
      // examples of useful methods 
      var field_values = this.get_fields_values(); 
      var ids = this.get_selected_ids(); 
      var id = field_values['id']; 
      var date = field_values['date']; 
      var model = this.model; 

      console.log(field_values) 
      console.log(ids) 
      console.log(id) 
      console.log(model) 
      console.log(date) 
      console.log(Date.today()) 

      date_to_compare = new Date(date); 
      console.log(date_to_compare) 

      if(date_to_compare < Date.today()){ 
       error = this.error; 
       var QWeb = instance.web.qweb; 
       var dialog = new instance.web.Dialog(this, { 
        title: _t("Set new expiry date"), 
        width: '30%', 
        size: 'medium', 
        /*dialogClass: 'oe_act_window',*/ 
        buttons: [ 
          { text: _t("OK"), click: function() { self.set_new_expiry_date(); }}, 
          { text: _t("Close"), click: function() { dialog.close(); return; } 
          },       
         ], 
       }, QWeb.render('custom_edit_button.expiry_date_form', {error: error})).open(); 

      } 

      this._super(); 
     } 
    }); 
} 

因此,如果过期日期在过去,表格将会出现改变它。您还必须定义方法set_new_expiry_date。另一方面,您必须添加此模板或类似的内容来显示表单。在文件中添加您__openerp__.py

<templates xml:space="preserve"> 
    <div t-name="custom_edit_button.expiry_date_form" > 
     <div class="form-group"> 
      <label for="date" class="control-label">New expiry date:</label> 
      <input name="date" class="form-control"/> 
     </div> 
    </div> 
</templates> 

通知的qweb节我模块的名称是在例如custom_edit_button

+0

感谢回答是基于现有到期日正是我试图('validity_date') 'sale.order'上的字段,只有在到期日超过当前日期时才启用编辑模式。 – 111sree

+0

嗯,我认为你不能这样做,因为编辑按钮出现时你没有记录的ID。另一种选择是在每个字段或组或字段中使用'attrs'属性。 – ChesuCR

+0

我更新了我的答案,检查它是否对您有用 – ChesuCR

相关问题