使用"sortablejs实现拖拽 参考官网http://www.sortablejs.com/index.html
安装
npm install sortablejs --save
<template>
<el-table :data="TableData" style="width: 100%" class="table-container">
<el-table-column
v-for="(item, index) in oldList"
:key="`col_${index}`"
:prop="newList[index].prop"
:label="item.label"
>
</el-table-column>
</el-table>
</template>
<script setup >
import Sortable from "sortablejs";
const TableData = ref([
{
name: "张三",
phoneNumber: "11187111121",
IdCard: "3219111111111164",
},
]);
const dropCol= ref([
{ label: '人员姓名' , prop: 'name' },
{ label: '手机号' , prop: 'phoneNumber' },
{ label: '身份证号' , prop: 'IdCard' },
])
const oldList = ref(JSON.parse(JSON.stringify(dropCol.value)))
const newList = ref(JSON.parse(JSON.stringify(dropCol.value)))
onMounted(() => {
//列拖拽
const wrapperTr = document.querySelector(".el-table__header-wrapper tr");
Sortable.create(wrapperTr, {
animation: 180,
delay: 0,
onEnd: (evt) => {
const oldItem = dropCol.value[evt.oldIndex];
dropCol.value.splice(evt.oldIndex, 1);
dropCol.value.splice(evt.newIndex, 0, oldItem);
},
});
})
</script>