React Class组件使用Antd中Resizable实现Table拖拽调节

使用react-resizable,可封装了一个ResizeableTable公共组件

(可适用于展示列动态变化和可拖拽调节表格,例如下图形式的表格)

1.安装依赖

yarn add react-resizable

2.封装公共组件ResizeableTable组件(ResizeableTable.js)

import * as React from 'react';
import { Table } from 'antd';
import { Resizable } from 'react-resizable';

import Emitter from '../utils/Emitter';

import './resizeable-table.less';

class ResizeableTitle extends React.Component {
    render() {
        const { onResize, width, resizable, onClick, ...restProps } = this.props;

        if (!width || resizable) {
            return <th {...restProps} />;
        }
        return (
            <Resizable
                width={width}
                height={0}
                handle={<span className="react-resizable-handle" onClick={(e) => { e.stopPropagation(); }} />}
                onResize={onResize}
                onResizeStart={() => { this.resizing = true; }}
                onResizeStop={() => { setTimeout(() => { this.resizing = false; }); }}
                draggableOpts={{ enableUserSelectHack: false }} >
                <th
                    {...restProps}
                    onClick={(...args) => { if (!this.resizing && onClick) { onClick(...args); } }}
                    style={{
                        ...restProps?.style,
                        width: `${width}px`,
                    }}
                />
            </Resizable>
        );
    }
}

class ResizeableTable extends React.PureComponent {
    constructor(props) {
        super(props);

        this.state = {
            columns: props.columns
        };
    }

    components = {
        header: {
            cell: ResizeableTitle, // 动态拖拽设置列宽
        }
    };

    componentDidMount() {
        // 监听columnsChanged事件
        Emitter.addListener('columnsChanged', (columns) => { this.setState({ columns: columns }); });

        const handlers = document.querySelectorAll(
            '.react-resizable .react-resizable-handle'
        );
        handlers.forEach((handler) => handler.addEventListener('click', () => { return false; }));
    }

    componentWillUnmount() {
        Emitter.removeListener('columnsChanged', (columns) => { this.setState({ columns: columns }); });
    }

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

        const components = {
            ...this.props.components,
            ...this.components
        };

        return <Table {...this.props} columns={columns} components={components} />;
    }

    // 拖拽后更新表格宽度
    handleResize = (index) => (e, { size }) => {
        e.stopImmediatePropagation();
        this.setState(({ columns }) => {
            const nextColumns = [...columns];
            nextColumns[index] = {
                ...nextColumns[index],
                width: size.width
            };
            return { columns: nextColumns };
        });
    };
}

export default ResizeableTable;

3.使用封装公共组件

引入ResizeableTable.js

import ResizeableTable from '../../../../components/ResizeableTable';

添加监听事件

Emitter.emit('columnsChanged', columns); // 可拖拽宽度表格 需要配合此事件更新内部表格

使用ResizeableTable组件

<ResizeableTable size="small" columns={columns} dataSource={data} pagination={false} scroll={{ y: (clientHeight - 235) }} onChange={this.onTableChange} /></div>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值