简体   繁体   English

Ngx-Datatable 按日期列排序不起作用

[英]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").我需要按日期格式(“dd/mm/yyyy”)和(“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!谢谢!

Ngx-tables can sort by date, but you need to specify that this is date, Ngx-tables 可以按日期排序,但是需要指定这是日期,

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.取自 ngx-datatable 存储库。

You can try to put pipe on your date, so angular will do your work.您可以尝试将管道放在约会对象上,这样 angular 就可以完成您的工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM