vue3 + Element plus + Ts + Sortable.js 實現(xiàn)table拖拽排序

  • 效果圖如下:
image.png

1. 安裝Sortable.js插件

使用 npm install sortable.js --save 或者 yarn add sortable.js

2. 引入sortable.js

import Sortable from 'sortablejs'

3. 項目中使用sortable.js

image.png
<template #icon>
    <el-icon class="move-icon cursor-pointer" style="font-size: 20px; cursor: pointer; margin-top: 5px"><Rank /></el-icon>
</template>
image.png
  • 主要代碼:
const initSortable = () => {
  const el1 = tableEl1.value.$el.querySelector('.el-table__body tbody')
  Sortable.create(el1, {
    handle: '.move-icon',
    onEnd: ({ newIndex, oldIndex }) => {
      const arr = state.sewList
      const currRow = arr.splice(oldIndex, 1)[0]
      arr.splice(newIndex, 0, currRow)
      state.sewList = []
      nextTick(() => {
        state.sewList = arr
      })
    }
  })
}
image.png
  • 掛載之后調(diào)用上圖方法:


    image.png

【參考文檔】

  1. https://www.bbsmax.com/A/Vx5MKlMYzN/
  2. Sortable簡單好用的拖拽排序工具
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容