Antd Table表格组件 列字段数据格式自定义

chat

在使用Table组件时 后端 返回的时间字段 是时间戳格式,需要在列表中显示为标准时间格式。

后端返回的数据:

[
  {
          productId: 2,
          startDate: 156156156,
          endTime: 157157157,
          limitSize: 5,
          validityTime: 48,
          leaderPrice: 201,
          memberPrice: 250,
          shareContent: 'tuykuku',
          status: 2,
          id: 3,
          createdDate: 1586241276000,
          title: 'XXX活动',
   }
]

一种做法是在拿到后端数据后 遍历修改数据,这样虽然可以,但是相当于多遍历了一遍数据(修改数据遍历一遍,渲染数据又遍历了一遍)。

antd Table 组件 Column(列数据对象)提供了render api:

render 生成复杂数据的渲染函数,参数分别为当前行的值,当前行数据,行索引,@return 里面可以设置表格行/列合并 Function(text, record, index) {}

修改列数据描述对象(使用moment插件格式化数据):

 {
        title: '创建时间',
        dataIndex: 'createdDate',
        key: 'createdDate',
        render: text => <span>{moment(text).format('YYYY-MM-DD HH:mm:ss')}</span>,
      },

同样的,后端返回的状态 是数字,我们要将状态数字标识转成描述文字:

    {
        title: '状态',
        dataIndex: 'status',
        key: 'status',
        render: text => {
          const dataMap = {
            0: '未开始',
            1: '进行中',
            2: '暂停',
            3: '到期',
            4: '终止',
          };
          return dataMap[text];
        },
      },

版权声明:
作者:东明兄
链接:https://blog.crazyming.com/note/2235/
来源:CrazyMing
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
海报
Antd Table表格组件 列字段数据格式自定义
React Ant Design Table表格组件 时间字段格式化,时间戳转时间格式。
<<上一篇
下一篇>>
chat