1.10 custom sort by days of the week

1.10 custom sort by days of the week

mihomesmihomes Posts: 165Questions: 23Answers: 0
edited February 2014 in General
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]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited February 2014
    I really thought there was a plug-in for this already, but can't find it!

    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
  • mihomesmihomes Posts: 165Questions: 23Answers: 0
    edited February 2014
    Okay, that makes sense... I edited, but still can't seem to get this going... is this the correct optioning to apply the custom sort to a column then? I want to add that the values coming from the database will always be caps as noted in the array below so there is no confusion on that part.

    [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]
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Yup - that looks correct. I'm afraid I'd need a link to the page to know why it isn't working.

    Allan
  • mihomesmihomes Posts: 165Questions: 23Answers: 0
    edited February 2014
    Alright so I just realized any custom sorting will not work as I am using serverside processing. Keep forgetting there are limitations with that usage. Unless anyone has any ideas I believe I will change the days to digits in my database.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Ah - yes. That would do it.

    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
  • mihomesmihomes Posts: 165Questions: 23Answers: 0
    hahaha... can't believe I didn't realize that till then. Well, its literally the abbreviated weekday stored in the database as a string. I think my best bet is to change that from the abbreviation to its corresponding digit same as above. I can either format the column in php or render it in the js to show the actual day in the table based off that digit value then.

    Sorry for the trouble. At least there is an example now for this though haha.
  • mihomesmihomes Posts: 165Questions: 23Answers: 0
    Not sure if this is correct or not and I just woke up so a little groggy from no sleep. Not the best way to go about it, but would this work :

    - 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.
This discussion has been closed.