You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo v14自定义列表渲染器JS代码修改数据后失效求助

问题解决:Odoo列表视图编辑后复选框禁用逻辑失效

问题分析

你的代码在页面首次加载时生效,是因为_renderRow会在每行初始化渲染时执行,但当编辑行数据后,Odoo会进行局部DOM更新,不会重新调用_renderRow,导致之前设置的禁用状态被重置,复选框恢复可用。

修正后的代码

odoo.define('de_alfa_parc.CustomListRenderer', function (require) {
"use strict";
    var ListRenderer = require('web.ListRenderer');

    ListRenderer.include({
        _renderRow: function (record) {
            var tr = this._super(record);
            this._updateCheckboxDisabledState(tr, record);
            return tr;
        },

        _recordUpdated: function (record, params) {
            this._super.apply(this, arguments);
            var $row = this.$('tr[data-id="' + record.id + '"]');
            this._updateCheckboxDisabledState($row, record);
        },

        _updateCheckboxDisabledState: function ($row, record) {
            var categ = ['Maintenance','Installation','Transport',"Volume d'impression"];
            var shouldDisable = record.data.stock <= 0 && !categ.includes(record.data.small_category_name_field);
            $row.find("input[type='checkbox']").prop('disabled', shouldDisable);
        }
    });
});

代码说明

  1. 抽离_updateCheckboxDisabledState方法,统一处理复选框禁用逻辑,避免重复代码
  2. 在_renderRow中调用该方法,保证首次渲染时生效
  3. 重写_recordUpdated方法:当记录数据更新后(比如编辑保存),找到对应的行DOM,重新应用禁用逻辑
  4. 简化条件判断:!categ.includes(...)本身是布尔值,不需要额外== true

关键原理

Odoo的列表视图在记录更新时,会触发_recordUpdated钩子,我们可以利用这个钩子对更新后的行重新执行状态设置,确保编辑后禁用逻辑依然生效。

内容的提问来源于stack exchange,提问作者DZEUF

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 00:13:15