Fabio Romero
Fabio Romero

Reputation: 27

Ngx-Datatable sorting by date column doesn't work

I have the next code:

<ngx-datatable
 class="material"
 [rows]="rows" 
 [columnMode]="'force'" 
 [headerHeight]="50" 
 [footerHeight]="50" 
 [sorts]="[{prop: 'name', dir: 'desc'}]"
 [limit]="3">
 <ngx-datatable-column name="Name">
   <ng-template let-row="row" ngx-datatable-cell-template>
    {{row.name}}
    </ng-template>
  </ngx-datatable-column>
  <ngx-datatable-column name="Date">
    <ng-template let-row="row" ngx-datatable-cell-template>
       {{row.date}}
    </ng-template>
  </ngx-datatable-column>
</ngx-datatable>

I need sort by date format ("dd/mm/yyyy") and ("hh:mm:ss dd/mm/yyyy"). I understand that this table is just sorting by string format, but when I sort by date doesn't work correctly.

Someone kind who can help me. Maybe I have to create an specific sorting or comparation. How I should do it?

Thanks!

Upvotes: 0

Views: 5596

Answers (1)

Kraken
Kraken

Reputation: 1955

Ngx-tables can sort by date, but you need to specify that this is date,

Here is small chunk of code that handles Date sorting

if (a instanceof Date && b instanceof Date) {
    if (a < b) return -1;
    if (a > b) return 1;
}

taken from ngx-datatable repository.

You can try to put pipe on your date, so angular will do your work.

<ng-template let-row="row" ngx-datatable-cell-template>
   {{row.date | date}}
</ng-template>

Upvotes: 2

Related Questions