PDA

View Full Version : Custom Sort Order for a grid column



bitdifferent
6 Sep 2007, 8:18 AM
If I want a column in a grid to be sorted under a differrent basis than just the value of the cells in that grid, what is the best way to achieve that?

eg I have a column whose cells contain a string, but when sorted I don't want it to be alphabetically, like the names of the days of the week.

The way that I do this now, as I've just explained to a guy on IRC, is to put a numeric value in the cell that's correctly ordered, and then in the custom renderer for the cell, look at the value of some other, hidden, columns in the same row.

Is there a better way?

Here's the code which I just pasted up for the guy I was helping:



//contrived example to show grid sorting on a different value, sort of...

// in my column model:
[
{header: "Day", width: 70, dataIndex: 'intDayNum', sortable:true, renderer: fnDOTWRenderer},
{header: "DayName", hidden: true, dataIndex: 'strDayName'},
{header: "Food", hidden: true, dataIndex: 'strFoodName'}
{header: "FoodCount", hidden: true, dataIndex: 'intFoodCount'}

]

//The 'day' column actually contains a number, 1-7 for example
//The 'DayName' column is *hidden*. It contains the 'Monday', 'Tuesday' or whatever

//here is the custom renderer for the Day column:

function fnDOTWRenderer(value, metadata, record,rowIndex,colIndex,store)
{
//normally a renderer reads and manipulates 'value'
//AND OF COURSE it would be feasible to lookup our value here in an array of Day names.
//this is just a contrived example!

strDayName = record.data.strDayName;
intFoodCount = record.data.intFoodCount;
strFoodName = record.data.strFoodName;

return "On "+strDayName+" he ate through "+intFoodCount+" "+strFoodName+"s, but he was STILL hungry!";
}


//end result is a column which displays something like

// "On Tuesday he ate through 3 watermelons, but he was STILL hungry"

//but sorts on the day of the week NUMBER not alphabetically....




Any thoughts?

Animal
6 Sep 2007, 10:17 AM
Ext.data.SortTypes.mySpecialSortType = fnDOTWRenderer;


See sort Type:

http://extjs.com/deploy/ext/docs/output/Ext.data.Record.html#create

You can add your own which convert into sortable values.

In your example, the sortable value is the displayable value, so it's the same function as the renderer.

YMMV. Let us know how you get on...

Animal
6 Sep 2007, 10:18 AM
Oops! Of course you won't have access to the Record when it's called as a sort type function!

Animal
6 Sep 2007, 10:19 AM
I think an enhancement request is called for so that the Store does pass the whole record to the sort function as the second param, so that you can examine other columns.

bitdifferent
6 Sep 2007, 10:15 PM
Thanks Animal, I hadn't noticed the sort types thing but I kind of intuitively expected it to be there, if you see what I mean! I'll have a play with that, thanks.

SoggyBottom
22 Jan 2008, 4:40 PM
This is exactly what I need. I have my custom sort function but I need access to the other fields of the record which I currently don't have.


I think an enhancement request is called for so that the Store does pass the whole record to the sort function as the second param, so that you can examine other columns.