Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
9707 Vistas

When I add

widget="many2many_checkboxes" 

to a m2m field, it will show them by their ids (id=1, id=2,...). Is there a way to show them in their name's alphabetical order? 

Normally when I display m2m in a tree, i can use default_order="name desc" but in here

<field name="categ_ids" widget="many2many_checkboxes" default_order="name desc"/>

does nothing.

Avatar
Descartar
Mejor respuesta

Hello Anti Nuga,

     You have to replace some line of code(template) in xml file..

here is path: addons >> web >> static >> src >> xml >> base.xml


<t t-name="FieldMany2ManyCheckBoxes">
    <t t-set="total_rec_value" t-value="0"/>
    <t t-set="rec_value" t-value="[]"/>
    <t t-foreach="widget.get('records')" t-as="record">
        <t t-set="rec_value[total_rec_value]" t-value="widget.get('records')[total_rec_value][1]"/>
        <t t-set="total_rec_value" t-value="total_rec_value + 1"/>
    </t>
    <t t-set="rec_value" t-value="rec_value.sort()"/>
    <t t-foreach="rec_value" t-as="rv">
        <t t-foreach="widget.get('records')" t-as="record">
            <t t-if="record[1] == rv">
                <div>
                    <label style="font-weight: normal">
                        <input type="checkbox" t-att-data-record-id="JSON.stringify(record[0])"
                               t-att-checked="selected[record[0]] or null"/>
                        <t t-esc="record[1]"/>
                    </label>
                </div>
            </t>
        </t>
    </t>
</t>


Hope this will help you, Thanks!

Avatar
Descartar
Autor

Works fine, only thing I noticed that it will sort stuff with capital first letter first and then the words with lower case first letter. e.g. 'asd', 'Foo','A' goes to 'A','Foo','asd'. This is just an observation, otherwise it works fine. Thanks!

Hi hardik, i dont want to show in ascending or descending but i want to show in order what we selected. Can u please help?

Publicaciones relacionadas Respuestas Vistas Actividad
1
jun 22
5890
2
ago 20
10723
2
sept 17
9942
1
abr 17
19334
1
mar 15
11592