Vue Elementui 使用sortablejs实现表格拖拽

原创
2021/01/28 11:06
阅读数 3.3K

Sortable.js是一款轻量级的拖放排序列表的js插件

安装Sortable.js

npm install sortablejs --save

引入表格拖拽依赖

 import Sortable from 'sortablejs'

给el-table添加属性

row-key="id"

添加行拖拽方法

      //行拖拽
      rowDrop() {
        //获取当前表格
        const tbody = document.querySelector('.el-table__body-wrapper tbody')
        const that = this
        //newIndex -- 新下标 oldIndex -- 旧下标
        Sortable.create(tbody, {
          onEnd({ newIndex, oldIndex }) {
            //移除原来的数据
            const currRow = that.tableData.splice(oldIndex, 1)[0]
            //移除原来的数据并插入新的数据
            that.tableData.splice(newIndex, 0, currRow)
          }
        })
      }

添加mounted

    mounted() {
      this.rowDrop()
    }

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部