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

东明兄 2020-04-14
0条评论 3,243 次浏览
东明兄 2020-04-140条评论 3,243 次浏览

在使用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];
        },
      },

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注