react使用antd实现表格的时间排序

import React from 'react';
import { Table } from 'antd';
import moment from 'moment';
 
const data = [
  {
    key: '1',
    date: '2018-01-11T12:00:00Z',
  },
  {
    key: '2',
    date: '2019-01-11T12:00:00Z',
  },
  // ...更多数据
];
 
const columns = [
  {
    title: 'Date',
    dataIndex: 'date',
    key: 'date',
    sorter: (a, b) => moment(a.date).valueOf() - moment(b.date).valueOf(),
    sortDirections: ['descend', 'ascend'],
  },
  // ...其他列
];
 
const App = () => {
  const [sortedInfo, setSortedInfo] = React.useState(null);
 
  const onChange = (pagination, filters, sorter) => {
    setSortedInfo(sorter);
  };
 
  return (
    <Table
      columns={columns}
      dataSource={data}
      onChange={onChange}
      sortDirections={['ascend', 'descend']}
      defaultSortedInfo={sortedInfo}
    />
  );
};
 
export default App;

  sorter={(a, b) => moment(a.created).valueOf() - moment(b.created).valueOf()}

posted @ 2024-05-29 15:47  nnc  阅读(32)  评论(0编辑  收藏  举报