【Sortable】vue2 使用 Sortable实现 el-table 拖拽

安装

 npm install sortablejs --save

引入

import Sortable from 'sortablejs';

 挂载

mounted() {
  this.$nextTick(() => {
    this.rowDrop();
  });
},

js代码

rowDrop() {
  const el = document.querySelectorAll(".draggable-table .el-table__body-wrapper > table > tbody")[0];
  const vm = this;

  Sortable.create(el, {
    disabled: false, // 拖拽是否可用
    ghostClass: 'sortable-ghost', //拖拽样式
    animation: 150, // 拖拽延时
    onEnd: function (e) { // 拖拽结束时触发
      let arr = vm.goodList;  //需要拖拽的数据
      arr.splice(e.newIndex, 0, arr.splice(e.oldIndex, 1)[0]);   // 数据处理,获取最新的表格数据
      vm.$nextTick(function () {
        vm.goodList = arr;  //赋值
      })
    },
  });

},

 html代码

<el-table ref="dataTable" class="draggable-table" v-loading="loading" :data="goodList" :row-key="row => { return row.id}" border
          @selection-change="handleSelectionChange">

实现后

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值