如何在角度 4 材料排序中对日期/时间列进行排序? [英] How to sort Date/Time column in angular 4 material sort?
本文介绍了如何在角度 4 材料排序中对日期/时间列进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用有角度的材料表并使用 matSort 进行排序.但它不是对日期/时间列进行排序.它将日期时间列值作为字符串.
I'm using angular material table and using matSort for sorting. But it's not sorting the dates/time column. It takes the datetime column values as strings.
如何在 angular 4 材质中对日期/时间列进行排序?
How to sort date/time column in angular 4 material ?
我的json看起来像这样
My json will look like this
{
"name": "Rule Test 5",
"time": "2017-11-17T08:34:32",
"version": 1,
"status": "SAVED"
}, {
"name": "Availability Adjustment",
"time": "2017-11-17T10:13:27",
"version": 1,
"status": "SAVED"
}, {
"name": "Class suppression",
"time": "2017-11-17T11:18:44",
"version": 1,
"status": "SAVED"
}
我的桌子是这样的
-------------------------------
name | version | status | date |
-------------------------------
...
..
..
--------------------------------
推荐答案
解决方案如下:
this.dataSource.sortingDataAccessor = (item, property): string | number => {
switch (property) {
case 'fromDate': return new Date(item.fromDate).toNumber();
default: return item[property];
}
};
MatTableDataSource 具有 sortDataAccessor,我们可以根据需要对其进行自定义.
MatTableDataSource has sortingDataAccessor which we can customize as per our need.
这篇关于如何在角度 4 材料排序中对日期/时间列进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文