I just cannot get any sorting plugins to work at all.

I just cannot get any sorting plugins to work at all.

MigMig Posts: 2Questions: 0Answers: 0
edited July 2012 in General
I am trying to sort a dd/mm/yyyy date column, which I can see is a popular thing. Nothing works. What am I doing wrong?

[code]
/**
* This is the plugin code I found somewhere. Will move to a new file when I see it works.
*/
$.fn.dataTableExt.oSort['date-asc'] = function(a,b) {
var x = Date.parse(a);
var y = Date.parse(b);
if (x == y) { return 0; }
if (isNaN(x) || x < y) { return 1; }
if (isNaN(y) || x > y) { return -1; }
};
$.fn.dataTableExt.oSort['date-desc'] = function(a,b) {
var x = Date.parse(a);
var y = Date.parse(b);
if (x == y) { return 0; }
if (isNaN(y) || x < y) { return -1; }
if (isNaN(x) || x > y) { return 1; }
};

$(document).ready(function() {

/**
* Set up data table
*/
$('#orders table').dataTable({
'oLanguage': eval(window.app.DATA_TABLES_CONFIG),
'sPaginationType': 'full_numbers',
'aaSorting': [[0, 'desc']],
'aoColumns': [null, null, null, {'sType': 'date'}]
});

});[/code]

Replies

  • allanallan Posts: 63,538Questions: 1Answers: 10,476 Site admin
    DataTables will automatically detect columns that can be sorted using Date.parse, so that should is not needed. However, since it isn't working, I guess you have non-date data in the the column, or it is in a format that can't be parsed by Date. Can you run your table through the DataTables debugger please?

    Allan
  • MigMig Posts: 2Questions: 0Answers: 0
    edited July 2012
    Can DataTables automatically sort dd/mm/yyyy though? I didn't think it could.

    Edit: It's OK. I got it to work. I think the problem was that my plugin code was wrapped in

    [code](function ($) {

    // ...

    }(jQuery));[/code]
  • allanallan Posts: 63,538Questions: 1Answers: 10,476 Site admin
    > Can DataTables automatically sort dd/mm/yyyy though? I didn't think it could.

    No, because Date.parse expects the American MM/DD/YYYY format...

    You want this plug-in: http://datatables.net/plug-ins/sorting#date_uk

    Allan
This discussion has been closed.