Ant Design table设置自定义排序表头和对字段进行隐藏

react + antd 动态设置表头和对表头进行排序
1、对title字段设置显示隐藏;


字段隐藏.gif

2、对title字段进行拖拽排序;


拖动排序.gif

3、选择后未点击确认关闭弹框则重置数据
重置设置.gif
import React, { Component } from "react";
import {
  Icon,
  Table,
  Button,
  Card,
  Tree,
} from "antd";

const { TreeNode } = Tree;

export default class Main extends React.Component {
  constructor(props) {
    super(props);
    let plainOptions = [
      {
        title: "表头1",
        key: "bt1"
      },
      {
        title: "表头2",
        key: "bt2"
      },
      {
        title: "表头3",
        key: "bt3"
      },
      {
        title: "表头4",
        key: "bt4"
      },
      {
        title: "表头5",
        key: "bt5"
      }
    ];//模拟接口请求的字段列表
    let checkedOptions = ["bt1", "bt2", "bt3", "bt4", "bt5"];//模拟接口请求的已选择字段
    this.state = {
      page_size: 10,
      page: 1,
      totalPage: 1,
      totalCount: 1,
      plainOptions, //默认的字段列表
      checkedOptions, //默认的已选择字段
      editPlainOptions: plainOptions, //当前选择的字段列表,未保存
      editCheckedOptions: checkedOptions, //当前已选择字段,未保存
      isClickHandleSearch:'',//设置字段后在未保存的情况下点击空白区域字段重置
    };
  }

  onFilterDropdownVisibleChange = (visible, type) => {
    if (visible && !this.state.isClickHandleSearch) {
      this.setState({
        isClickHandleSearch: false
      });
    } else {
      this.setState({
        plainOptions: this.state.editPlainOptions,
        checkedOptions: this.state.editCheckedOptions
      });
    }
  };
  handleSearch = confirm => {
    //确定 保存用户设置的字段排序和需要显示的字段key
    const { plainOptions, checkedOptions } = this.state;
    this.setState(
      {
        isClickHandleSearch: true,
        editPlainOptions: plainOptions,
        editCheckedOptions: checkedOptions
      },
      () => {
        confirm();
      }
    );
  };

  handleReset = clearFilters => {
    //用户点击取消按钮,重置字段
    clearFilters();
    this.setState({
      plainOptions: this.state.editPlainOptions,
      checkedOptions: this.state.editCheckedOptions
    });
  };

  onCheck = (checkedKeys) => {
    this.setState({
      checkedOptions: checkedKeys
    });
  };

  onDrop = info => {
    const dropKey = info.node.props.eventKey;
    const dragKey = info.dragNode.props.eventKey;
    const dropPos = info.node.props.pos.split("-");
    const dropPosition =
      info.dropPosition - Number(dropPos[dropPos.length - 1]);
    if (dropPosition === 1 || dropPosition == -1) {
      const loop = (data, key, callback) => {
        data.forEach((item, index, arr) => {
          if (item.key === key) {
            return callback(item, index, arr);
          }
          if (item.children) {
            return loop(item.children, key, callback);
          }
        });
      };
      const data = [...this.state.plainOptions];
      let dragObj;
      loop(data, dragKey, (item, index, arr) => {
        arr.splice(index, 1);
        dragObj = item;
      });
      let ar;
      let i;
      loop(data, dropKey, (item, index, arr) => {
        ar = arr;
        i = index;
      });
      if (dropPosition === -1) {
        ar.splice(i, 0, dragObj);
      } else {
        ar.splice(i + 1, 0, dragObj);
      }
      this.setState({
        plainOptions: data
      });
    }
  };

  render() {
    const {
      inquiry_list,
      page,
      page_size,
      totalCount,
      plainOptions,
      checkedOptions
    } = this.state;
    const loop = data =>
      data.map((item, index) => {
        return <TreeNode key={item.key} title={item.title} />;
      });
    const title_list = [
      {
        title: "表头1",
        dataIndex: "bt1",
        key: "bt1",
        width: 170,
        filterType: checkedOptions.some(item => item === "bt1")
      },
      {
        title: "表头2",
        dataIndex: "bt2",
        key: "bt2",
        width: 170,
        filterType: checkedOptions.some(item => item === "bt2")
      },
      {
        title: "表头3",
        dataIndex: "bt3",
        key: "bt3",
        width: 170,
        filterType: checkedOptions.some(item => item === "bt3")
      },
      {
        title: "表头4",
        dataIndex: "bt4",
        key: "bt4",
        width: 170,
        filterType: checkedOptions.some(item => item === "bt4")
      },
      {
        title: "表头5",
        dataIndex: "bt5",
        key: "bt5",
        width: 170,
        filterType: checkedOptions.some(item => item === "bt5")
      }
    ];
    let new_title_list = [];
    let scrollX = 0;
    plainOptions.map(item => {
      title_list.map(titleItem => {
        if (item.title === titleItem.title) {
          new_title_list.push(titleItem);
          if (
            titleItem.width &&
            checkedOptions.some(optionsItem => optionsItem === item.key)
          ) {
            scrollX += titleItem.width;
          }
        }
      });
    });
    scrollX = scrollX + 160;
    new_title_list.push({
      title: "操作",
      key: "action",
      width: 160,
      align: "center",
      filterDropdown: ({ confirm, clearFilters }) => (
        <div style={{ padding: 8 }}>
          <Tree
            checkable
            className="draggable-tree"
            draggable
            blockNode
            selectable={false}
            onCheck={this.onCheck}
            checkedKeys={checkedOptions}
            onDrop={this.onDrop.bind(this)}
          >
            {loop(plainOptions)}
          </Tree>
          <div>
            <Button
              type="primary"
              size="small"
              onClick={() => this.handleSearch(confirm)}
              style={{ width: "60px", marginRight: "10px" }}
            >
              确定
            </Button>
            <Button
              size="small"
              onClick={() => this.handleReset(clearFilters)}
              style={{ width: "60px" }}
            >
              取消
            </Button>
          </div>
        </div>
      ),
      filterIcon: filtered => <Icon type="setting" theme="filled" />,
      onFilterDropdownVisibleChange: this.onFilterDropdownVisibleChange.bind(
        this
      ),
      render: item => {
        return <div>查看</div>;
      }
    });
    return (
      <div>
        <Card>
          <Table
            rowKey="id"
            columns={new_title_list.filter(
              item => item.filterType || item.filterType === undefined
            )}
            dataSource={inquiry_list || []}
            pagination={{
              current: page,
              total: totalCount,
              pageSize: page_size,
              showQuickJumper: true,
              showSizeChanger: true,
              pageSizeOptions: ["10", "20", "50", "100"],
              showTotal: this.showTotal
            }}
            scroll={{ x: scrollX }}
          />
        </Card>
      </div>
    );
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原文:https://my.oschina.net/liuyuantao/blog/751438 查询集API 参...
    阳光小镇少爷阅读 3,854评论 0 8
  • 现在很多vue/react等js框架配套的UI框架,表格自带点击表头排序的动能。后来小想了js/jq 手写的话,逻...
    celineWong7阅读 3,534评论 0 1
  • 第3章 映射 映射是定义存储和索引的文档类型以及字段的过程。索引中的每一个文档都有一个类型,每种类型都有它自己的映...
    MR_ChanHwang阅读 2,320评论 0 1
  • (一)过去 有时候,找工作就像挤公交,还没有想好去哪里,已随人流挤上了车。 好不容易想好去哪里了,却阴差阳错地坐过...
    刘懋伟阅读 422评论 0 1
  • 是谁,唤醒了我沉睡的记忆; 是谁,偷走了我珍藏的时间; 是谁,带走了我宝贝的男孩; 昼与夜的交接,是你我的契机; ...
    人可禾子阅读 162评论 0 2