PDA

View Full Version : sortInfo Sort by multiple fields



ValterBorges
20 Jul 2009, 11:52 AM
Is there a way to sort by multiple fields. For example if I have an editable grid with travel destination, from, and to dates where the entire record can be the same including from and only to differs. I would like to sort by destination, from, and to.

Here is an example how the data should be sorted.

Florida, 1/1/2009, 1/10/2009
Florida, 3/1/2009, 4/1/2009
Florida, 3/1/2009, 5/1/2009
Las Vegas, 5/1/2009, 6/1/2009

pmatsumura
20 Jul 2009, 11:57 AM
I'm not quite sure if Ext can handle this, but I would try sorting it on the server side. Let the database do the hard work ;)

ValterBorges
20 Jul 2009, 1:40 PM
Sorry, my fault for not specifying, the grid will be part of a form to submit data and the data is not yet in the database. It will only be applied to the database once they enter it and press save or via an ajax call.

Condor
21 Jul 2009, 3:31 AM
It depends.

If you click on the 'from' column, would the sort order depend on the previous sort or would the sort order by fixed ('from', 'destination', 'to')?

For the first one you would have to modify the internal Store code.

For the second one you would specify your own sortType function for every field to do custom sorting.

ValterBorges
21 Jul 2009, 7:26 AM
The second one is what i'm trying to do. Any examples to look at?

Condor
21 Jul 2009, 9:35 AM
I forgot, you still need to patch Store (but it's part of my default overrides):

Ext.override(Ext.data.Store, {
sortData : function(f, direction){
direction = direction || 'ASC';
var st = this.fields.get(f).sortType;
var fn = function(r1, r2){
var v1 = st(r1.data[f], r1), v2 = st(r2.data[f], r2);
return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
};
this.data.sort(direction, fn);
if(this.snapshot && this.snapshot != this.data){
this.snapshot.sort(direction, fn);
}
}
}):

After including this override you can use:

fields: [
{name: 'destination', sortType: function(v, r){
return String.leftPad(r.get('destination'), 20).substr(0, 20) +
r.get('from').dateFormat('YmdHis') +
r.get('to').dateFormat('YmdHis');
}},
{name: 'from', sortType: function(v, r){
return r.get('from').dateFormat('YmdHis') +
String.leftPad(r.get('destination'), 20).substr(0, 20) +
r.get('to').dateFormat('YmdHis');
}},
{name: 'to', sortType: function(v, r){
return r.get('to').dateFormat('YmdHis');
String.leftPad(r.get('destination'), 20).substr(0, 20) +
r.get('from').dateFormat('YmdHis') +
}}
]

ValterBorges
22 Jul 2009, 7:03 AM
awesome, thanks.

ironlion
18 Feb 2010, 10:54 PM
Cool, I just find this solution. You saved my day. It works great. Thanks!