9

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 Answer 1

6

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Actually, there was nothing wrong with my code... what I failed to realize was custom sorting will never work when using a serverside data source lol...
@user756659 just read that comment after struggling for half an hour. So obvious... thanks buddy
Lol. Yeah, now that I look back at it I can laugh because it is obvious, but when you are first starting out using DataTables it can easily slip by your thought process - there is a lot to take in when you start looking at all the options and its operation. If you are using pdo and serverside you might be interested in my modified ssp class. Send me a message and I can send it over.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.