0

I have a date column in this format dd/mm/yyyy for bootstrap datatable. I try to sort it had issues. So I tried to insert this columndef. The results is same the sorting is not correct still.

var table = $('#listTable').DataTable({
        responsive:true,

        "columnDefs": [
         { type: 'date-euro', targets: 0 }
        ]
    });
5
  • 1
    please follow the steps which is in this link https://datatables.net/plug-ins/sorting/date-eu Jun 1, 2017 at 6:22
  • So I must include that extra plugin I have read it not so clear can you help me describe? Jun 1, 2017 at 6:43
  • If you see the link it gives this codes columnDefs: [ { type: 'date-eu', targets: 0 } ] so this what I followed accordingly? Jun 1, 2017 at 6:44
  • yes , you have to add that extra plugin. check this link https://stackoverflow.com/questions/2862322/how-to-sort-by-date-with-datatables-jquery-plugin Jun 1, 2017 at 6:44
  • you can mark my answer as accepted. Jun 1, 2017 at 6:53

1 Answer 1

1

You have to add this plugin for this:

Refer this link : click here

Add this code:

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
    "date-eu-pre": function ( date ) {
        date = date.replace(" ", "");

        if ( ! date ) {
            return 0;
        }

        var year;
        var eu_date = date.split(/[\.\-\/]/);

        /*year (optional)*/
        if ( eu_date[2] ) {
            year = eu_date[2];
        }
        else {
            year = 0;
        }

        /*month*/
        var month = eu_date[1];
        if ( month.length == 1 ) {
            month = 0+month;
        }

        /*day*/
        var day = eu_date[0];
        if ( day.length == 1 ) {
            day = 0+day;
        }

        return (year + month + day) * 1;
    },

    "date-eu-asc": function ( a, b ) {
        return ((a < b) ? -1 : ((a > b) ? 1 : 0));
    },

    "date-eu-desc": function ( a, b ) {
        return ((a < b) ? 1 : ((a > b) ? -1 : 0));
    }
} );

this is for datatable define:

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

you can also find your answer from this stack overflow link:click here

4
  • I have added those codes and saved as a date-eu.js same folder as where my jquery.dataTables.min.js and dataTables.bootstrap.min.js is. But it doesnt seem to be working properly. Jun 1, 2017 at 17:04
  • are you add date-eu.js file in your page? Jun 2, 2017 at 5:25
  • you had check that stackoverflow refresh link ? Jun 2, 2017 at 5:27
  • Yes I have added the date-eu.js in my page too. I have checked and followed the link too Jun 4, 2017 at 10:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.