el-table拖拽排序

36 篇文章 0 订阅

拖拽排序
在这里插入图片描述
拖拽后
在这里插入图片描述

在这里插入图片描述
设置row-key,这里暂时用的id,id是他们的唯一标识,不能用index,index是会变的
在这里插入图片描述

//安装组件
cnpm install sortablejs --save
//页面中引入
import Sortable from "sortablejs";
// 行拖拽
      rowDrop() {
       this.$nextTick(() => {
          this.sortable1 = Sortable.create(
            document.querySelector(".table-cont .el-table__body-wrapper tbody"),
            {
              handle: ".el-table__row",
              onEnd: ({ newIndex, oldIndex }) => {
                this.tableData.splice(
                  newIndex,
                  0,
                  this.tableData.splice(oldIndex, 1)[0]
                );
                var newArray = this.tableData.slice(0);
                this.tableData = [];
                this.$nextTick(function () {
                  this.tableData = newArray;
                  console.log('this.tableData',this.tableData)
                });
              },
            }
          );

        });
        
      },
//拖拽一下调用一次接口
// 行拖拽
      rowDrop() {
        this.$nextTick(() => {
          this.sortable1 = Sortable.create(
            document.querySelector(".table-cont .el-table__body-wrapper tbody"),
            {
              handle: ".el-table__row",
              onEnd: ({ newIndex, oldIndex }) => {
                let currRow=this.tableData.splice(oldIndex, 1)[0]
                this.tableData.splice(
                  newIndex,
                  0,
                  currRow
                );
                //拖拽一次调一次后台接口
                this.updateMove(newIndex,oldIndex,currRow)
              },
            }
          );

        });
      },
      updateMove(newIndex,oldIndex,currRow){
        //根据下标判断向上还是向下
        if(newIndex>oldIndex){//向下
          newIndex=newIndex-1;
        }else if(newIndex<oldIndex){//下
          newIndex=newIndex+1;
        }else{
          return; //没有变
        }
        let tableData=this.tableData;
        let insetData=tableData[newIndex];
        //调换顺序的两行
        console.log('insetData',insetData)
        console.log('currRow',currRow);
        //调用接口就看后台需要怎么传参
      }
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值