Antt
Antt

Reputation: 159

angular-datatables not sorting when formatting date

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

Upvotes: 3

Views: 3526

Answers (2)

Antt
Antt

Reputation: 159

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.

Upvotes: 2

davidkonrad
davidkonrad

Reputation: 85538

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

Upvotes: 3

Related Questions