Search code examples
odooodoo-14odoo-15

Hide field from tree view base on the condition, Odoo


I want to hide field from tree view depends on the condition. I tried some techniques, but didn't work for me. Currently I can hide just value not all column. I want to hide whole column.

This is my tree view. And this code just hide the value not column.

<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"/>
                <field name="state" class="bg-danger" attrs="{'invisible': [('state', '!=', 'unapproved')]}"/>
                <field name="state" class="bg-success" attrs="{'invisible': [('state', '!=', 'approved')]}"/>
                <field name="state" class="bg-warning" attrs="{'invisible': [('state', '!=', 'pending')]}"/>
            </tree>
        </field>
</record>

Actually, as you can see in the code, I just want to change the colors depends on the states. So is there any simple solution for this?


Solution

  • Odoo allows you to change the behavior of the text based on the attributes of the corresponding record.

    This documentation is for tree views, but what they describe also works for fields.

    <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"/>
                <field name="state"
                    decoration-danger=="state != 'unapproved'"
                    decoration-success="state != approved'"
                    decoration-warning="state != 'pending'"/>
            </tree>
        </field>
    </record>