react antd table拖拽

下载node包

npm install react-resizable -D
npm install @types/react-resizable --save-dev

定义一个公用组建 ResizableTable.tsx

import { useEffect, useState } from "react";
import { Resizable } from "react-resizable";
import "./resize.scss"
import { Table } from 'antd';
const ResizableTitle = (props: any) => {
    const { onResize, width, ...restProps } = props

    if (!width) { return <th {...restProps} /> }
    return (
        <Resizable
            width={width}
            height={0}
            handle={
                <span className="react-resizable-handle" onClick={(e) => { e.stopPropagation() }} />
            }
            onResize={onResize}
            draggableOpts={{ enableUserSelectHack: false }}
        >
            <th {...restProps} />
        </Resizable>
    )
}


/**
 * ant-table 可伸缩列
 * @param props
 * @returns
 */
const ResizableTable = (props: any) => {
    const [columns, setColumns] = useState([]);

    useEffect(() => {
        if (props.columns) {
            setColumns(props.columns)
        }
    }, [props.columns]);

    const handleResize = (index: number) => (_: any, { size }: any) => {
        const newColumns: any = [...columns];
        newColumns[index] = {
            ...newColumns[index],
            width: size.width,
        };
        setColumns(newColumns);
    };

    const mergeColumns = columns.map((col: any, index) => ({
        ...col,
        onHeaderCell: (column: any) => ({
            width: column.width,
            onResize: handleResize(index),
        }),
    }));

    return (
        <div className="resizeTable">
            <Table
                {...props}
                components={{
                    header: {
                        cell: ResizableTitle
                    }
                }}
                scroll={{ x: 900 }}
                columns={mergeColumns}
                dataSource={props.dataSource}
            />
        </div>
    )
}

export default ResizableTable

定义样式文件 resize.scss

.resizeTable { 
    .react-resizable {
      position: relative;
      background-clip: padding-box;
      user-select: none;
    }
 
    // 防止默认出现横向滚动条
    .ant-table-content>table{min-width: calc(100% - 5px)!important;}
 
    .react-resizable-handle {
      position: absolute;
      width: 10px;
      height: 100%;
      bottom: 0;
      right: -5px;
      cursor: col-resize;
      background-image: none;
      z-index: 1;
    }
}

然后就可以愉快的使用了,使用实例(只需要换个名字就好了,其他和正常使用table没有区别,但是要有宽度哦,不然没办法拖拽)
 

<ResizableTable dataSource={dataSource} columns={columns} />;

相关推荐

  1. React实践

    2024-07-10 08:38:03       73 阅读
  2. React实践

    2024-07-10 08:38:03       67 阅读
  3. React实践

    2024-07-10 08:38:03       54 阅读
  4. React 元素教程 react-dnd 实现

    2024-07-10 08:38:03       65 阅读
  5. HTML5- 功能

    2024-07-10 08:38:03       38 阅读
  6. 实现窗口移动

    2024-07-10 08:38:03       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 08:38:03       99 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 08:38:03       107 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 08:38:03       90 阅读
  4. Python语言-面向对象

    2024-07-10 08:38:03       98 阅读

热门阅读

  1. VB 关键字

    2024-07-10 08:38:03       35 阅读
  2. 前端面试题(13)答案版

    2024-07-10 08:38:03       35 阅读
  3. 智能警卫:Conda包依赖的自动监控之道

    2024-07-10 08:38:03       34 阅读
  4. vue处理重复请求

    2024-07-10 08:38:03       29 阅读
  5. 深度学习:从数据采集到模型测试的全面指南

    2024-07-10 08:38:03       25 阅读
  6. jQuery Mobile 实例

    2024-07-10 08:38:03       31 阅读
  7. Electron 简单搭建项目

    2024-07-10 08:38:03       35 阅读
  8. adb 常用的命令总结

    2024-07-10 08:38:03       30 阅读