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

Odoo Tree View中根据状态设置字段颜色及列隐藏问题求助

Odoo Tree View 列隐藏与动态背景色解决方案

问题根源

你之前重复定义了3个state字段,导致生成多列,且attrs的invisible仅控制单单元格内容隐藏,无法实现整列隐藏;同时重复字段也无法正确绑定动态样式。

解决方案

1. 给State列设置动态背景色

只保留一个state字段,通过attrs动态绑定样式类,就能实现不同状态对应不同背景色:

XML视图代码

<record id="view_tai_approval_tree" model="ir.ui.view">
    <field name="name">tai.approval.tree</field>
    <field name="model">tai.approval</field>
    <field name="arch" type="xml">
        <tree create="false">
            <field name="tai_reference"/>
            <field name="tai_project_name"/>
            <field name="responsible_name"/>
            <field name="tai_purchase_date"/>
            <field name="tai_supplier_name"/>
            <!-- 单个state字段,动态绑定样式类 -->
            <field name="state" attrs="{'class': [
                ('state', '=', 'unapproved'): 'bg-danger',
                ('state', '=', 'approved'): 'bg-success',
                ('state', '=', 'pending'): 'bg-warning'
            ]}"/>
        </tree>
    </field>
</record>

可选:补充CSS确保样式生效

如果Odoo默认的背景色类没起作用,加一段自定义CSS(通过web.assets_backend资产引入):

.o_list_view .bg-danger {
    background-color: #dc3545 !important;
    color: #fff;
}
.o_list_view .bg-success {
    background-color: #28a745 !important;
    color: #fff;
}
.o_list_view .bg-warning {
    background-color: #ffc107 !important;
    color: #000;
}

2. 整列隐藏(全局条件触发)

整列隐藏是针对整个列的显示/隐藏,不能基于每行的state值(那是单元格隐藏),必须用全局条件,比如用户权限、上下文参数:

示例1:按用户组控制显示

只有特定组的用户能看到state列:

<field name="state" 
       attrs="{'class': [
           ('state', '=', 'unapproved'): 'bg-danger',
           ('state', '=', 'approved'): 'bg-success',
           ('state', '=', 'pending'): 'bg-warning'
       ]}"
       groups="your_module.group_tai_approval_view_state"/>

示例2:按上下文参数控制

通过上下文参数hide_state_column控制是否隐藏:

<field name="state" 
       attrs="{'class': [
           ('state', '=', 'unapproved'): 'bg-danger',
           ('state', '=', 'approved'): 'bg-success',
           ('state', '=', 'pending'): 'bg-warning'
       ],
       'invisible': [('context.get', 'hide_state_column', False)]}"/>

额外技巧:整行背景色

如果需要整行根据state变色,直接在tree标签上加decoration-*属性:

<tree create="false" 
      decoration-danger="state == 'unapproved'"
      decoration-success="state == 'approved'"
      decoration-warning="state == 'pending'">
    <!-- 字段定义 -->
</tree>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 14:54:24