3

I have an issue in sorting date with angular-datables plugins when I want to format them. Let me explain : When I'm using

<td>{{date}}</td>

The result is :

enter image description here

When I'm using :

{{date | date}}

The result is :

enter image description here

But when I'm using :

{{date | date : 'dd/MM/yyyy'}}

The result is :

enter image description here

Even so I declared it as a date format :

_this.dtColumnDefs = [  
    DTColumnDefBuilder.newColumnDef(0).
    .withOption('type', 'date')
];

Any ideas what i'm doing wrong ? Thanks

3

We are a rare species, those of us using dd/MM/YYYY format :) The default date type is only functional on "valid" date formats, i.e. strings that can be evaluated with Date.parse(). European dd/MM/YYYY is not one of those. Before mingling with momentjs and such I think you could solve this very easy by a custom sorting plugin :

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
 "Antt-date-pre": function ( a ) {
    if (a == null || a == '') {
      return 0;
    }
    var date = a.split('/');
    return Date.parse(date[1] + '/' + date[0] + '/' + date[2])
  }
});

Usage

DTColumnDefBuilder.newColumnDef(0).
  .withOption('type', 'Antt-date')

small demo -> http://plnkr.co/edit/00vQcoeitZlrQkprN58t?p=preview

| improve this answer | |
  • I'm trying to copy your code but using it with : this.dtColumnDefs = [ DTColumnDefBuilder.newColumnDef(0) .withOption('type', 'date-euro') ]; But I get no call to the jQuery code ... (note that when I replace withOptions with noSortable(), the sort is not working as expected) – Antt Nov 28 '16 at 16:33
  • @Antt You are using 'date-euro'? If you have renamed 'Antt-date' to 'date-euro' it really should work (as in the sample). I do exactly this myself in several apps, just with another naming of course. Sry, dont quite understand the comment about notSortable(), if you want the column to be not sortable, but still want to pre sort on that column, you can use .withOption('order', [[<index>, 'asc']]) or similar in your dt-options plnkr.co/edit/Wo2gtv9OAPWEZ2Fkem6u?p=preview – davidkonrad Nov 28 '16 at 18:25
  • Yes sorry I use 'date-euro' I double check :/ the 'notSortable' thingy was to make sur my dtColumnDefs was called which seems to be the case ... – Antt Nov 29 '16 at 8:33
1

Finally I used something similar from your answer provided directly from DataTables : https://datatables.net/plug-ins/sorting/date-eu

The plugin is deprecated but is working well for what I want.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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