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?
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?