sortable实现elementui中表格拖拽排序

1.安装sortable

npm install sortablejs

2.在页面中使用

html

<el-button
              type="text"
              icon="el-icon-d-caret"
              v-show="!sort"
              size="small"
              class="move"
            ></el-button>

js

<script>
import Sortable from "sortablejs";
export default {
 mounted() {
    this.dragSort()
    
  },
methods: {
    dragSort() {
      const el = this.$refs.singleTable.$el.querySelectorAll(
        ".el-table__body-wrapper > table > tbody"
      )[0];
      this.sortable = Sortable.create(el, {
        handle: ".move",
        onEnd: e => {
          //onEnd是结束拖拽时触发,onUpdate是列表内元素顺序更新的时候触发,更多请看文末配置项
          //e.oldIndex为拖动一行原来的位置,e.newIndex为拖动后新的位置
          const targetRow = this.tableData.splice(e.oldIndex, 1)[0];
          this.tableData.splice(e.newIndex, 0, targetRow);
          let dragId = this.tableData[e.newIndex].showNo; //拖动行的showNo
          let aaaaa = this.tableData[e.newIndex].jnlNo; //拖动行的jnlNo
          let oneId, twoId;
          //拖动行的前一行
          if (this.tableData[e.newIndex - 1]) {
            oneId = this.tableData[e.newIndex - 1].showNo;
          } else {
            oneId = "";
          }
          //拖动行的后一行
          if (this.tableData[e.newIndex + 1]) {
            twoId = this.tableData[e.newIndex + 1].showNo;
          } else {
            twoId = "";
          }
          console.log("拖动行:" + dragId);
          console.log("拖动行:" + aaaaa);
          console.log("前一行:" + oneId);
          console.log("后一行:" + twoId);
          //然后就可以Sortable发送请求了......
          // this.getList()
        }
      });
    },
}
}
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值