当前位置: 首页 > news >正文

Antd中使用Table集成 react-resizable实现可伸缩列

需求:需要实现可自定义拖拽宽度的Table列表

官方文档:集成 react-resizable 来实现可伸缩列。

 

问题

直接安装npm install react-resizable --save

会直接报错

ERROR  Failed to compile with 1 errors                                                                                                                                                 17:25:56error  in ./node_modules/react-draggable/build/cjs/Draggable.jsModule parse failed: Unexpected token (210:22)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|   // the underlying DOM node ourselves. See the README for more information.
|   findDOMNode() /*: ?HTMLElement*/{
>     return this.props?.nodeRef?.current ?? _reactDom.default.findDOMNode(this);
|   }
|   render() /*: ReactElement<any>*/{@ ./node_modules/react-draggable/build/cjs/cjs.js 6:4-26@ ./node_modules/react-resizable/build/Resizable.js@ ./node_modules/react-resizable/index.js

 在查阅网上内容后  把问题定位在安装的 react-resizable版本兼容上  因为当前公司项目使用的是Antd3.x    偏老的项目

兼容版本

依赖库推荐版本说明
antd3.x (如 3.26.20)Ant Design 3.x 主版本
react-resizable1.x (如 1.11.1)支持 React 16+,与 Antd 3.x 兼容
react-draggable4.x (如 4.4.5)react-resizable@1.x 的 peerDependency

问题解决 | 安装命令

npm install antd@3.x react-resizable@1.11.1 react-draggable@4.4.5

完整步骤

1. 确保 react-resizable 的样式文件被正确引入

react-resizable 需要手动引入其 CSS 样式文件,否则拖拽手柄会不可见。
在代码顶部添加以下引入语句

import "react-resizable/css/styles.css"; // 必须引入!

如果使用 Umi.js,确保样式文件能被 Webpack 正确处理。如果仍然不生效,可以尝试在 global.less 中直接复制样式内容:

/* global.less */
.react-resizable {position: relative;
}
.react-resizable-handle {position: absolute;width: 10px;height: 100%;bottom: 0;right: -5px;cursor: col-resize;background: transparent;z-index: 1;
}
.react-resizable-handle:hover {background: #eee;
}

2. 检查 react-resizable 和 react-draggable 的版本

Antd 3.x 需要配合 react-resizable@1.x 和 react-draggable@4.x,版本过高可能导致兼容性问题。
安装指定版本

npm install react-resizable@1.11.1 react-draggable@4.4.5

3. 确认 Umi.js 的配置

如果样式仍未生效,可能是 Umi.js 的 CSS 加载顺序问题。
在 Umi 的配置文件(如 .umirc.ts)中检查以下配置:

export default {// 确保 CSS 相关 loader 配置正确cssLoader: {modules: {localIdentName: '[local]', // 避免样式被哈希化},},// 如果需要,显式注入样式styles: ['https://unpkg.com/react-resizable@1.11.1/css/styles.css'],
};

4. 检查 DOM 结构是否正确

在浏览器开发者工具中检查 <th> 元素是否包含 react-resizable-handle 子元素:

  • 如果存在但不可见,说明样式未生效(回到步骤 1)。

  • 如果根本不存在,说明 Resizable 组件未正确渲染(检查版本或代码逻辑)。


5. 完整代码修正示例

import React from "react";
import { Table } from "antd";
import { Resizable } from "react-resizable";
import "react-resizable/css/styles.css"; // 关键!确保引入样式const ResizableTitle = (props) => {const { onResize, width, ...restProps } = props;return (<Resizablewidth={width}height={0}onResize={onResize}draggableOpts={{ enableUserSelectHack: false }}><th {...restProps} /></Resizable>);
};class Demo extends React.Component {state = {columns: [{ title: "Date", dataIndex: "date", width: 200 },{ title: "Amount", dataIndex: "amount", width: 100 },{ title: "Type", dataIndex: "type", width: 100 },{ title: "Note", dataIndex: "note", width: 100 },{ title: "Action", key: "action", render: () => <a>Delete</a> },],};components = {header: {cell: ResizableTitle,},};handleResize = (index) => (e, { size }) => {this.setState(({ columns }) => {const nextColumns = [...columns];nextColumns[index] = { ...nextColumns[index], width: size.width };return { columns: nextColumns };});};render() {const columns = this.state.columns.map((col, index) => ({...col,onHeaderCell: (column) => ({width: column.width,onResize: this.handleResize(index),}),}));return (<Tableborderedcomponents={this.components}columns={columns}dataSource={this.data}/>);}
}export default Demo;

在 global.less 或你的全局样式文件中,添加以下 CSS:

/* 覆盖 react-resizable 默认的斜向拖动手柄 */
.react-resizable-handle {position: absolute;width: 10px !important;       /* 调整手柄宽度 */height: 100% !important;      /* 手柄高度和表头一致 */bottom: 0;right: -5px;                  /* 让手柄稍微超出列边界 */cursor: col-resize !important; /* 列调整光标 */background-color: transparent !important;z-index: 1;
}/* 鼠标悬停时显示手柄 */
.react-resizable-handle:hover {background-color: #f0f0f0 !important;
}/* 确保表头单元格可以触发拖拽 */
.react-resizable {position: relative !important;
}

 


最终效果

 

 

http://www.lryc.cn/news/590547.html

相关文章:

  • PowerJob集群机器数为0问题
  • 深度剖析 TDMQ RabbitMQ 版经典队列底层存储机制
  • vue页面不销毁的情况下再返回,总是执行created,而不触发 activated
  • QOpenGLWidget自定义控件— 2D点云显示(支持平移、放缩、绘制网格)
  • SpringBoot集成Minio存储文件,开发图片上传等接口
  • 【c++深入系列】:万字详解list(附模拟实现的list源码)
  • 【Fedora 42】Linux内核升级后,鼠标滚轮失灵,libinput的锅?
  • 开源 python 应用 开发(六)网络爬虫
  • ubuntu中拷贝docker容器中的文件到宿主机
  • IKE学习笔记
  • K8s 自定义调度器 Part1:通过 Scheduler Extender 实现自定义调度逻辑
  • AI产品经理面试宝典第28天:自动驾驶与智慧交通融合面试题与答法
  • Xshell 7.0.0111p.exe 下载安装教程 - 详细步骤指南(包含安装包)
  • Kotlin获取集合中的元素操作
  • Kotlin比较接口
  • 《工程伦理》分析报告二 无人驾驶
  • 利用pdfjs实现的pdf预览简单demo(包含翻页功能)
  • 用AI做带货视频评论分析进阶提分【Datawhale AI 夏令营】
  • Windows11怎样禁止应用开机启动
  • pytorch | minist手写数据集
  • 每日算法刷题Day49:7.16:leetcode 差分5道题,用时2h
  • C# 按照主题的订阅 按照类型的订阅
  • OCR 与 AI 图像识别:协同共生的智能双引擎
  • Spring MVC中@PathVariable的用法详解
  • Vue 3 中调用子组件方法
  • LLM大语言模型不适合统计算数,可以让大模型根据数据自己建表、插入数据、编写查询sql统计
  • 从洞察到行动:大数据+AI赋能消费者洞察
  • 【前端】HTML语义标签的作用与实践
  • Ubuntu GRUB菜单密码重置教程
  • 重学SpringMVC一SpringMVC概述、快速开发程序、请求与响应、Restful请求风格介绍