Vue el-table实现拖曳调整顺序

31 篇文章 3 订阅

1661151235812

el-table 想要实现拖曳调整表格顺序的功能,需要借助第三方插件Sortablejs来实现

首先需要安装:

npm install --save sortablejs

在界面中调用:

import Sortable from 'sortablejs'

el-table部分:

其中 row-key 是必须的并且是唯一的,class-name="allowDrag"可以在每个字段上都加上

<el-table
  border
  :data="strategyList"
  ref="strategyTable"
  row-key="id"
  height="400"
>
  <el-table-column label="执行顺序" align="center" type="index" width="80px" class-name="allowDrag"/>
  <el-table-column label="策略标识" align="center" prop="strategyMark" min-width="80px" class-name="allowDrag"/>
  <el-table-column
    label="策略名称"
    align="center"
    prop="strategyName"
    :show-overflow-tooltip="true"
    class-name="allowDrag"
  />
  <el-table-column
    label="关联载荷"
    align="center"
    prop="loadName"
    :show-overflow-tooltip="true"
    class-name="allowDrag"
  />
</el-table>

方法部分:

methods: {
   // 拖曳表格
   dragTable() {
     const el = this.$refs.strategyTable.$el.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]
     const sortable = Sortable.create(el, {
       handle: '.allowDrag',
       onEnd: evt => {
         const targetRow = this.strategyList.splice(evt.oldIndex, 1)[0]
         this.strategyList.splice(evt.newIndex, 0, targetRow)
         // 自己的业务逻辑部分
         for (let index in this.strategyList) {
           this.strategyList[index].sort = parseInt(index) + 1
           this.strategyList[index].queue = parseInt(index) + 1
         }
       }
     })
   }
}

调用:

如果是正常界面,在mounted中调用

mounted() {
	this.dragTable()
}

如果是子组件,在子组件打开时并且子组件加载完成后调用:

watch: {
  sortData: {
    handler(n, o) {
      if (this.sortData.open) {
        this.getList()
        this.$nextTick(() => {
          this.dragTable()
        })
      }
    },
    deep: true
  }
},
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值