Ibrahim Rahimi
Ibrahim Rahimi

Reputation: 539

How to change color of a column based on a field's value in Odoo12?

I have inherited a tree view and added a selection field to this view. I need to have different colors based on the value of the field. I want to know, is it possible in Odoo 12 to have values of a column with different colors or not. I have tried blew code but it does not work in my case.

python code:

availability = fields.Selection(string="Availability", compute='_compute_physician_availability', selection=[('available', "Available"), ('not_available', "Not Available")])

def _compute_physician_availability(self):
        for physician in self:
            employees = self.env['hr.employee'].search([('user_id', '=', physician.user_id.id)])
            for employee in employees:
                if employee.attendance_state == 'checked_in':
                    physician.availability = 'available'
                else:
                    physician.availability = 'not_available'

XML code:

<record id="physician_tree_view_inherited" model="ir.ui.view">
        <field name="name">Physician Tree View Inherited</field>
        <field name="model">hms.physician</field>
        <field name="inherit_id" ref="acs_hms.view_physician_tree"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='email']" position="after">
                <field name="availability" options="{'color': 'red: availability == Not Available'}"/>
            </xpath>
        </field>
    </record>

I have also tried below code, but it also does not worked in my case.

 <field name="availability" colors=="red:availability=='not_available'; green:availability=='available'"/>

I need to have different colors for each state of my physician for more user-friendliness of my tree view and it is a requirement by the client.

Upvotes: 0

Views: 2632

Answers (1)

Younis Mahsud
Younis Mahsud

Reputation: 517

Odoo 9th and odoo 10th version support custom colors in tree view but Odoo 11th and odoo 12th version support decorators only.

The possible decorators are:

decoration-bf - shows the line in BOLD 
decoration-it - shows the line in ITALICS 
decoration-danger - shows the line in LIGHT RED 
decoration-info - shows the line in LIGHT BLUE 
decoration-muted - shows the line in LIGHT GRAY 
decoration-primary - shows the line in LIGHT PURPLE 
decoration-success - shows the line in LIGHT GREEN 
decoration-warning - shows the line in LIGHT BROWN

Example

<record id="physician_tree_view_inherited" model="ir.ui.view">
    <field name="name">Physician Tree View Inherited</field>
    <field name="model">hms.physician</field>
    <field name="inherit_id" ref="acs_hms.view_physician_tree"/>
    <field name="arch" type="xml">
        <xpath expr="//tree" position="attributes">
            <attribute name='decoration-danger'>availability='not_available'</attribute>
            <attribute name='decoration-success'>availability='available'</attribute>
        </xpath>
    </field>
</record>

Upvotes: 1

Related Questions