【问题标题】:Odoo - Disable selection option in tree view for some rows based on conditionOdoo - 根据条件在树视图中禁用某些行的选择选项
【发布时间】:2021-03-16 14:23:49
【问题描述】:

如何根据条件禁用某些行的树视图中的选择选项?

【问题讨论】:

    标签: javascript python-3.x xml odoo odoo-12


    【解决方案1】:

    为每个复选框提供一个唯一的 ID。

    使用 Jquery 或 javascript onLoad 或 document.ready 方法禁用复选框。创建 for 循环并为您的匹配值编写简单的禁用代码。

    document.getElementById("myCheck").disabled = true;

    我不确定 odoo。但我对odoo有一点了解。如果动态数据来自数据库,则必须为此设置一个数据字段。

    【讨论】:

      【解决方案2】:

      根据documentation,没有多重编辑功能。

      复选框选择器添加在ListRenderer小部件的_renderSelector函数中,因此您需要覆盖它以根据条件禁用复选框。

      在以下示例中,当state(硬编码)字段值等于draft 时,我们禁用选择复选框。

      示例:

      odoo.define('web.CustomListRenderer', function (require) {
      "use strict";
      
          var ListRenderer = require('web.ListRenderer');
      
          ListRenderer.include({
              _renderRow: function (record) {
                  var self = this;
                  var tr = this._super(record);
                  tr.find("input[type='checkbox']").prop('disabled', record.data.state == 'draft');
                  return tr;
              },
          });
      });
      

      您可以在操作上下文中传递一个评估域,然后使用当前记录值在_renderSelector 函数中评估该域。

      我们在init函数中初始化domain,并使用compute函数得到评估结果:

      odoo.define('web.CustomListRenderer', function (require) {
      "use strict";
          var ListRenderer = require('web.ListRenderer');
          var Domain = require('web.Domain');
      
          ListRenderer.include({
              init: function (parent, state, params) {
                  this._super.apply(this, arguments);
                  if (state.context.selection_domain) {
                      this.domain = new Domain(state.context.selection_domain, state.context);
                  }
              },
      
              _renderRow: function (record) {
                  var self = this;
                  var tr = this._super(record);
                  if (record.evalContext.selection_domain) {
                      tr.find("input[type='checkbox']").prop('disabled', this.domain.compute(record.data));
                  }
                  return tr;
              },
          });
      });
      

      您需要将selection_domain 添加到操作上下文中:

       {..., 'selection_domain': [('state', '=', 'draft')]}
      

      查看Assets Management 文档以了解如何将上述 JavaScript 代码添加到网络资产,您还可以在Adding files in an asset bundle 部分查看示例。

      【讨论】:

      • 我不精通 JS。今天要试试这个,如果它有效,将更新。由于时间限制,我确实喜欢将行的颜色更改为灰色以欺骗客户。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-10
      • 2021-02-08
      • 2016-10-17
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多