PDA

View Full Version : Sort order backwards??



tryanDLS
9 Nov 2006, 4:27 PM
Jack,

Is it me, or is the sort on DefaultDataModel going in the opposite direction of what's specified?

jack.slocum
9 Nov 2006, 4:39 PM
It looks like it is. I recently upgraded the sort code (nice upgrade :?) and I must have broken it. I swapped the return values to re-reverse it:


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(typeof sortInfo == 'function'){
sortType = sortInfo;
}else if(typeof sortInfo == 'object'){
sortType = sortInfo.getSortType(columnIndex);;
}
var fn = function(cells, cells2){
var v1 = sortType ? sortType(cells[columnIndex], cells) : cells[columnIndex];
var v2 = sortType ? sortType(cells2[columnIndex], cells2) : cells2[columnIndex];
if(v1 < v2)
return dsc ? 1 : -1;
if(v1 > v2)
return dsc ? -1 : 1;
return 0;
};
this.data.sort(fn);
if(!suppressEvent){
this.fireRowsSorted(columnIndex, direction);
}
};

tryanDLS
9 Nov 2006, 5:03 PM
At least that's an easy one :)

Another ?. The comments for sort() say pass a null as the 1st arg to use the default. When I did



dm.setDefaultSort(cm, 1, 'ASC');
dm.loadData(data);
dm.sort(null, 1, 'ASC');


I got a sortInfo has no properties err. If I pass 'cm' as 1st arg it works OK.

One more ?. Is there a way to get the rowIndex to display as a column when it's not in the data (using JSONDataModel)?

jack.slocum
9 Nov 2006, 5:14 PM
Do me a favor and try passing undefined as the first argument. That I am guessing will work.

jack.slocum
9 Nov 2006, 5:15 PM
Anyway, try this:


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 fn = function(cells, cells2){
var v1 = sortType ? sortType(cells[columnIndex], cells) : cells[columnIndex];
var v2 = sortType ? sortType(cells2[columnIndex], cells2) : cells2[columnIndex];
if(v1 < v2)
return dsc ? +1 : -1;
if(v1 > v2)
return dsc ? -1 : +1;
return 0;
};
this.data.sort(fn);
if(!suppressEvent){
this.fireRowsSorted(columnIndex, direction);
}
};

jack.slocum
9 Nov 2006, 5:17 PM
As for displaying the rowIndex in the first column, what type of data model are you using?

tryanDLS
9 Nov 2006, 5:22 PM
Passing 0 or '' to dm.sort() does work.

For the rowIndex issue, I'm using JSON. In fact, here's a chunk of code, if you want to experiment


init : function(){

var data = {'States':[
{'__type':'State', 'Name':'Alaska', 'Code':'AK','IsContentViewable':true},
{'__type':'State', 'Name':'Arkansas', 'Code':'AR','IsContentViewable':true},
{'__type':'State', 'Name':'California', 'Code':'CA','IsContentViewable':true},
{'__type':'State', 'Name':'Colorado', 'Code':'CO','IsContentViewable':true},
{'__type':'State', 'Name':'Connecticut', 'Code':'CT','IsContentViewable':true},
{'__type':'State', 'Name':'Delaware', 'Code':'DE','IsContentViewable':true}
]};

var dm, cm, sm;

cm = new YAHOO.ext.grid.DefaultColumnModel(
[{header:'', hidden:true},
{header:'Name', width: 150},
{header:'Code', width: 50},
{header:'Content Viewable', width: 125}
]);

cm.defaultSortable = true;

var schema = {
root:'States',
id:'use-index',
fields: ['__type', 'Name', 'Code', 'IsContentViewable']
};

dm = new YAHOO.ext.grid.JSONDataModel(schema);

dm.onLoad.subscribe(this.onLoad.createDelegate(this));
dm.onLoadException.subscribe(this.showError.createDelegate(this));
dm.setDefaultSort(cm, 1, 'ASC');
dm.loadData(data);
dm.sort(0, 2, 'DESC');

sm = new YAHOO.ext.grid.SingleSelectionModel();

this.grid = new YAHOO.ext.grid.Grid('exGrid', dm, cm, sm);
this.grid.render();

jack.slocum
9 Nov 2006, 5:43 PM
The problem with the rowIndex is if you manually insert or add rows it won't be updated automatically. If all you are doing is loading data the normal way, then you could do something like this:

1. Add a bogus column as the first field in your data model. Make sure you give it a name that won't be found such as "row-index".

2. Add a preprocessor to column 0 that looks something like this:

var rowIndex = 0;
dm.addPreprocessor(0, function(){ return ++rowIndex; });
dm.on('beforeload', function(){ rowIndex = 0; });

Obviously you could format these to be more aesthetically pleasing. :)

tryanDLS
10 Nov 2006, 7:27 AM
That does exactly what I was looking for - no inserts needed. Thanks.