PDA

View Full Version : Sorting the Grid



Animal
14 Sep 2006, 5:04 AM
I've implemented the sort() method in my AspicioDataModel.

It re-issues the query on the server, and grabs the first block of rows back as an Object[][] using a DWR remote call.

It then fires fireRowsDeleted(firstBlockSize, oldSize - 1) to clear down the rows after the new, smaller data block, and fireRowsUpdated(0, firstBlockSize - 1) to update the view of the datablock.

The Arrow indicators do not appear. Looking at the DOM inspector the two spans are still display:none

I've tried adding fireRowsSorted(0, firstBlockSize - 1), but that doesn't seem to do anything.

What can I do to force the Grid to set the column's sort status?

BTW, first click on a column sorts DESC. Wouldn't it be more intuitive to have the initial sort on a column use ASC?

rodiniz
14 Sep 2006, 5:45 AM
You can see my implementation of sort on
http://www.rodrigodiniz.qsh.eu/YahooGrid.aspx
I think I have done a much more simple logic.
My implementation also has paging.

jack.slocum
14 Sep 2006, 11:57 AM
In the current version there are two undocumented variables the GridView will look for on your data model to determine it's sort state:

var sortColumn = this.grid.dataModel.sortColumn;
var sortDir = this.grid.dataModel.sortDir;

If you set those two when you do your sort and then call fireRowsSorted, it should correct your problem.

While working on remote datasets I realized I needed to improve the interface between the data model and the view in regards to sorting. In the dev version I have added a method to the AbstractDataModel interface getSortState() so the view is not accessing undocumented public variables.

It looks like this:


/**
* Interface method to supply the view with info regarding the Grid's current sort state - if overridden,
* this should return an object like this {column: this.sortColumn, direction: this.sortDir}.
* @return {Object}
*/
getSortState : function(){
return {column: this.sortColumn, direction: this.sortDir};
}


Second, fireRowsSorted causes the grid to refresh the view (re-render the rows). Since you have already reloaded the data, that refresh isn't necessary. There's was no way to notify the view of a sort change without the view refreshing the data. To fix that problem I've added a new argument on the end of fireRowsSorted:



fireRowsSorted : function(sortColumnIndex, sortDir, noRefresh)


If you pass true for noRefresh, the view will skip the re-rendering of the rows. That's what you want in your model. That functionality will be in the coming release.

Jack

kovtik
2 Oct 2006, 8:24 AM
I have a grid with 2 columns: FolderItem and Type. I wand to sort FolderItem in windows explorer style (it means that during sorting by FolderItem files and folders remain grouped by Type). I don't know how to do this. Have you any suggestion?

kovtik
2 Oct 2006, 8:44 AM
Sorting must be done on client-side. The problem is that I need to write a sorting function which can access data from others columns (not only sorted column) of the rows. I don't see the solution how I can attach such sorting function to any ColumnModel or DataModel class.

jack.slocum
2 Oct 2006, 9:01 AM
You can override the sort method on your data model and do something like this:



yourDM.sort = function(columnModel, columnIndex, direction, suppressEvent){
if(columnIndex == 2){ // your column index for folder/file compare
var fn = function(cells1, cells2){
// compare the two , cells1 and cell2 have all the cell data
}
this.data.sort(fn);
if(!suppressEvent){
this.fireRowsSorted(columnIndex, direction);
}
} else {
YAHOO.ext.DefaultDataModel.sort.apply(this, arguments);
}
};

kovtik
3 Oct 2006, 2:53 AM
You can override the sort method on your data model and do something like this...Please look at my code:

dataModel.sort = function(columnModel, columnIndex, direction, suppressEvent){
if(columnIndex == 0){
var sortType = columnModel.getSortType(columnIndex);
var fn = function(cell1, cell2){
var name1 = sortType(cell1[0]);
var name2 = sortType(cell2[0]);
var type1 = sortType(cell1[1]);
var type2 = sortType(cell2[1]);

if (type1 == "Folder" && type2 != "Folder")
return 1;
if (type1 != "Folder" && type2 == "Folder")
return -1;
if (name1 < name2)
return -1;
if (name1 > name2)
return 1;
return 0;
}
this.data.sort(fn);
if (direction == "DESC")
{
this.data.reverse();
}

if(!suppressEvent){
this.fireRowsSorted(columnIndex, direction);
}
} else {
YAHOO.ext.grid.DefaultDataModel.prototype.sort.apply(this, arguments);
}
};I have the following problems:
1) sorting of "name" column works only during the first call to dataModel.sort. All others calls to dataModel.sort with columnIndex=0 contains direction='ASC'.
2) a small arrow showing the direction of sorting does not appear after clicking on the header of the "name" column of the grid
3) could you explain me what is the "arguments" parameter of the YAHOO.ext.grid.DefaultDataModel.prototype.sort.apply(this, arguments); call? I don't know where this parameter is declared, but this code seems to work fine.[/code]

jack.slocum
3 Oct 2006, 3:20 AM
"arguments" is available in every function call in JS. It is a psuedo array of the arguments the function was called with.

Add these three lines to the top of your sort method and it should correct your problems:

this.columnModel = columnModel;
this.sortColumn = columnIndex;
this.sortDir = direction;

kovtik
3 Oct 2006, 3:31 AM
"arguments" is available in every function call in JS. It is a psuedo array of the arguments the function was called with.

Add these three lines to the top of your sort method and it should correct your problems:

this.columnModel = columnModel;
this.sortColumn = columnIndex;
this.sortDir = direction;Excuse my poor knowledge of javascript. You saved me alot of time. Thank you.

jack.slocum
3 Oct 2006, 3:47 AM
Glad I could help, Jack.

Animal
3 Oct 2006, 11:17 PM
Can we send some kind of virus to this f***-head 5wr0gx's computer?

I've pm'd him inviting him to "go away", but to no avail.

jack.slocum
4 Oct 2006, 1:11 AM
It has to be a bot. It comes back with posts every day. I tried banning the user but he/it just made a new one and started again.

Animal
4 Oct 2006, 3:38 AM
Perhaps forum2 will contain anti-bot features :?: :?:

Maybe if the same message is posted >n times in different folders, the account is deactivated and deleted.

jack.slocum
4 Oct 2006, 3:46 AM
forum2 should be bot proof since it requires ajax login and will use a YUI dialog to post messages. :) At least that's what I'm hoping!