Skip to Content
Menu
This question has been flagged
1 Reply
9703 Views

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
Discard
Best Answer

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
Discard
Author

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?

Related Posts Replies Views Activity
1
Jun 22
5885
2
Aug 20
10723
2
Sep 17
9937
1
Apr 17
19328
1
Mar 15
11585