数据排序 antd拖拽排序 记录

官网打开慢问题

antd:
	https://ant-design.gitee.io/index-cn
antd-pro
	https://procomponents.ant.design/docs/getting-started/

编辑表单 排序

方案一:
对表格每行实施编辑 然后保存操作
优点: 引用库少 操作简单
缺点: 输入排序后需要查重
- eg:
实现方案

https://blog.csdn.net/qq_42359718/article/details/125623254?spm=1001.2014.3001.5502

方案二:
拖拽排序
优点: 拖拽排序,交互直观, 数据无需查重
缺点:引用类库 逻辑相对复杂

注意: **所渲染数据中每条数据必定包含index值** 

eg:
以下组件只对数据进行排序 、删除功能 ,不做数据保存
传入: dataSource 数据展示
changeDataSource 数据改变后回调
deleteSupply 删除回调

import { MenuOutlined } from '@ant-design/icons';
import { Table } from 'antd';
import { arrayMoveImmutable } from 'array-move';
import React, { useEffect, useState } from 'react';
import { SortableContainer, SortableElement, SortableHandle } from 'react-sortable-hoc';
// import './SortTable.less';

const DragHandle = SortableHandle(() => (
    <MenuOutlined
        style={{
            cursor: 'grab',
            color: '#999',
        }}
    />
));

const SortableItem = SortableElement((props) => <tr {...props} />);
const SortableBody = SortableContainer((props) => <tbody {...props} />);

const SortTable = (fprops) => {
    const { dataSource, changeDataSource, deleteSupply } = fprops;
    const [showData, setDataSource] = useState([]);

    const onSortEnd = ({ oldIndex, newIndex }) => {
        if (oldIndex !== newIndex) {
            const newData = arrayMoveImmutable(showData.slice(), oldIndex, newIndex).filter(
                (el) => !!el,
            );
            changeDataSource(JSON.parse(JSON.stringify(newData)));
        }
    };
    
    const DraggableContainer = (props) => (
        <SortableBody
            useDragHandle
            disableAutoscroll
            helperClass="row-dragging"
            onSortEnd={onSortEnd}
            {...props}
        />
    );
    
    const DraggableBodyRow = ({ className, style, ...restProps }) => {
    // function findIndex base on Table rowKey props and should always be a right array index
        const index = showData.findIndex((x) => x.index === restProps['data-row-key']);
        return <SortableItem index={index} {...restProps} />;
    };

    const columns = [
        {
            title: '',
            width: 50,
            dataIndex: 'sort',
            className: 'drag-visible',
            render: () => <DragHandle />,
        },
        {
            title: '商品编号',
            dataIndex: 'spid',
            key: 'spid',
            width: 120,
            ellipsis: true,
        },
        {
            title: '商品名称',
            dataIndex: 'spname',
            key: 'spname',
            ellipsis: true,
            width: 180,
            render: (text, row) => {
                const { spname } = row;
                return <span title={spname}>{spname}</span>;
            }
        },
      
        {
            title: '操作',
            dataIndex: 'spid',
            key: 'spid',
            render: (text) => {
                return (
                    <a
                        href="#"
                        onClick={() => {
                            deleteSupply(text);
                        }}
                    >删除</a>
                );
            },
            width: 80,
        },
    ];

    useEffect(() => {
        setDataSource(JSON.parse(dataSource));
        changeDataSource(JSON.parse(dataSource));
    }, [dataSource]);

    return (
        <Table
            className="sortTable"
            pagination={false}
            dataSource={showData}
            columns={columns}
            bordered
            rowKey="index"
            components={{
                body: {
                    wrapper: DraggableContainer,
                    row: DraggableBodyRow,
                },
            }}
        />
    );
};

export default SortTable;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值