Antd Table组件拖动调整列宽的一种实现

15 篇文章 1 订阅
9 篇文章 0 订阅

近来一直在用React+Ant Design进行开发,然后用了一段时间发现其中的Table组件却不支持拖动列调整列宽的功能。对于习惯了Vue+ElementUI的我,多多少少有点不方便。对于ElementUITable组件,将border属性设置成true之后便可拖动列宽。

方案

在网上一番查找之后,看到其中一种方案是借助于React-Resizable实现拖动列宽效果。关于React-Resizable的详细用法,可以查看官方Github主页https://github.com/react-grid-layout/react-resizable
简单的说,就是借助AntdTable组件的components参数,覆盖Table组件header
cell的渲染方式,在cell里嵌套一层React-Resizable库的Resizable组件,通过Resizable组件的onResize事件参数,进而调整Table列组件的宽度,达到调整列宽的目的。
其中Table组件的components参数具体属性如下:

export interface TableComponents<RecordType> {
  table?: CustomizeComponent;
  header?: {
    wrapper?: CustomizeComponent;
    row?: CustomizeComponent;
    cell?: CustomizeComponent;
  };
  body?:
    | CustomizeScrollBody<RecordType>
    | {
        wrapper?: CustomizeComponent;
        row?: CustomizeComponent;
        cell?: CustomizeComponent;
      };
}

代码实现

import { Table } from "antd";
import React from "react";
import { Resizable } from "react-resizable";
import "../node_modules/react-resizable/css/styles.css";  // 引入react-resizable样式,否则无法显示Resize样式

const dataSource = [
  {
    key: "1",
    name: "张三",
    age: 32,
    address: "西湖区湖底公园1号"
  },
  {
    key: "2",
    name: "李四",
    age: 42,
    address: "西湖区湖底公园1号"
  }
];

const columns = [
  {
    title: "姓名",
    dataIndex: "name",
    key: "name",
    width: 110
  },
  {
    title: "年龄",
    dataIndex: "age",
    key: "age",
    width: 90
  },
  {
    title: "住址",
    dataIndex: "address",
    key: "address",
    width: 220
  },
  {}
];

// 重写Table组件header单元格渲染方式
const ResizableTitle = (props) => {
  const { onResize, width, ...restProps } = props;
  if (width === undefined) {
    return <th {...restProps}></th>;
  }
  return (
    // 外包一层Resizable组件
    // 其中onResize属性调用col.onResize方法
    <Resizable width={width} height={0} onResize={onResize}>
      <th {...restProps}></th>
    </Resizable>
  );
};

export default class MyTable extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      dataSource,
      columns: columns.map((col) => {
        // 注意:给每一列的onHeaderCell属性增加onResize方法,用于传递onResize事件
        col.onHeaderCell = () => ({
          width: col.width,
          onResize: this.handleResize(col)
        });
        return col;
      })
    };
  }

  // Table 组件 components 
  components = {
    header: {
      cell: ResizableTitle
    }
  };

  // 动态修改col.width属性
  handleResize = (column) => (e, { size }) => {
    this.setState(({ columns }) => {
      columns.forEach((item) => {
        if (item === column) {
          item.width = size.width;
        }
      });

      return { columns };
    });
  };

  render() {
    return (
      <div>
        <Table
          bordered
          dataSource={this.state.dataSource}
          columns={this.state.columns}
          components={this.components}
        />
      </div>
    );
  }
}

源码codesandbox地址请查看:https://codesandbox.io/s/antd-table-column-resize-width-jlpdtz?file=/src/MyTable.js

最终效果

在这里插入图片描述

拓展阅读:
https://blog.csdn.net/m0_58016522/article/details/123470541
参考链接:
https://blog.csdn.net/ysunshine512/article/details/88948545

  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用antd table组件进行分页获取数据,需要在表格中设置pagination属性,同时需要在后端接口中实现分页查询功能。 对于跨页多选,antd table提供了rowSelection属性,可以通过设置type为checkbox实现多选,但是默认只能在当前页进行选择,无法跨页选择。 要实现跨页多选,需要自定义rowSelection的selectedRowKeys和onChange方法。具体步骤如下: 1. 在state中定义selectedRowKeys数组,用于保存已选中的行的key值。 2. 在rowSelection属性中设置selectedRowKeys为state中定义的selectedRowKeys数组。 3. 在rowSelection属性中定义onChange方法,用于处理选中行的变化。在该方法中,需要使用setState更新state中的selectedRowKeys数组。 4. 在表格的pagination属性中设置onChange方法,用于处理页码变化时清空已选中的行。 以下是示例代码: ``` import React, { Component } from 'react'; import { Table } from 'antd'; class MyTable extends Component { state = { selectedRowKeys: [], // 保存已选中的行的key值 }; onSelectChange = selectedRowKeys => { // 更新state中的selectedRowKeys数组 this.setState({ selectedRowKeys }); }; onPaginationChange = () => { // 清空已选中的行 this.setState({ selectedRowKeys: [] }); }; render() { const { selectedRowKeys } = this.state; const rowSelection = { type: 'checkbox', selectedRowKeys, onChange: this.onSelectChange, }; return ( <Table rowSelection={rowSelection} columns={columns} dataSource={data} pagination={{ onChange: this.onPaginationChange }} /> ); } } ``` 注意,以上代码仅为示例,具体实现需要根据实际情况进行调整

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值