8
votes

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" coming 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.

1

1 Answers

5
votes

Got 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 comes to dataTables and Plunker)