问题
I'm working with Odoo and studio.
I want to format a selection field text based on the selected value using XML.
Here is what I have already tried but it has no effect at all:
<field name="my_status" string="Status" colors="orange:my_status == 'negotiation';green:my_status == 'signed';purple:my_status == 'internal'"/>
I have also tried:
<field name="my_status" string="Status" decoration-success="my_status == 'signed'" decoration-danger="my_status == 'negociation'" decoration-muted="my_status == 'internal'"/>
Do you know what I am missing for it to work?
EDIT: I found this, I guess I can't color format any of the fields? https://github.com/odoo/odoo/blob/12.0/odoo/addons/base/rng/common.rng#L206
回答1:
If you want to do this you need css selectors. when you invesitigate Element in a browser you have two different representation.
in view mode the selection field is turned to a simple span:
<span name="priority" class="o_field_widget">Non urgent</span>
in Edit mode the selection field Is HTML input:
<select class="o_input o_field_widget" name="priority" id="o_field_input_229">
<option value="false"></option>
<option value="0">Non urgent</option>
<option value="1">Normale</option>
<option value="2">Urgent</option>
<option value="3">Très urgent</option>
</select>
There is no way to select an element based on it's Inner HTML using CSS only. and as you can see option values changes as the language changes so even if you do a selector like (Jquery)
$(".o_field_widget[name='priority']:contains('Non urgent')")
What happen when A french user access the page?!!!
a typical way to do is to change the behavior of the selection widget in Odoo (not an easy thing to do). If you really need this try first to find any app in Odoo store else I don't see another way to do it.
回答2:
I have found a workaround:
<field name="my_status" string="Status" style="color: red;" attrs="{'invisible': ['|',['my_status','=','signed'],['my_status','=','internal']]}"/>
<field name="my_status" string="Status" style="color: green;" attrs="{'invisible': ['|',['my_status','=','negotiation'],['my_status','=','internal']]}"/>
<field name="my_status" string="Status" style="color: purple;" attrs="{'invisible': ['|',['my_status','=','signed'],['my_status','=','negotiation']]}"/>
来源:https://stackoverflow.com/questions/56524496/xml-odoo-field-conditional-color-formatting