2

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

Koul
  • 77
  • 1
  • 9

2 Answers2

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.

Charif DZ
  • 14,415
  • 3
  • 21
  • 40
  • thanks for your answer. What about with XML? Can't I use XML as for `decoration-success` in `` views? – Koul Jun 14 '19 at 09:52
  • we can't select it using CSS, but we can hide the fields based on value, and style each field with colors. I agree it's laborious though – Koul Jun 14 '19 at 10:20
1

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']]}"/>
Koul
  • 77
  • 1
  • 9
  • Watchout from the side effect of this you should not have a field more than one time in Odoo, I wonder if this works I think It work only if the field is readonly. is this field editable? – Charif DZ Jun 14 '19 at 10:30
  • you mean we shouldn't have the same field declared more than one time in the XML view? Yes this field is editable, also it's a selection field. I hope not to have any side effects, if any I'll come back here! – Koul Jun 14 '19 at 10:39
  • 1
    You should have, Odoo always pics the value of the last field, I don't know if this going to be a problem for you, and yes This what I wanted to say – Charif DZ Jun 14 '19 at 11:16