0

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!

Augustin R
  • 7,089
  • 3
  • 26
  • 54
Fabio Romero
  • 27
  • 2
  • 9

1 Answers1

2

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>
Augustin R
  • 7,089
  • 3
  • 26
  • 54
Kraken
  • 1,905
  • 12
  • 22
  • Kraken thank u for your answer, your code would go in en comparator of ngx-datatable ([comparate]="comparatorFunction"), I understood that I have to compare and to sort manually each row like your example. – Fabio Romero Oct 06 '18 at 19:05