elementui 实现表格拖拽功能

 Element-UI Table组件目前没有拖拽的功能,我们可以通过引入sortable包可以实现拖拽功能。

步骤如下:

1.安装sortable.js的包

   npm install sortable.js --save

2.代码中引入sortable.js

import Sortable from 'sortablejs'

2.在vue文件中添加方法

  //行拖拽
    rowDrop() {
      let tbody = document.querySelector('.el-table__body-wrapper tbody')
      let _this = this
      Sortable.create(tbody, {
        onEnd({ newIndex, oldIndex }) {
          const currRow = _this.tableData.splice(oldIndex, 1)[0]
          _this.tableData.splice(newIndex, 0, currRow)
        }
      })
    },
    

    //列拖拽
    columnDrop() {
      let wrapperTr = document.querySelector('.el-table__header-wrapper tr')
      this.sortable = Sortable.create(wrapperTr, {
        animation: 180,
        delay: 0,
        onEnd: evt => {
          let oldItem = this.dropCol[evt.oldIndex]
          this.dropCol.splice(evt.oldIndex, 1)
          this.dropCol.splice(evt.newIndex, 0, oldItem)
        }
      })
    }
原文地址:https://www.cnblogs.com/jiayuexuan/p/15631954.html