antd的Table组件多列排序时删除取消排序

2 篇文章 0 订阅
1 篇文章 0 订阅

antd的Table组件多列排序时删除取消排序

import React, { useEffect, useState } from 'react';
import { Table } from 'antd';

export default () => {
  // 默认进入或刷新页面为不排序 
  const [sortType, setSortType] = useState<string>(''); // 排序类型 默认无
  const [currentSort, setCurrentSort] = useState<string>(''); // 当前排序列 默认无
  // 默认进入或刷新页面为某列排序请设置默认值 
  // const [sortType, setSortType] = useState<string>('descend'); // 排序类型 默认降序
  // const [currentSort, setCurrentSort] = useState<string>('name'); // 当前排序列 默认name姓名列

  // 表格数据
  const dataSource = [
    {
      key: '1',
      name: '胡彦斌',
      age: 32,
      address: '西湖区湖底公园1号',
    },
    {
      key: '2',
      name: '胡彦祖',
      age: 42,
      address: '西湖区湖底公园1号',
    },
    {
      key: '3',
      name: '胡彦祖',
      age: 42,
      address: '西湖区湖底公园1号',
    },
    {
      key: '4',
      name: '胡彦祖',
      age: 42,
      address: '西湖区湖底公园1号',
    },
  ];

  useEffect(() => {
    loadData();
  }, [sortType, currentSort]);

  async function loadData() {
    let sort = {};
    if (currentSort) {
      sort = {
        name: currentSort,
        type: sortType,
      };
    }
    // 获取到当前排序列的名称和类型
    console.log(sort);
    // 根据自己需求改为请求接口 根据接口传递参数
    // const { dataSet, total } = await search({ pageIndex: 1, pageSize: 20, ...sort });
    // return { data: dataSet, total: total };
  }

  const columns: any = () => {
    let extra = {
      sortOrder: sortType,
      sortDirections: ['descend', 'ascend'],
      showSorterTooltip: false,
    };

    return [
      {
        title: '姓名',
        dataIndex: 'name',
        key: 'name',
        sorter: true,
        ...(currentSort === 'name' ? extra : null),
      },
      {
        title: '年龄',
        dataIndex: 'age',
        key: 'age',
        sorter: true,
        ...(currentSort === 'age' ? extra : null),
      },
      {
        title: '住址',
        dataIndex: 'address',
        key: 'address',
        sorter: true,
        ...(currentSort === 'address' ? extra : null),
      },
    ];
  };
  return (
    <div>
      <Table
        columns={columns()}
        dataSource={dataSource}
        onChange={(pagination: any, filters: any, sorter: any, extra: any) => {
          if (currentSort && currentSort !== sorter.columnKey) {
          	// 切换排序列重置为升序
            setCurrentSort(sorter.columnKey);
            setSortType('ascend');
          } else {
            setCurrentSort(sorter.columnKey);
            setSortType(sorter.order || 'descend');
          }
        }}
      />
    </div>
  );
};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
antd Table组件提供了很方便的列排序功能,可以通过设置`sorter`属性来实现。 首先在表头列中设置`sorter`属性为一个对象,对象中包含两个属性:`compare`和`multiple`。 `compare`属性是一个比较函数,接收两个参数,分别是当前行数据的两个单元格的数据,返回值为`1`或`-1`。当返回值为`1`表示第一个单元格的数据大于第二个单元格的数据,需要升序排列;当返回值为`-1`表示第二个单元格的数据大于第一个单元格的数据,需要降序排列。 `multiple`属性用于支持多列排序,当该属性为`true`,按下`shift`键再点击列头可以添加该列到排序中。 举个例子,假设我们有一个表格数据如下: ```javascript const dataSource = [ { key: '1', name: '张三', age: 23, address: '北京市海淀区', }, { key: '2', name: '李四', age: 25, address: '上海市黄浦区', }, { key: '3', name: '王五', age: 20, address: '广州市天河区', }, ]; ``` 我们可以在表头列中设置`sorter`属性来实现对数据的排序,比如按照年龄从小到大排序: ```javascript import { Table } from 'antd'; const columns = [ { title: '姓名', dataIndex: 'name', key: 'name', }, { title: '年龄', dataIndex: 'age', key: 'age', sorter: (a, b) => a.age - b.age, }, { title: '地址', dataIndex: 'address', key: 'address', }, ]; function App() { return <Table dataSource={dataSource} columns={columns} />; } ``` 这样就可以在年龄列头点击来实现升序或降序排列了。 如果要支持多列排序,只需要将`multiple`属性设置为`true`即可: ```javascript import { Table } from 'antd'; const columns = [ { title: '姓名', dataIndex: 'name', key: 'name', }, { title: '年龄', dataIndex: 'age', key: 'age', sorter: { compare: (a, b) => a.age - b.age, multiple: true }, }, { title: '地址', dataIndex: 'address', key: 'address', sorter: { compare: (a, b) => a.address.localeCompare(b.address), multiple: true }, }, ]; function App() { return <Table dataSource={dataSource} columns={columns} />; } ``` 现在我们可以按下`shift`键再点击地址列头来实现多列排序了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值