react+antd实现Table拖拽调整列宽

12 篇文章 0 订阅

  

 注意:列需要传入 width 并且配合下面的css样式才能显示拖拽手势

import React, { useEffect, useState } from 'react';
import { Table } from 'antd';
import { Resizable } from 'react-resizable';
import './index.css';

// 调整table表头
const ResizeableTitle = (props) => {
  const { onResize, width, ...restProps } = props;

  if (!width) {
    return <th {...restProps} />;
  }

  return (
    <Resizable
      width={width}
      height={0}
      onResize={onResize}
      draggableOpts={{ enableUserSelectHack: false }}
    >
      <th {...restProps} />
    </Resizable>
  );
};

// 拖拽调整table
const ResizeTable = () => {

  // table 数据
  const dataSource = () => {
    const data = []
    for(let i = 0; i <= 500; i++) {
      data.push({
        key: i,
        name: '胡彦斌',
        age: 32,
        address: '西湖区湖底公园1号',
      })
    }
    return data
  }

  // 表格列
  const [cols, setCols] = useState([
    {
      title: '姓名',
      dataIndex: 'name',
      key: 'name',
      width: 100
    },
    {
      title: '年龄',
      dataIndex: 'age',
      key: 'age',
      width: 100
    },
    {
      title: '住址',
      dataIndex: 'address',
      key: 'address',
    },
  ]);
  const [columns, setColumns] = useState([])

  // 定义头部组件
  const components = {
    header: {
      cell: ResizeableTitle,
    },
  };

  // 处理拖拽
  const handleResize = (index) => (e, { size }) => {
    const nextColumns = [...cols];
    // 拖拽是调整宽度
    nextColumns[index] = {
      ...nextColumns[index],
      width: size.width,
    };

    setCols(nextColumns);
  };

  useEffect(() => {
    setColumns((cols || []).map((col, index) => ({
      ...col,
      onHeaderCell: (column) => ({
        width: column.width,
        onResize: handleResize(index),
      }),
    })))
  }, [cols])

  return (
    <div className="components-table-resizable-column">
      <Table
        size="small"
        bordered
        components={components}
        columns={columns}
        dataSource={dataSource()}
      />
    </div>
  )
}

export default ResizeTable;

index.css

  /* 内容过多以...显示 */
  .ellipsisText {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: block;
  }
  /* 显示拖拽样式 */
  .react-resizable {
    position: relative ;
    background-clip: padding-box;
  }
  /* 显示拖拽手势 */
  .react-resizable-handle {
    position: absolute;
    width: 10px;
    height: 100%;
    bottom: 0;
    right: 0;
    cursor: col-resize;
    z-index: 6;
    //border-left: white 1px solid;
  }
  .react-resizable-handle:hover {
    //background-color: #69c0ff;
  }

  • 6
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

__畫戟__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值