Skip to Content
Menu
This question has been flagged
2 Replies
19690 Views

I have a standard form view that includes a computed float field as follows.

<field name="x_margin"/>

I would like to change the color of the field based on it's value.

For example

0 < x_margin < 0.1 == red

0.1 < x_margin < 0.2 == yellow

x_margin > 0.2 == green

I know I can set the color of the field like this

<field name="x_margin" style="color:red"/>

but I can't figure out how to apply conditional formatting to a style.  Everything I've found in searching seems to only apply to the tree view. I would like to apply this to a field in a form view.

Avatar
Discard
Best Answer

Can you just put a label above/below/near the field?





Don't  follow this snippet exactly - I used the existing MARGIN field which is a flat dollar margin amount - but you should be able to get what you want if you understand what I did here:

<label colspan="2"

            string="YOU ARE FIRED IF YOU SELL AT THESE PRICES"

            attrs="{'invisible': [('margin','&lt;', 45)]}" style="background-color:yellow;"/>

<label colspan="2"

            string="HEALTHY COMMISSION CHECK COMING YOUR WAY!"

            attrs="{'invisible': [('margin','&gt;=', 45)]}" style="background-color:palegreen;"/>

Avatar
Discard
Author Best Answer

Ray Carnes thanks for the help - I used your idea and did this instead.  

<field name="x_elec_lab_mar" attrs="{'invisible':[('x_elec_lab_mar','&lt;',0.3)]}" style="background-color:yellow;" nolabel="1" readonly="1"/>


<field name="x_elec_lab_mar" attrs="{'invisible':[('x_elec_lab_mar','&gt;',0.3)]}" style="background-color:green;" nolabel="1" readonly="1"/>

So basically I'm showing the field twice and just making the other condition invisible so it just shows one at a time.  Not the most elegant solution but it does work.  

I was hoping there was a way to directly inject some conditional statement for the style attribute.

Avatar
Discard

Just check the the field has a value both times - there aren't many cases where you can show a field twice in the same view and have the value work on the second occurance of the field.

Author

I actually have it on there 3 times (3 colors) and it seems to work for all cases. I tested it and changing a value changes all 3 instances. Thanks for the warning though. I will keep an eye on it.

Related Posts Replies Views Activity
2
Jun 17
5734
1
Feb 25
329
1
Mar 23
3536
1
Mar 25
2226
2
Nov 20
2760