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} />;
相关推荐
架构师ZYL8 分钟前
node.js+Koa框架+MySQL实现注册登录
前端·javascript·数据库·mysql·node.js
gxhlh1 小时前
React Native防止重复点击
javascript·react native·react.js
miao_zz1 小时前
基于react native的锚点
android·react native·react.js
一只小白菜~1 小时前
实现实时Web应用,使用AJAX轮询、WebSocket、还是SSE呢??
前端·javascript·websocket·sse·ajax轮询
晓翔仔2 小时前
CORS漏洞及其防御措施:保护Web应用免受攻击
前端·网络安全·渗透测试·cors·漏洞修复·应用安全
jingling5552 小时前
后端开发刷题 | 数字字符串转化成IP地址
java·开发语言·javascript·算法
GISer_Jing3 小时前
【前后端】大文件切片上传
前端·spring boot
csdn_aspnet3 小时前
npm 安装 与 切换 淘宝镜像
前端·npm·node.js
GHUIJS3 小时前
【Echarts】vue3打开echarts的正确方式
前端·vue.js·echarts·数据可视化
Mr.mjw3 小时前
项目中使用简单的立体3D柱状图,不用引入外部组件纯css也能实现
前端·css·3d