ericwaldheim
11-28-2006, 11:17 AM
drop-in replacement for DefaultDataModel sort:
YAHOO.ext.grid.DefaultDataModel.prototype.sort = function(sortInfo, columnIndex, direction, suppressEvent){
// store these so we can maintain sorting when we load new data
this.sortInfo = sortInfo;
this.sortColumn = columnIndex;
this.sortDir = direction;
var dsc = (direction && direction.toUpperCase() == 'DESC');
var sortType = null;
if(sortInfo != null){
if(typeof sortInfo == 'function'){
sortType = sortInfo;
}else if(typeof sortInfo == 'object'){
sortType = sortInfo.getSortType(columnIndex);;
}
}
var data = this.data;
var ds = [];
for (var i = 0; i < data.length; ++i)
ds.push([data[i][columnIndex], i]);
var fn = function(d1, d2){
var v1 = sortType ? sortType(d1[0], d1) : d1[0];
var v2 = sortType ? sortType(d2[0], d2) : d2[0];
if (v1 < v2) return dsc ? +1 : -1;
if (v1 > v2) return dsc ? -1 : +1;
if (d1[1] < d2[1]) return dsc ? +1 : -1;
return dsc ? -1 : +1;
};
ds.sort(fn);
var newData = [];
for (var i = 0; i < ds.length; ++i)
newData[i] = data[ds[i][1]];
this.data = newData;
if (!suppressEvent){
this.fireRowsSorted(columnIndex, direction);
}
}
YAHOO.ext.grid.DefaultDataModel.prototype.sort = function(sortInfo, columnIndex, direction, suppressEvent){
// store these so we can maintain sorting when we load new data
this.sortInfo = sortInfo;
this.sortColumn = columnIndex;
this.sortDir = direction;
var dsc = (direction && direction.toUpperCase() == 'DESC');
var sortType = null;
if(sortInfo != null){
if(typeof sortInfo == 'function'){
sortType = sortInfo;
}else if(typeof sortInfo == 'object'){
sortType = sortInfo.getSortType(columnIndex);;
}
}
var data = this.data;
var ds = [];
for (var i = 0; i < data.length; ++i)
ds.push([data[i][columnIndex], i]);
var fn = function(d1, d2){
var v1 = sortType ? sortType(d1[0], d1) : d1[0];
var v2 = sortType ? sortType(d2[0], d2) : d2[0];
if (v1 < v2) return dsc ? +1 : -1;
if (v1 > v2) return dsc ? -1 : +1;
if (d1[1] < d2[1]) return dsc ? +1 : -1;
return dsc ? -1 : +1;
};
ds.sort(fn);
var newData = [];
for (var i = 0; i < ds.length; ++i)
newData[i] = data[ds[i][1]];
this.data = newData;
if (!suppressEvent){
this.fireRowsSorted(columnIndex, direction);
}
}