element-ui table 行拖拽

引入sortable包

npm i sortable.js --save

使用

import Sortable from 'sortablejs'

初始化

mounted() {
    // 阻止默认行为
  document.body.ondrop = function(event) {
    event.preventDefault()
    event.stopPropagation()
  }
    this.rowDrop()
},

排序方法

rowDrop() {
  const tbody = document.querySelector('.task-modal-wrapper .el-table__body-wrapper tbody')
  const _this = this
  Sortable.create(tbody, {
      onEnd({ newIndex, oldIndex }) {
          const currRow = _this.crowdList.splice(oldIndex, 1)[0]
          _this.crowdList.splice(newIndex, 0, currRow)
    }
  })
},

注意: table 需要指定row-key,否则会乱序。这也是后端突然改字段,导致我找一中午排序bug问题。。。😅😅😓

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容