- 安装依赖
npm install sortablejs
- 引入
import Sortable from 'sortablejs';
- 在页面初始生命周期内使用
const el = document.querySelector('.sort-el-table .el-table__body-wrapper table tbody');
if (el) {
Sortable.create(el, {
animation: 150,
handle: '.drag-icon',
onEnd: async function (evt) {
let newIndex = evt.newIndex
let oldIndex = evt.oldIndex
if (newIndex !== oldIndex) {
const updatedData = [...dataList];
const [movedItem] = updatedData.splice(oldIndex, 1);
updatedData.splice(newIndex, 0, movedItem);
dataList= [];
await nextTick();
updatedData.forEach((item, index) => {
item.sort = index;
});
dataList = updatedData;
}
}
});
}