当前位置: 首页 > news >正文

react使用react-sortable-hoc实现拖拽

react-sortable-hoc拖拽

安装

 npm install react-sortable-hoc --save

代码如下(示例):

import React, { useImperativeHandle, forwardRef, memo, useState } from 'react';import { DrawerForm } from '@ant-design/pro-form';import { message, Select ,Table} from 'antd';import { MenuOutlined } from '@ant-design/icons';import { useUpdateEffect } from 'react-use';// 核心代码import { SortableContainer, SortableElement, SortableHandle } from 'react-sortable-hoc';type ModalProps = {ref?: React.MutableRefObject<SortTrackDrawerRefObject | undefined>;onUpdated: () => void;};export type SortTrackDrawerRefObject = {openModal: () => void;};// 定义拖拽的图标const DragHandle = SortableHandle(() => <MenuOutlined style={{ cursor: 'move', color: '#999' }} />);const columns = [{title: '排序',align: 'center',dataIndex: 'sort',width: 30,className: 'drag-visible',render: () => <DragHandle />,},{title: '音频名称',dataIndex: 'name',className: 'drag-visible',},];const SortTrackDrawer: React.ForwardRefRenderFunction<SortTrackDrawerRefObject, ModalProps> = memo(forwardRef((sProps, ref) => {const [visible, setVisible] = useState<boolean>(false);// 拖拽体const SortableItem = SortableElement((props: any) => <tr {...props} />);// 拖拽容器const SortContainer = SortableContainer((props: any) => <tbody {...props} />);// 拖拽时原列表替换const arrayMoveMutable=(array: any[], fromIndex: number, toIndex: number) =>{const startIndex = fromIndex < 0 ? array.length + fromIndex : fromIndex;if (startIndex >= 0 && startIndex < array.length) {const endIndex = toIndex < 0 ? array.length + toIndex : toIndex;const [item] = array.splice(fromIndex, 1);array.splice(endIndex, 0, item);}}// 拖拽时返回新数组const arrayMoveImmutable=(array: any[], fromIndex: number, toIndex: number) =>{array = [...array];arrayMoveMutable(array, fromIndex, toIndex);return array;}// 拖拽后回调const onSortEnd = ({ oldIndex, newIndex }:{ oldIndex: number; newIndex: number }) => {if (oldIndex !== newIndex) {const newData = arrayMoveImmutable([...tableData], oldIndex,                                 newIndex).filter((el) => !!el);setTableData([...newData]);}};//  获取表格数据const getTableData = async () => {try {let res = [{name:"测试13",orderNum:1uid:1,}]setTableData(res);} finally {}};// 确定按钮返回的数据const handleFinish = async () => {const uids = tableData.map((item) => item.uid);console.log(uids)message.success('排序已更新');return true;};// 拖拽容器方法const DraggableContainer = (props: any) => (<SortContaineruseDragHandledisableAutoscrollhelperClass="row-dragging"onSortEnd={onSortEnd}{...props}/>);// 拖拽体方法const DraggableBodyRow = (props: any) => {const { className, style, ...restProps } = props;const index = tableData.findIndex((x) => x.uid === restProps['data-row-key']);return <SortableItem index={index} {...restProps} />;};// 进入组件加载数据useUpdateEffect(() => {getTableData();}, [orderType]);return (<DrawerFormwidth={isMobile ? '100%' : 520}onVisibleChange={setVisible}title={<div className="sort-track-drawer-header"><div>拖拽排序({tableData.length})</div></div>}visible={visible}onFinish={handleFinish}drawerProps={{ closable: false }}submitter={{searchConfig: {submitText: '保存排序',resetText: '取消',},}}><Tablepagination={false}dataSource={tableData}columns={columns as any}rowKey="uid"size="small"showHeader={false}loading={loading}components={{body: {wrapper: DraggableContainer,row: DraggableBodyRow,},}}/></DrawerForm>);}),);export default SortTrackDrawer;

 

效果如图: 

相关文章:

  • 海外媒体发稿:如何利用8种出口贸易媒体发稿实现销售突破-华媒舍
  • 路由器基础(七):NAT原理与配置
  • 数据中心系统解决方案
  • XXL-JOB 默认 accessToken 身份绕过导致 RCE
  • 计算机视觉基础——基于yolov5-face算法的车牌检测
  • dubbo集群容错策略
  • mediasoup webrtc音视频会议搭建
  • 【KVM】软件虚拟化和硬件虚拟化类型
  • JavaScript执行上下文和调用栈
  • chrome 扩展 popup 弹窗的使用
  • 微信小程序 uCharts的使用方法
  • (自适应手机端)响应式新闻博客知识类pbootcms网站模板 自媒体运营博客网站源码下载
  • Session+Cookie实现登录认证
  • 【QT】文件读写
  • 【Java 进阶篇】Java文件下载案例详解
  • Iterator 和 for...of 循环
  • leetcode讲解--894. All Possible Full Binary Trees
  • miaov-React 最佳入门
  • Spring Security中异常上抛机制及对于转型处理的一些感悟
  • uni-app项目数字滚动
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 函数式编程与面向对象编程[4]:Scala的类型关联Type Alias
  • 首页查询功能的一次实现过程
  • 做一名精致的JavaScripter 01:JavaScript简介
  • scrapy中间件源码分析及常用中间件大全
  • 阿里云ACE认证学习知识点梳理
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • !!【OpenCV学习】计算两幅图像的重叠区域
  • #includecmath
  • #图像处理
  • ()、[]、{}、(())、[[]]等各种括号的使用
  • (Matalb分类预测)GA-BP遗传算法优化BP神经网络的多维分类预测
  • (pojstep1.1.2)2654(直叙式模拟)
  • (附源码)springboot猪场管理系统 毕业设计 160901
  • (附源码)计算机毕业设计ssm电影分享网站
  • (附源码)计算机毕业设计SSM基于健身房管理系统
  • (使用vite搭建vue3项目(vite + vue3 + vue router + pinia + element plus))
  • (转)【Hibernate总结系列】使用举例
  • (转)LINQ之路
  • (转)创业家杂志:UCWEB天使第一步
  • .NET Core 网络数据采集 -- 使用AngleSharp做html解析
  • .net 重复调用webservice_Java RMI 远程调用详解,优劣势说明
  • .Net+SQL Server企业应用性能优化笔记4——精确查找瓶颈
  • .net遍历html中全部的中文,ASP.NET中遍历页面的所有button控件
  • .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)
  • @data注解_一枚 架构师 也不会用的Lombok注解,相见恨晚
  • @Responsebody与@RequestBody
  • @zabbix数据库历史与趋势数据占用优化(mysql存储查询)
  • [.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序...
  • [ABP实战开源项目]---ABP实时服务-通知系统.发布模式
  • [AIGC] 开源流程引擎哪个好,如何选型?
  • [Angularjs]asp.net mvc+angularjs+web api单页应用之CRUD操作
  • [bbk5179]第66集 第7章 - 数据库的维护 03
  • [BSGS算法]纯水斐波那契数列
  • [C/C++]数据结构 循环队列