Ant Design + react-drag-listview实现Table拖拽变换列位置

Ant Design + react-drag-listview实现Table拖拽变换列位置

Ant Design + react-drag-listview + react-resizable 实现拖拽变换列位置, 可改变列宽

废话不多说,直接上代码

一:单独使用,只实现拖拽变换列位置

import React from 'react';
import { Table } from 'antd';
import ReactDragListView from 'react-drag-listview'

class DragTable extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            columns: this.props.columns
        }
    }

    static getDerivedStateFromProps(nextProps, prevState) {
        return null;
    }
    
    render() {
        const { components, columns, ...others } = this.props;
        const that = this;
        const dragProps = {
            onDragEnd(fromIndex, toIndex) {
                const columns = [...that.state.columns];
                const item = columns.splice(fromIndex, 1)[0];
                columns.splice(toIndex, 0, item);
                that.setState({
                    columns
                });
            },
            nodeSelector: "th"
        };

        
        return (<ReactDragListView.DragColumn {...dragProps}>
            <Table 
                columns={this.state.columns}
                { ...others }
            ></Table>
        </ReactDragListView.DragColumn>)
    }
}

export default DragTable;

 二:与react-resizable结合使用,可改变列宽,可改变列位置

react-resizable可参Ant Design + react-resizable实现列表页可拖拽宽度变化_仗剑走天涯的博客-CSDN博客

 DragTable 组件封装:

import React from 'react';
import { Table } from 'antd';
import { Resizable } from 'react-resizable';
import ReactDragListView from 'react-drag-listview'
import 'react-resizable/css/styles.css';
import './index.css'

const ResizeableTitle = (props) => {
	const { onResize, width, ...restProps } = props;
    if (!width) {
        return <th {...restProps} />;
    }
    return (
        <Resizable
            width={width?width:null}
            height={0}
            minConstraints={[50, 50]}
            onResize={onResize}
            draggableOpts={{ enableUserSelectHack: false }}
            >
            <th {...restProps} />
        </Resizable>
    );
}

class DragTable extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            columns: this.props.columns
        }
    }

    static getDerivedStateFromProps(nextProps, prevState) {
        return null;
    }

    components = {
		header: {
			cell: ResizeableTitle,
	 	},
	};
   
	handleResize = index => (e, { size }) => {
		this.setState(({ columns }) => {
			const nextColumns = [...columns];
			nextColumns[index] = {
                ...nextColumns[index],
                width: size.width?size.width:'100%',
            };
			return { columns: nextColumns };
		});
	}
    
    render() {
        const { components, columns, ...others } = this.props;
        const that = this;
        const dragProps = {
            onDragEnd(fromIndex, toIndex) {
                const columns = [...that.state.columns];
                const item = columns.splice(fromIndex, 1)[0];
                columns.splice(toIndex, 0, item);
                that.setState({
                    columns
                });
            },
            nodeSelector: "th",
            handleSelector: 'p'
        };

        const newColumns = this.state.columns.map((col, index) => ({
            ...col,
            onHeaderCell: column => ({
                width: column.width?column.width:100, // 100 没有设置宽度可以在此写死 例如100试下
                onResize: this.handleResize(index),
            }),
        })) 
        
        return (<ReactDragListView.DragColumn {...dragProps}>
            <Table 
                components={this.components}
                columns={newColumns}
                { ...others }
            ></Table>
        </ReactDragListView.DragColumn>)
    }
}

export default DragTable;

样式调整

.react-resizable-handle-se {
    top: 0;
    right: 0;
    cursor: w-resize;
}

.react-resizable-handle {
    position: absolute;
    width: 10px;
    height: calc(100% - 10px);
    background-repeat: no-repeat;
    background-origin: content-box;
    box-sizing: border-box;
    background-image: none;
    background-position: bottom right;
    border-right: 1px solid #f0f0f0;
    margin: 5px 0px;
    padding: 0px;
}

.ant-table-header .ant-table-cell p {
    width: calc(100% - 15px);
    cursor: move;
    margin-left: 5px;
}

.ant-table thead > tr > th {
    margin-right: 10px;
}

.ant-table-header {
    -moz-user-select:none; /* Firefox私有属性 */
    -webkit-user-select:none; /* WebKit内核私有属性 */
    -ms-user-select:none; /* IE私有属性(IE10及以后) */
    -khtml-user-select:none; /* KHTML内核私有属性 */
    -o-user-select:none; /* Opera私有属性 */
    user-select:none; /* CSS3属性 */
}

.ant-table-header th:last-child .react-resizable-handle {
    display: none;
}

使用:

import React from 'react';
import DragTable from '../../components/DragTable';

class APP extends React.Component {
     constructor(props) {
        super(props);
        this.state = {
            loading: false,
            data: []
        }
    }
    render() {
        const columns = [
            { title: () => { return <p>name</p> }, dataIndex: 'name', key: 'name', ellipsis: true },
            { title: () => { return <p>value</p> }, dataIndex: 'value', key: 'value', ellipsis: true }
        ]
        return (<DragTable 
                    columns={columns}
                    loading={this.state.loading}
                    rowKey='id'
                    dataSource={this.state.data}
                    size='small'
                    pagination={false}
                    style={{marginTop: 10}}
                    rowClassName={(record, index) => {}}
                ></DragTable>);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪山上的小灰熊

谢谢支持哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值