Sort a String characteristc in combobox ExtJs

39 views Asked by At

I hava the follow situation
in software the characteristcs of a store combobox are defined by client as String, but the sort of combobox is:

A 1 A 11 A 2 A 21 A 3 ...

but my client needs a sort like that bellow A 1 A 2 A 3 A 11 A 21 ...

How could I get this information from the front threat and render sorted correctly?

1

There are 1 answers

1
shae On

you need to use sorter function and update the logic as per your usecase like below,

Ext.application({
    name: 'YourApp',
    launch: function () {

        var Store = Ext.create('Ext.data.Store', {
            data: [
                { sampledata: 'A 1' },
                { sampledata: 'A 11' },
                { sampledata: 'A 2' },
                { sampledata: 'A 21' },
                { sampledata: 'A 3' }
            ],
            sorters: [{
                sorterFn: function(item1, item2) {
                    var pattern = /([A-Za-z]+) (\d+)/;
                    var match1 = item1.get('sampledata').match(pattern);
                    var match2 = item2.get('sampledata').match(pattern);
                    var groupComparison = match1[1].localeCompare(match2[1]);
                    if (groupComparison !== 0) {
                        return groupComparison;
                    }
                    var num1 = parseInt(match1[2], 10);
                    var num2 = parseInt(match2[2], 10);
                    return num1 - num2;
                }
            }]
        });

        Ext.create('Ext.form.ComboBox', {
            store: Store,
            displayField: 'sampledata',
            fieldLabel: 'ComboBox',
            renderTo: Ext.getBody()
        });
    }
});