sortable.js 对el-table表格拖拽排序

1,235 阅读1分钟

1. 安装步骤

  • Sortable.js是一款轻量级的拖放排序列表的js插件(虽然体积小,但是功能很强大)
  • npm install sortablejs --save
  • vue 在 main.js 中引入 import Sortable from "sortablejs"
  • 在表格上添加row-key
  • 官方Demo:rubaxa.github.io/Sortable/

image.png image.png

  • sort_number 是我们后台返回的唯一值,所以当亲们用的时候用后台返回唯一值 id等就行 2. 使用方法
mounted() {
  this.rowDrop()
},
methods:{
    rowDrop() {
      const tbody = document.querySelector('.el-table__body-wrapper tbody')
      const _this = this
      Sortable.create(tbody, {
        animation: 1000,
        onEnd({ newIndex, oldIndex }) {
          //oldIndex为拖动一行原来的位置,newIndex为拖动后新的位置
          const currRow = _this.dataList.splice(oldIndex, 1)[0]
          _this.dataList.splice(newIndex, 0, currRow)
        }
       })
    }
}

就可以简单实现表格拖拽效果