el-table拖拽排序

拖拽排序
el-table拖拽排序_第1张图片
拖拽后
el-table拖拽排序_第2张图片

el-table拖拽排序_第3张图片
设置row-key,这里暂时用的id,id是他们的唯一标识,不能用index,index是会变的
el-table拖拽排序_第4张图片

//安装组件
cnpm install sortablejs --save
//页面中引入
import Sortable from "sortablejs";
// 行拖拽
      rowDrop() {
       this.$nextTick(() => {
          this.sortable1 = Sortable.create(
            document.querySelector(".table-cont .el-table__body-wrapper tbody"),
            {
              handle: ".el-table__row",
              onEnd: ({ newIndex, oldIndex }) => {
                this.tableData.splice(
                  newIndex,
                  0,
                  this.tableData.splice(oldIndex, 1)[0]
                );
                var newArray = this.tableData.slice(0);
                this.tableData = [];
                this.$nextTick(function () {
                  this.tableData = newArray;
                  console.log('this.tableData',this.tableData)
                });
              },
            }
          );

        });
        
      },
//拖拽一下调用一次接口
// 行拖拽
      rowDrop() {
        this.$nextTick(() => {
          this.sortable1 = Sortable.create(
            document.querySelector(".table-cont .el-table__body-wrapper tbody"),
            {
              handle: ".el-table__row",
              onEnd: ({ newIndex, oldIndex }) => {
                let currRow=this.tableData.splice(oldIndex, 1)[0]
                this.tableData.splice(
                  newIndex,
                  0,
                  currRow
                );
                //拖拽一次调一次后台接口
                this.updateMove(newIndex,oldIndex,currRow)
              },
            }
          );

        });
      },
      updateMove(newIndex,oldIndex,currRow){
        //根据下标判断向上还是向下
        if(newIndex>oldIndex){//向下
          newIndex=newIndex-1;
        }else if(newIndex

你可能感兴趣的:(vue,js,javascript,前端,element)