Search code examples
xmltextformatconditional-statementsodoo

Odoo XML Conditional Formatting Text


I am trying to edit the xml code on Odoo using the studio app. I want to format text colour based on the value of the cell - red for some values, green for others.

Please can someone help?

I used an answer on this previous topic as a guide. XML Odoo field conditional color formatting

<field name="x_studio_mr_approval" string="MR Approval (test)" style="color: red;" attrs="{'invisible': ['|',['x_studio_mr_approval','=','Approved'],['x_studio_mr_approval','=','Approved Unseen']]}"/>

<field name="x_studio_mr_approval" string="MR Approval (test)" style="color: green;" attrs="{'invisible': ['|',['x_studio_mr_approval','=','New'],['x_studio_mr_approval','=','Awaiting Approval'],['x_studio_mr_approval','=','Rejected']]}"/>

I would expect to see "Approved" and "Approved Unseen" as Green, with the other 3 options being red.

What happens with my current code is this: - If the value is "Approved" or "Approved Unseen", it works correctly and shows the green field. The red field is invisible.

  • If the value is "Awaiting Approval" or "New" or "Rejected", it does not work correctly. It shows the field and value twice on the form view. One field is red, the other is green. It doesnt make the green field invisible for some reason

Solution

  • A better use of domains could help:

    <field name="x_studio_mr_approval" string="MR Approval (test)" style="color: red;" attrs="{'invisible': [('x_studio_mr_approval','in',['Approved','Approved Unseen'])]}"/>
    <field name="x_studio_mr_approval" string="MR Approval (test)" style="color: green;" attrs="{'invisible': ['!',('x_studio_mr_approval','in',['Approved','Approved Unseen'])]}"/>
    

    For reference, read https://www.odoo.com/documentation/12.0/reference/orm.html#domains