如何按角度4材质排序对日期/时间列进行排序? [英] How to sort Date/Time column in angular 4 material sort?

查看:108
本文介绍了如何按角度4材质排序对日期/时间列进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用有角度的材料表,并使用matSort进行排序.但这不是对日期/时间列进行排序.它以datetime列值作为字符串.

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.

如何对有角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具有sortingDataAccessor,我们可以根据需要对其进行自定义.

MatTableDataSource has sortingDataAccessor which we can customize as per our need.

这篇关于如何按角度4材质排序对日期/时间列进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆