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

vuedraggable Sortable.js 实现拖拽排序功能VUE3

一、页面效果:通过拖拽☰完成拖拽并立即更新顺序  

二、方法:

2-1:引用:

npm install vuedraggable@next

2-2:代码:

<template><div class="main" v-loading="loading"><table class="order_write order_table_top"><thead></thead><tbody><tr><th>{{ t('common.当前顺序') }}</th><td colspan="3">{{ seqArray }}</td></tr><tr><th>{{ t('common.中心管理') }}</th><td colspan="3"></td></tr><tr><th>{{ t('common.顺序') }}</th><td><!-- 拖拽容器 --><div ref="sortableContainer" class="sortable-container"><div v-for="(item, index) in tableList" :key="item.CENTER_SEQ" class="draggable-item"><span class="drag-handle">☰</span><el-input readonly class="iptBox1":value="`${item.CENTER_NM}`" /></div></div></td></tr></tbody></table><div class="btn-box"><el-button type="primary" @click="handleSave" size="small">{{ t('btn.save') }}</el-button><el-button type="primary" plain @click="handleClose">{{ t('btn.close') }}</el-button></div></div>
</template><script setup>
import { ref, onMounted } from 'vue';
import Sortable from 'sortablejs';
import { useI18n } from 'vue-i18n';
import { toast } from '~/composables/util';
import { closePopup } from '~/composables/popupManager';
import { apiService } from '~/api/setting';const { t } = useI18n();const loading = ref(false);
const tableList = ref([]);
const seqArray = ref([]);
const sortableContainer = ref(null);// 创建广播通道
const channel = new BroadcastChannel('vue-apps-channel');// 获取列表数据
const getList = async () => {loading.value = true;try {await apiService.getList().then((res) => {if (res.code === 200) {tableList.value = JSON.parse(JSON.stringify(res?.data));if (tableList.value?.length > 0) {seqArray.value = tableList.value.map((item) => item.CENTER_SEQ);}} else {toast(res.msg || t('common.error'), 'error');}});} finally {loading.value = false;}
};// 初始化 Sortable.js
onMounted(() => {getList();// 初始化拖拽功能new Sortable(sortableContainer.value, {handle: '.drag-handle', // 指定拖拽手柄animation: 150, // 动画效果onEnd: (evt) => {const { oldIndex, newIndex } = evt;// 更新 tableList 的顺序const movedItem = tableList.value.splice(oldIndex, 1)[0];tableList.value.splice(newIndex, 0, movedItem);// 更新 seqArrayseqArray.value = tableList.value.map((item) => item.CENTER_SEQ);console.log('拖拽结束,当前顺序:', seqArray.value);},});
});// 保存排序结果
const handleSave = async () => {try {if (!seqArray.value.length) {toast(t('common.noDataToSave'), 'error');return;}// 更新排序数据const currentData = {CENTER_SEQ: seqArray.value,};await centerStoreService.sortCenterStore(currentData).then((res) => {if (res.code === 200) {toast(t('common.updateSuccess'));} else {toast(res.msg, 'error');}});} catch (error) {toast(t('common.error'), 'error');} finally {// 关闭窗口handleClose();}
};// 关闭窗口
const handleClose = () => {closePopup('centerSort');channel.postMessage({ type: 'popup', message: false });
};
</script><style scoped>
.main {width: 90%;padding: 0 8px;color: #313131;
}.order_write {width: 80%;margin: 20px auto;background-color: #fff;width: 100%;
}.order_write th,
.order_write td {border: 1px solid #e5e5e5;padding: 10px;border-collapse: collapse;font-size: 12px;
}.order_write th {width: 10%;font-size: 12px;color: #444;background-color: #fbfbfbee;
}.order_write td {width: 40%;
}.order_table_top {border-top: 1px solid #e5e5e5;
}.sortable-container {display: flex;flex-direction: column;
}.draggable-item {display: flex;align-items: center;margin-bottom: 5px;
}.drag-handle {margin-right: 10px;font-size: 16px;color: #aaa;cursor: move;
}.iptBox1 {width: 95%;
}.btn-box {display: flex;justify-content: center;margin-bottom: 2vh;
}
</style>

http://www.xdnf.cn/news/1510.html

相关文章:

  • 4.2 Prompt工程与任务建模:高效提示词设计与任务拆解方法
  • 【Python网络爬虫实战指南】从数据采集到反反爬策略
  • HTML5 服务器发送事件 (Server-Sent Events):实现网页自动获取服务器更新
  • [论文阅读]REPLUG: Retrieval-Augmented Black-Box Language Models
  • 嵌入式:Linux系统应用程序(APP)启动流程概述
  • Qt 处理 XML 数据
  • 音视频之H.265/HEVC环路后处理
  • 国产紫光同创FPGA视频采集转SDI编码输出,基于HSSTHP高速接口,提供2套工程源码和技术支持
  • 模拟电路方向主要技术要点和大厂真题解析
  • 算法时代的“摩西十诫”:AI治理平台重构数字戒律
  • 理解npm的工作原理:优化你的项目依赖管理流程
  • express的中间件,全局中间件,路由中间件,静态资源中间件以及使用注意事项 , 获取请求体数据
  • 经验分享 | 如何高效使用 `git commit --amend` 修改提交记录
  • Android移动应用开发入门示例:Activity跳转界面
  • 【数据结构】Map与Set结构详解
  • React-组件通信
  • 【网络原理】从零开始深入理解TCP的各项特性和机制.(一)
  • 机器学习漏洞大汇总——利用机器学习服务
  • Scrapy框架爬虫官网的学习
  • 放爱心烟花
  • # 构建和训练一个简单的CBOW词嵌入模型
  • GCoNet+:更强大的团队协作 Co-Salient 目标检测器 2023 GCoNet+(总结)
  • 【Deepseek学习大模型推理】MOONCAKE: A KVCache-centric Architecture调度(中)调度
  • win10 快速搭建 lnmp+swoole 环境 ,部署laravel6 与 swoole框架laravel-s项目3
  • 代码随想录算法训练营第二十五天
  • 卡方检验(Chi-square test)
  • 奇安信春招面试题
  • 在线测试来料公差
  • Java工厂模式解析:三种实现与最佳实践
  • 03-Java入门-JDK的安装和下载