sortable.js 959 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import Sortable from 'sortablejs'
  2. /**
  3. * 组件必须提供一个sortableEnd方法用于拖拽数据更新
  4. */
  5. export default {
  6. data() {
  7. return {
  8. sortable: null
  9. }
  10. },
  11. methods: {
  12. // 初始化拖拽排序
  13. initSortable() {
  14. // 获取表格的tbody元素
  15. const el = this.$refs.elTable.$el.querySelectorAll(
  16. '.el-table__body-wrapper > table > tbody'
  17. )[0]
  18. this.sortable = new Sortable(el, {
  19. // 拖拽时的类名
  20. ghostClass: 'sortable-ghost',
  21. // 拖拽动画时间
  22. animation: 150,
  23. // 结束拖拽时的回调
  24. onEnd: this.sortableEnd
  25. })
  26. },
  27. // 重新实例化sortable
  28. refreshSortable() {
  29. this.$nextTick(() => {
  30. if (this.sortable) {
  31. this.sortable.destroy()
  32. }
  33. this.initSortable()
  34. })
  35. }
  36. },
  37. // 销毁实例
  38. beforeDestroy() {
  39. if (this.sortable) {
  40. this.sortable.destroy()
  41. }
  42. }
  43. }