Skip to Content
Menu
This question has been flagged
3 Replies
10226 Views

Hello everyone,I hope all of you are fine.I have  one question,the question is that suppose I have a field of type selection in my class model.Suppose following is the field :


q_test = fields.Selection ( selection = [( 'n' , 'Normal' ), ( 'abn' , 'Abnormal' )], string = 'my field' )

Now suppose I want that when the user selects Abnormal option from the selection field,the text displayed to the user on frontend should be red,and if the user selects the option Normal,then it should be displayed as normal (i.e Black color font).

How can I achieve this,should I use some widget for it or is their any builtin option available in Odoo that can help me perform this task.

Thank you in advance.

Avatar
Discard
Best Answer

Hello Omar
You can apply one trick to show different color for selection field.

<field name="gender" string="Gender" style="color: red;" attrs="{'invisible': ['|',('gender','=','female'),('gender', '=', False)]}"/>
<field name="gender" string="Gender" style="color: green;" attrs="{'invisible': ['|',('gender','=','male'),('gender', '=', False)]}"/>
<field name="gender" string="Gender"  attrs="{'invisible': ['|',('gender','=','male'),('gender','=','female')]}"/>

Thanks.
Avatar
Discard
Best Answer

Hello Omar,

Might this work for you

Add decoration in view

Avatar
Discard

<field name="q_test" decoration-danger="q_test == 'abn'" />

Author Best Answer

This is not working in my case,and also even if it works,then dont you think its not that good an approach because its quite static,hard coded,as you can see that in this example we have just 2 values,suppose when we have more than 2 values,like 10,15 values and I want to give different colors to them

For example the field is country=fields.Selection() and in that field we have 30 countries,so I will have to give different color to all the fields by writing <field='country'.....> 30 times.So is their any better and dynamic solution for this kind of problem like using some widgets or javascript code by which we get the value of selection field when user enters the data like ,document.getElementById('xyz').fontcolor='blue' ,something like this?Any such solution?Does that exist?Suggestion on this problem will be highly appreciated.

Avatar
Discard
Related Posts Replies Views Activity
3
Dec 19
2930
2
Oct 24
176
0
Apr 22
2402
1
May 21
2744
3
Mar 24
5220