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

uni-app使用movable-area 实现数据的拖拽排序功能

文档地址

template部分
<movable-area :style="getAreaStyle"><movable-view class="table-row" v-for="v,i in move.list":key="v.id":y="v.y"@change="handle_moving"direction="vertical"@touchstart="handle_dragstart(i,v)" @touchend="handle_dragend(i,v)"@longpress="handleLongpress(v)" :disabled="move.disabled"><view class="table-cell">{{i+1}}</view><view class="table-cell">选择物料</view></movable-view>
</movable-area>
.table-cell,.table-row{height:50rpx;
}
.table-row{width:100%;display: flex;
}
.table-cell{width:200rpx;
}

 注意使用movable-area时需要设置高度和宽度 否则默认10

js部分
const move = reactive({list:[],disabled: true,activeIndex: -1,moveToIndex: -1,oldIndex: -1,tempDragInfo: {y: ''},cloneList: [],longpress: true,})// 获取位置const getPosition_y = (index) => {return index * 25}const getAreaStyle = computed(() => {return {width: '100%',height: move.list.length * 25 + 'px'}})// 初始化列表const initList = (list = []) => {const newList = JSON.parse(JSON.stringify(list));move.list = newList.map((item, index) => {return {...item,y: getPosition_y(index)}})move.cloneList = JSON.parse(JSON.stringify(move.list));}// 开始拖拽const handle_dragstart = (i,v) => {if(!v.id){return false;}move.activeIndex = i;move.oldIndex = i;}// 拖拽结束const handle_dragend = (i,v) => {if(!v.id){return false;}if (move.disabled) return;if (move.moveToIndex != -1 && move.activeIndex != -1 && move.activeIndex != move.moveToIndex) {move.cloneList.splice(move.moveToIndex, 0, ...move.cloneList.splice(move.activeIndex, 1));} else {move.list[move.activeIndex]['y'] = move.tempDragInfo.y;}initList(move.cloneList)move.activeIndex = -1;move.oldIndex = -1;move.moveToIndex = -1;move.disabled = true;}// 移动const handle_moving = (e) => {console.log({e})if (e.detail.source !== 'touch') return;let y = e.detail.y;move.tempDragInfo.y = y;const currentY = Math.floor((y + 12.5) / 25)move.moveToIndex = Math.min(currentY, move.list.length - 1);if (move.oldIndex != move.moveToIndex && move.oldIndex != -1 && move.moveToIndex != -1) {const newList = JSON.parse(JSON.stringify(move.cloneList));let splicItem = newList.splice(move.activeIndex, 1)[0]newList.splice(move.moveToIndex, 0, splicItem);move.list.forEach((item, index) => {if (index != move.activeIndex) {const itemIndex = newList.findIndex(val => val?.id === item?.id);item['y'] = getPosition_y(itemIndex);}});move.oldIndex = move.moveToIndex;}}const handleLongpress = (v) => {if(!v.id){return false}move.disabled = false;}

 

http://www.lryc.cn/news/472893.html

相关文章:

  • 如何设置使PPT的画的图片导出变清晰
  • 和鲸科技 CEO 范向伟受邀揭牌启动南京大学 2024 级大学生人工智能素养大赛
  • NewStarCTF2024-Week4-Web-WP
  • Java学习Day56:暴打舔狗!(SpringBoot)
  • RSA加密算法实现
  • 大数据新视界 -- 大数据大厂之优化大数据计算框架 Tez 的实践指南
  • java 中 List<T> 类型数据在 postgreSql 数据库中存储
  • 公共命名空间,2024年10月的笔记
  • frida脚本,自动化寻址JNI方法
  • ‌MySQL中‌between and的基本用法‌
  • Ceph 存储系统全解
  • C# ftp帮助类 项目实战优化版
  • 栈和队列相关|有效的括号|用队列实现栈|用栈实现队列|设计循环队列(C)
  • 云原生后端开发教程
  • TortoiseSVN小乌龟下载安装(Windows11)
  • Android adb命令获取设备id
  • Skywalking教程一
  • React中管理state的方式
  • 服务器数据恢复—RAID5阵列中部分成员盘重组RAID5阵列后如何恢复原raid5阵列数据?
  • 【Linux】文件切割排序 cut sort
  • 零售EDI:HornBach EDI 项目案例
  • SpringBoot 集成RabbitMQ 实现钉钉日报定时发送功能
  • 基于java ssm springboot女士电商平台系统源码+文档设计
  • Matlab数字信号处理——基于改进小波变换的图像去噪方法(7种去噪算法)
  • leetcode hot100【LeetCode 70. 爬楼梯】java实现
  • Java异常2
  • 2024熵密杯初始题2
  • echarts属性之title
  • VUE errolog, vue 错误集
  • 驱动开发系列13 - Linux tasklet用法介绍