Sort datatable.net jquery by date

Asked

Viewed 332 times

0

I’m ordering the column REQUEST thus:

"order": [[3, "asc"]]

Note that the ordering of the column REQUEST nay is correct:

inserir a descrição da imagem aqui

The ordering of the column REQUEST it must be so:

inserir a descrição da imagem aqui

  • The camp guy must be like String, note that you are only considering the first digits. You should convert to DateTime.

1 answer

2


Depending on the version or the files you use, it does not have the extension to sort by date.

To change, simply add the Datetable date-euro in your project and add the column that should be ordered, example:

$('#example').dataTable( {
     columnDefs: [
       { type: 'date-euro', targets: 0 }
     ]
  });

If you want to add only the code, the extension code is this:

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
    "date-euro-pre": function ( a ) {
        var x;

        if ( $.trim(a) !== '' ) {
            var frDatea = $.trim(a).split(' ');
            var frTimea = (undefined != frDatea[1]) ? frDatea[1].split(':') : [00,00,00];
            var frDatea2 = frDatea[0].split('/');
            x = (frDatea2[2] + frDatea2[1] + frDatea2[0] + frTimea[0] + frTimea[1] + frTimea[2]) * 1;
        }
        else {
            x = Infinity;
        }

        return x;
    },

    "date-euro-asc": function ( a, b ) {
        return a - b;
    },

    "date-euro-desc": function ( a, b ) {
        return b - a;
    }
} );

See an example in Jsfiddle.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.