react antd table拖拽

下载node包

TypeScript 复制代码
npm install react-resizable -D
npm install @types/react-resizable --save-dev

定义一个公用组建 ResizableTable.tsx

TypeScript 复制代码
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

html 复制代码
.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没有区别,但是要有宽度哦,不然没办法拖拽)

html 复制代码
<ResizableTable dataSource={dataSource} columns={columns} />;
相关推荐
元宵大师4 分钟前
[升级V2.1.5]回测模块重构:参数确认+异步进度+日志持久化!本地Web版多因子轮动系统
前端·重构
咋吃都不胖lyh13 分钟前
限流重试、指数退避、随机抖动
前端
之歆31 分钟前
DAY_11JavaScript BOM与DOM深度解析:底层原理与工程实践(上)
开发语言·前端·javascript·ecmascript
冴羽yayujs35 分钟前
GitHub 前端热榜项目 - 日榜(2026-05-17)
前端·github
老马952735 分钟前
opencode8-桌面应用实战 3
前端·人工智能·后端
逆yan_37 分钟前
🧭 基于 pnpm Workspace 和 Turborepo 的 Monorepo 最佳实践
前端·javascript·架构
广州华水科技1 小时前
单北斗形变监测一体机在大坝安全监测中的应用与技术优势
前端
沙漠1 小时前
Vue总结系列一
前端
渐儿1 小时前
React Native 实操开发文档
前端
Nturmoils1 小时前
书签真正难的不是收藏,而是找回来:我是怎么做这个 Chrome 插件的
javascript·后端·浏览器