欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > react antd table拖拽

react antd table拖拽

2024/10/25 1:26:26 来源:https://blog.csdn.net/qq_37061571/article/details/140285251  浏览:    关键词: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 } = propsif (!width) { return <th {...restProps} /> }return (<Resizablewidth={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} />;

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com