PDA

View Full Version : [OPEN-1449] Problem with sorting column with undefined values in grid.



ybubnov
8 Dec 2010, 12:12 PM
I recently found problem with grid/store: I'm receiving data from server and some fields could be null. Then when I tried to sort grid by the fields, it was displaying in some weired way.
So I dug in the source code and found out that Store does not handle 'undefined' values.
Here is my version of the sort function:

Ext.override(Ext.data.Store,{createSortFunction:function(field, direction) {
direction = direction || "ASC";
var directionModifier = direction.toUpperCase() == "DESC" ? -1 : 1;

var sortType = this.fields.get(field).sortType;

//create a comparison function. Takes 2 records, returns 1 if record 1 is greater,
//-1 if record 2 is greater or 0 if they are equal
return function(r1, r2) {
var v1 = sortType(r1.data[field]),
v2 = sortType(r2.data[field]);
if (v1 && !v2){
return directionModifier*1;
}

if (!v1 && v2){
return directionModifier*-1;
}
return directionModifier * (v1 > v2 ? 1 : (v1 < v2 ? -1 : 0));
};
}});