Change index of options in a select field?

Hi there guys, in a doctype I have a select field which has 10 options, when I log the indexes of the options instead of having the sequence from 0 to 9 (0,1,2,3,4,5,6,7,8,9) it has another sequence something like 0,4,9,14… and so on.
Is there any way possible for me to modify/edit the index of those options so that they have the sequence from 0 to 9?

Thank you in advance!

To sort the Select options in ERPNext select field using client script, you can use the following code:

frappe.ui.form.on('Your Doctype', {
    refresh: function(frm) {
        var select_field = frm.fields_dict['your_select_field'];
        select_field.$input.find('option').sort(function(x, y) {
            return $(x).text() > $(y).text() ? 1 : -1;
        }).appendTo(select_field.$input);
    }
});

Replace ‘Your Doctype’ with the name of your doctype and ‘your_select_field’ with the name of your select field. This code will sort the options in ascending order.

You can also sort the options in descending order by changing the comparison operator from ‘>’ to ‘<’ in the sort function.

1 Like