Ext JS 4 – Custom Sorting

I actually never needed to do this in Ext JS 3, so I have no idea if it has changed. But, for my current project, I have a grid where the data behind one of the columns is an object instead of a string or a number. Guess what, it doesn’t know how to sort itself.

Turns out that you just need to define a sortType and the Ext 4 docs are very helpful:   Ext.data.SortTypes
And copied here:

Ext.apply(Ext.data.SortTypes, {
    asPerson: function(person){
        // expects an object with a first and last name property
        return person.lastName.toUpperCase() + person.firstName.toLowerCase();
    }    
});

Ext.define('Employee', {
    extend: 'Ext.data.Model',
    fields: [{
        name: 'person',
        sortType: 'asPerson'
    }, {
        name: 'salary',
        type: 'float' // sortType set to asFloat
    }]
});

As a matter a fact, I didn’t realize those default sorters existed…

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s