1.10 custom sort by days of the week
1.10 custom sort by days of the week
I have come across a situation where I need a column sorted by days of the week... mon, tue, wed, thu, etc...
I wrote up a quick js real quick to do this, however, I am a little confused as to how to go about adding this custom sort into datatables. I have looked at the examples via http://next.datatables.net/plug-ins/sorting/ ... what exactly is the 'pre' func for?
[code]
function sort_days(days) {
var list = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
return days.sort(function(a,b) { return list.indexOf(a) > list.indexOf(b); });
}
var test = ["Tue","Sat","Mon","Thu"];
var output = sort_days(test);
document.write (output);
[/code]
Here is what I attempted :
[code]
/* custom day sorting */
$.extend( $.fn.dataTableExt.oSort, {
//define the week ordering and naming
var list = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
"days-pre": function ( a ) {
return a;
},
"days-asc": function ( a, b ) {
return list.indexOf(a) > list.indexOf(b);
},
"days-desc": function ( a, b ) {
return list.indexOf(a) < list.indexOf(b);
}
} );
[/code]
I wrote up a quick js real quick to do this, however, I am a little confused as to how to go about adding this custom sort into datatables. I have looked at the examples via http://next.datatables.net/plug-ins/sorting/ ... what exactly is the 'pre' func for?
[code]
function sort_days(days) {
var list = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
return days.sort(function(a,b) { return list.indexOf(a) > list.indexOf(b); });
}
var test = ["Tue","Sat","Mon","Thu"];
var output = sort_days(test);
document.write (output);
[/code]
Here is what I attempted :
[code]
/* custom day sorting */
$.extend( $.fn.dataTableExt.oSort, {
//define the week ordering and naming
var list = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
"days-pre": function ( a ) {
return a;
},
"days-asc": function ( a, b ) {
return list.indexOf(a) > list.indexOf(b);
},
"days-desc": function ( a, b ) {
return list.indexOf(a) < list.indexOf(b);
}
} );
[/code]
This discussion has been closed.
Replies
The `pre` function is a pre formatter. Its just an optimisation so you can have complex calculations done only once and the sort itself can happen on the calculated data as fast as possible. Documentation for it is still to come...
Simply return the value that you want to be sorted from your pre - so Sun=0, Mon=1 etc. In fact, something like:
[code]
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"weekday-pre": function ( a ) {
return $.inArray( a, ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"] );
}
} );
[/code]
should do it.
Allan
[code]
/* custom sorting by weekday */
$.extend( $.fn.dataTableExt.oSort, {
"weekday-pre": function ( a ) {
return $.inArray( a, ["SUN","MON","TUE","WED","THU","FRI","SAT"] );
},
"weekday-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"weekday-desc": function ( a, b ) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );
[/code]
then on the column I want to apply :
[code]
"columns": [
{
"data": "day",
"type": "weekday"
},
......
[/code]
Allan
You would need to have the server do the sort in that case. If it is stored as a date formatted item, it should automatically do such a sort, or you could extract the week date from the date stamp and sort on that. But that's probably one for an SQL forum!
Allan
Sorry for the trouble. At least there is an example now for this though haha.
- use the formatter in my serverside script to change from MON to 1
- then in the js use render to change from 1 to MON or whatever I wanted?
I can't remember right now if the formatter stuff is parsed during the serverside stuff or as it comes back if that makes sense.