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

ElementUI-Table 表格实现行拖拽

1、引入依赖

npm install sortablejs --save

2、table表格设置

1、添加属性 ref="multipleTable"  row-key="id" @row-drag-end="handleDragEnd" 

2、添加列

<el-table-column width="50" align="center">
<template >
<i class="el-icon-rank drag-handle"></i>
</template>
</el-table-column>

 <el-table  v-loading="loading" :data="tableData" :highlight-selection-row="true"      :key="componentKey"ref="multipleTable"  row-key="id" @row-drag-end="handleDragEnd" ><el-table-column width="50" align="center"><template ><i class="el-icon-rank drag-handle"></i></template></el-table-column><el-table-column type="selection" width="55" align="center" /><el-table-column label="序号" type="index" align="center" min-width="5%" /></el-table>

显示效果如下

3、添加事件

1、必须是 mounted ,不可以使用created

2、解决无法二次拖拽、拖拽后表格数据无法重新渲染问题

补充:    :

1、key="componentKey"

2、// 重新初始化拖拽功能
this.$nextTick(() => {
this.initDragAndDrop();
});

3、  this.$set(this, 'materialList', newData);

<script>
import Sortable from "sortablejs";export default {name: "demo",data() {return {tableData: [], // 确保初始化为空数组sortable: null,componentKey: 1000,}},
created() {this.getList();},mounted() {this.initDragAndDrop();},methods: {initDragAndDrop() {// 获取表格的tbody元素const tbody = this.$refs.multipleTable.$el.querySelector('.el-table__body-wrapper tbody');// 初始化Sortablethis.sortable = new Sortable(tbody, {handle: '.drag-handle', // 指定拖拽手柄animation: 150, // 动画时间ghostClass: 'sortable-ghost', // 拖拽时的占位元素样式onEnd: (event) => {// 拖拽结束时触发this.handleDragEnd(event);}});console.log(this.sortable);},// 处理拖拽结束事件handleDragEnd(evt) {// 如果位置没有变化,不做处理if (evt.oldIndex === evt.newIndex) {return;}// 获取拖拽的行数据const draggingRow = this.materialList[evt.oldIndex];// 从原数组中移除并插入到新位置var newData = JSON.parse(JSON.stringify(this.materialList));;newData.splice(evt.newIndex, 0, newData.splice(evt.oldIndex, 1)[0]);console.log(newData)const nextRow = newData[evt.newIndex + 1];// 通知父组件数据已更新this.$set(this, 'materialList', newData);// 强制刷新组件以确保视图更新this.componentKey += 1;// 发送拖拽信息this.$emit('drag-end', {id: draggingRow.materialDeviceId,   // 拖拽行的IDoriginalIndex: evt.oldIndex, // 原始位置(0开始)originalPosition: evt.oldIndex + 1, // 原始位置(1开始)newIndex: evt.newIndex,      // 新位置(0开始)newPosition: evt.newIndex + 1,      // 新位置(1开始)moved: evt.oldIndex !== evt.newIndex // 是否发生了移动});// TODO + -console.log(`拖拽完成 - ID: ${draggingRow.materialDeviceId}, 从第${evt.oldIndex + 1}行移到第${evt.newIndex + 1}行`);console.log('下一个位置:' , nextRow.materialDeviceId , nextRow.orderNum , nextRow.name)// 重新初始化拖拽功能this.$nextTick(() => {this.initDragAndDrop();});},/** 查询素材分配列表 */getList() {listDevice(this.queryParams).then(response => {// 确保响应数据不为nullthis.tableData= response.rows ? response.rows : [];this.total = response.total ? response.total : 0;}).catch(error => {// 添加错误处理,防止数据为nullconsole.error('获取数据失败:', error);this.tableData= [];this.total = 0;});},}}  </script>

4、添加样式

<style scoped>

.el-icon-rank{

  font-size: x-large;

}

/* 拖拽相关样式 */
.cursor-move {
cursor: move;
color: #909399;
transition: color 0.2s;
}

.cursor-move:hover {
color: #409EFF;
}

/* 拖拽时的占位符样式 */
::v-deep .sortable-ghost {
opacity: 0.5;
background-color: #f5f7fa;
}

/* 选中行样式 */
::v-deep .sortable-chosen {
background-color: #e6f7ff !important;
}

/* 正在拖拽的行样式 */
::v-deep .dragging-row {
background-color: #fffbe6 !important;
}
</style>

5、拖拽效果

http://www.dtcms.com/a/418396.html

相关文章:

  • Logit论文阅读
  • 网站cms建设wordpress模仿app启动广告屏弹窗
  • 21. 一个像素对应多个纹素
  • 设备沉睡的“心跳”难题:BLE休眠后无法被手机唤醒的分析与优化
  • 【计算机基础】网络系列(二)TCP
  • 网站seo整站优化淘宝网站的建设
  • Python 自动化导出PDF表格:List、Dictionary、Pandas DataFrame和数据库实例演示
  • GPT-5-Codex Prompting Guide|提示词指南
  • PDF转图片工具:基于PyQt5的完整实现与深度解析
  • 万兴PDF专家安装教程(附安装包)万兴PDF免费版下载详细安装图文教程
  • Codeforces Round 1052 (Div. 2) C. Wrong Binary Searchong Binary Search
  • 网站开始怎么做的做网站用php还是html
  • 算法基础篇(7)双指针
  • 从零开始学华为:Console口连接设备
  • 华为bgp路由的各种控制和团体属性及orf使用案例
  • 网站中的表单怎么做吉林网页制作公司
  • 【开题答辩全过程】以 Python基于大数据的四川旅游景点数据分析与可视化为例,包含答辩的问题和答案
  • MySQL复合查询(重点)
  • Java面试揭秘:从Spring Boot到微服务的技术问答
  • 【项目】自然语言处理——情感分析 <上>
  • 生态碳汇涡度相关监测与通量数据分析实践技术应用
  • 【linux内核驱动day04】
  • 安全笔记(一)
  • 17-Language Modeling with Gated Convolutional Networks
  • ES启用Xpack,配置ssl证书
  • 网站无收录的原因湖南长沙微信平台号
  • k8s-pod的启动
  • RHCA - CL260 | Day11:管理存储集群
  • 多线程环境下处理Flask上下文问题的文档
  • 第四部分:VTK常用类详解(第95章 vtkLegendBoxActor图例框演员类)