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
    评论
作为前端使用React、TypeScript、React Router、Redux、Axios、Ant Design和Sass开发ERP软件项目的职责描述,主要包括以下几个方面: 1. 分析需求和设计界面:与产品经理、设计师等团队成员合作,分析用户需求和产品设计,设计符合用户需求的界面,并提供良好的用户体验。 2. 使用React和TypeScript开发组件:根据设计稿或需求文档,使用React和TypeScript开发可复用的组件,利用类型检查提高代码的可靠性和可维护性。 3. 使用React Router实现路由管理:使用React Router进行页面之间的导航和路由管理,确保页面之间的跳转和参数传递的正常。 4. 使用Redux进行状态管理:使用Redux进行全局状态的管理,包括定义和处理数据流、异步操作、状态持久化等,确保数据的一致性和可控性。 5. 使用Axios进行网络请求:使用Axios库发送HTTP请求与后端API进行数据交互,并处理请求的错误和异常情况。 6. 使用Ant Design进行UI开发:使用Ant Design提供的组件库进行界面开发,保证界面的一致性和美观性,并根据需求进行自定义样式。 7. 使用Sass进行样式管理:使用Sass预处理器编写可复用的样式代码,提高样式开发效率,并保持样式的可维护性。 8. 优化性能和用户体验:通过前端优化技术(如代码分割、懒加载、缓存等),提升ERP软件的性能和用户体验,确保页面加载速度快、操作流畅。 9. 跨浏览器兼容性测试:测试并确保ERP软件在各种主流浏览器(如Chrome、Firefox、Safari等)下的正常运行,并解决兼容性问题。 10. 代码版本管理和团队协作:使用版本管理工具(如Git)管理代码,与团队成员协作开发,参与代码评审和项目迭代。 11. 系统维护和故障排除:及时响应用户反馈并解决软件中出现的前端问题,修复bug,确保ERP软件的稳定运行。 总的来说,前端使用React、TypeScript、React Router、Redux、Axios、Ant Design和Sass开发ERP软件项目的职责是负责开发和维护ERP软件的前端界面和功能,与后端进行数据交互,优化性能和用户体验,并与团队成员协作推动项目的成功交付。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

雪山上的小灰熊

谢谢支持哦

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

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

打赏作者

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

抵扣说明:

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

余额充值