Unable to use datatables date sorter jquery plugin

Go To StackoverFlow.com

1

Below code is taken from : http://datatables.net/forums/discussion/2467/need-help-for-sorting-date-with-ddmmyyyy-format/p1

Here is how I'm invoking the functionality :

$('#tab').dataTable({
      'aoColumns': [null, null, null, null, { "sType": "date-euro" }]

});

I've added below code to a seperate .js file. How do I invoke the sort functionality ?

function trim(str) {
    str = str.replace(/^\s+/, '');
    for (var i = str.length - 1; i >= 0; i--) {
        if (/\S/.test(str.charAt(i))) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    return str;
}

(function( $ ) {
      $.fn.dataTableExt.oSort['date-euro-asc'] = function(a, b) {
    if (trim(a) != '') {
        var frDatea = trim(a).split(' ');
        var frTimea = frDatea[1].split(':');
        var frDatea2 = frDatea[0].split('/');
        var x = (frDatea2[2] + frDatea2[1] + frDatea2[0] + frTimea[0] + frTimea[1] + frTimea[2]) * 1;
    } else {
        var x = 10000000000000; // = l'an 1000 ...
    }

    if (trim(b) != '') {
        var frDateb = trim(b).split(' ');
        var frTimeb = frDateb[1].split(':');
        frDateb = frDateb[0].split('/');
        var y = (frDateb[2] + frDateb[1] + frDateb[0] + frTimeb[0] + frTimeb[1] + frTimeb[2]) * 1;                      
    } else {
        var y = 10000000000000;                     
    }
    var z = ((x < y) ? -1 : ((x > y) ? 1 : 0));
    return z;
};
})( jQuery );

(function( $ ) {
      $.fn.dataTableExt.oSort['date-euro-desc'] = function(a, b) {
    if (trim(a) != '') {
        var frDatea = trim(a).split(' ');
        var frTimea = frDatea[1].split(':');
        var frDatea2 = frDatea[0].split('/');
        var x = (frDatea2[2] + frDatea2[1] + frDatea2[0] + frTimea[0] + frTimea[1] + frTimea[2]) * 1;                       
    } else {
        var x = 10000000000000;                     
    }

    if (trim(b) != '') {
        var frDateb = trim(b).split(' ');
        var frTimeb = frDateb[1].split(':');
        frDateb = frDateb[0].split('/');
        var y = (frDateb[2] + frDateb[1] + frDateb[0] + frTimeb[0] + frTimeb[1] + frTimeb[2]) * 1;                      
    } else {
        var y = 10000000000000;                     
    }                   
    var z = ((x < y) ? 1 : ((x > y) ? -1 : 0));                 
    return z;
}; 
})( jQuery );
2012-04-05 20:27
by user701254
Is that your actual code? You're missing a ' here: $('#tab).dataTable({ ... });. Also, are you sure that jQuery is being loaded before you initialize dataTables - Colin Brock 2012-04-05 20:46
I've added the missing ' to question, I removed it accidentally when posting the question. jQuery is being loaded before I initialize dataTables, other dataTables functionality is working as expected - user701254 2012-04-05 20:50


0

If you have a table with four columns you should use

$('#tab').dataTable({
      'aoColumns': [null, null, null, { "sType": "date-euro" }]
});
2012-04-06 08:48
by Nicola Peluchetti
I have a table with five columns, the date is being added to the fifth column - user701254 2012-04-06 19:01
Ads