vue 中对 多组 div 进行拖拽排序

368 阅读1分钟

1. 安装步骤

  • Sortable.js是一款轻量级的拖放排序列表的js插件(虽然体积小,但是功能很强大)
  • npm install sortablejs --save
  • vue 在 main.js 中引入 import Sortable from "sortablejs"
  • 在表格上添加row-key
  • 官方Demo:rubaxa.github.io/Sortable/ **2.使用步骤
  • 子最外层需套用一个div,指定类名 image.png
  • 在 mounted 中进行调用 image.png
  • 方法

image.png

 const tbody = document.getElementById('sortableCard')
  const that = this
  Sortable.create(tbody, {
    animation: 1000,
    sort: true,
    onEnd(evt) {
      that.submitForm.goods.splice(evt.newIndex, 0, that.submitForm.goods.splice(evt.oldIndex, 1)[0])
      var newArray = that.submitForm.goods.slice(0)
      that.submitForm.goods = []
      that.$nextTick(() => {
        that.submitForm.goods = newArray
      })
    }
  })
},
```