I need to sort a column by weekdays (mon, tue, wed, thu, fri, sat, sun) and cannot seem to get this working. Note that I am using the latest 1.10 version of datatables.
This is located along with other extensions in its own file and called after jquery.dataTables.js is loaded, but before the table initialization.
/* 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));
}
} );
then in my table initialization I specify the sort for this particular column. Values can/will only be "SUN","MON","TUE","WED","THU","FRI","SAT" ing from the database.
"columns": [
..... some column entries,
{
"data": "day",
"type": "weekday"
},
..... the rest of the column entries
No errors in the console, however, sorting just defaults to the regular alphabetical sorting when I sort by clicking on the column title.
I need to sort a column by weekdays (mon, tue, wed, thu, fri, sat, sun) and cannot seem to get this working. Note that I am using the latest 1.10 version of datatables.
This is located along with other extensions in its own file and called after jquery.dataTables.js is loaded, but before the table initialization.
/* 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));
}
} );
then in my table initialization I specify the sort for this particular column. Values can/will only be "SUN","MON","TUE","WED","THU","FRI","SAT" ing from the database.
"columns": [
..... some column entries,
{
"data": "day",
"type": "weekday"
},
..... the rest of the column entries
No errors in the console, however, sorting just defaults to the regular alphabetical sorting when I sort by clicking on the column title.
Share Improve this question edited Feb 27, 2014 at 2:40 user756659 asked Feb 27, 2014 at 2:17 user756659user756659 3,51213 gold badges59 silver badges118 bronze badges1 Answer
Reset to default 6Got this working with dataTables 1.10.0-beta.2:
$(function() {
$('#datatable').DataTable({
"oLanguage": {
"sSearch": "Filter Data"
},
"iDisplayLength": -1,
"sPaginationType": "full_numbers",
"aoColumns": [{
"sType": "weekday"
},null]
});
});
Note that i just defined a type in aoColumns. The actual sorting is still done by your code.
Look at this Plunk and tell me if this is what you wanted. (Tested on Chrome cuz FF is a little bit picky when it es to dataTables and Plunker)