Question

Can someone help me to sort the store based on a user defined criteria?

Ext.define('Role', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'role_id', type: 'int'},
        {name: 'role_name',  type: 'string'}
    ]
})

The role_name returned are admin,user,read-only,super I would like to sort it as

read-only,user,admin,super.

Était-ce utile?

La solution

You can sort it using the sort() method as follow:

Ext.define('Role', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'role_id', type: 'int'},
        {name: 'role_name',  type: 'string'}
    ]
})

var store = Ext.create('Ext.data.Store', {
    model: 'Role',    
    data: [             // non-sorted data
        [ 1, 'user' ],
        [ 2, 'super' ],
        [ 3, 'read-only' ],
        [ 4, 'admin' ]                
    ]
});

// sort the store
store.sort([{
    sorterFn: function(v1, v2) {
        var order = ['read-only', 'user', 'admin', 'super'],
            v1o   = order.indexOf(v1.get('role_name')),
            v2o   = order.indexOf(v2.get('role_name'));           

        return v1o < v2o ? -1 : 1;; 
    }
}]);

console.log(store.data);

​You can see it working here: http://jsfiddle.net/lontivero/wGc2D/4/

Good luck!

Autres conseils

This is covered in the documentation, use a custom sort type:

http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Field-cfg-sortType

// current sort     after sort we want
// +-+------+          +-+------+
// |1|First |          |1|First |
// |2|Last  |          |3|Second|
// |3|Second|          |2|Last  |
// +-+------+          +-+------+

sortType: function(value) {
   switch (value.toLowerCase()) // native toLowerCase():
   {
      case 'first': return 1;
      case 'second': return 2;
      default: return 3;
   }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top