0
votes

I am working with NGX Datatables, and I have all my data sorting correctly, besides the dates. It seems that NGX's inbuilt sort is sorting from left to right, so it sorts the month/day but not by year. I am trying to sort with this format ('MM-DD-YYYY h:mm A'). If I change to this format ('YYYY-MM-DDh:mm A') it works just fine. I saw someone else said to use the pipe date in angular for dates, so I tried

{{value | date:'MM-DD-YYYY h:mm A'}}

but that also didn't work. It still doesn't sort the years. So it will sort the month and day, but the year will be mixed. Does anyone know a fix for this? And if not, is there way I can add a custom sort to just one column?

I am currently using this configuration to allow the built in sort to sort all of my data.

 [sorts]="[{prop: 'status', dir: 'desc'}]">

I think I need to create my own sort for the date column though, if I can't get it to sort the year. Is there a way to keep the custom sort for all columns but one, and add my own sort to that column? Any help would be appreciated! Thank you!

2

2 Answers

0
votes

I figured out how to get the functionality I wanted, incase anyone else has this problem as well. I have the data from my backend bringing the date through as YYYY-mm-dd and then on the Angular side I am using their date pipe

{{value | date:'MM-DD-YYYY h:mm A'}}

to get the MM-DD-YYYY format that I want. It seems like as long as the value comes in with year first, it will sort it correctly.

0
votes

It's not the best way, but the only workaround to this problem was to write my own comparator.

 <ngx-datatable-column prop="datumKnj" [comparator]="datumKnjComparator" name="Datum knj.">
  <ng-template ngx-datatable-cell-template let-value="value">
    {{value | date : 'dd.MM.yyyy'}}
  </ng-template>
</ngx-datatable-column>

And the compare function is look like this:

  datumKnjComparator(propA, propB, rowA, rowB) {
    console.log('Sorting Comparator', propA, propB, rowA, rowB);
    // do something with rowA and rowB objects.
    if (rowA.datumKnj < rowB.datumKnj) { return -1; }
    if (rowA.datumKnj > rowB.datumKnj) { return 1; }
  }