Vue3+El-Plus實現(xiàn)表格行拖拽功能完整代碼
一: 安裝sortablejs
npm install sortablejs --save

二: 頁面使用
這里項目只需要一個地方用到,就沒有封裝成組件,直接在用到的.vue文件中寫了。
在使用的 .Vue 文件中導入
import { default as Sortable, SortableEvent } from "sortablejs";看下圖:
注意事項:el-table需要配置 row-key 且保持唯一性,不然會出現(xiàn)排序不對的情況

rowDrag方法:
// 行拖拽
const rowDrag = function () {
// 要拖拽元素的父容器
const tbody = document.querySelector(
".draggable .el-table__body-wrapper tbody"
);
if (!tbody) return;
Sortable.create(tbody as HTMLElement, {
// 可被拖拽的子元素
draggable: ".draggable .el-table__row",
onEnd(event: SortableEvent) {
if (event.oldIndex !== undefined && event.newIndex !== undefined) {
const currRow = tableList.splice(event.oldIndex, 1)[0];
tableList.splice(event.newIndex, 0, currRow);
}
},
});
};效果如下:
圖標不能使用,自己可以更換了,這個無所謂啦。

三 : 下面是封裝成組件使用
還不夠完善,可以根據(jù)自己的需求進行修改。
dragTable 組件代碼如下:
<template>
<div class="t-table" ref="table_ref">
<el-table
class="draggable"
ref="tables"
:data="state.tableData"
row-key="id"
border
fit
highlight-current-row
style="width: 100%"
>
<!-- 拖拽 -->
<el-table-column align="center" label="" width="80">
<template #default="{}">
<i-ep-fold style="cursor: move" />
</template>
</el-table-column>
<template v-for="item in state.tableHeaders" :key="item.id">
<el-table-column
:property="item.property"
:min-width="item.width"
align="center"
show-overflow-tooltip
>
<template #header>
<p style="margin: 0; display: flex; justify-content: center">
{{ item.label }}
</p>
</template>
</el-table-column>
</template>
</el-table>
</div>
</template>
<script setup lang="ts" name="dragTable">
import { ref, watch, reactive, onMounted } from "vue";
import { default as Sortable, SortableEvent } from "sortablejs";
const props = defineProps<{
// 列表數(shù)據(jù)
table: any;
// 表頭數(shù)據(jù)
headers: {
id: string;
property: string;
width: string;
label: string;
show: boolean;
}[];
}>();
// 初始化數(shù)據(jù)
const state = reactive({
tableData: props.table,
tableHeaders: props.headers,
});
// 獲取el-table ref
const tables: any = ref<HTMLElement | null>(null);
// 獲取t-table ref
const table_ref: any = ref<HTMLElement | null>(null);
// 拋出事件 在 應用的.Vue 文件做相應的操作
const emits = defineEmits(["rowSort"]);
// 監(jiān)聽移動的 表格數(shù)據(jù) 重新賦值
watch(
() => props.table,
(val) => {
console.log("watch val", val);
state.tableData = val;
},
{ deep: true }
);
onMounted(() => {
console.log("state.tableData >>>", state.tableData);
console.log("state.tableHeaders >>>", state.tableHeaders);
initSort();
});
// 行拖拽
const initSort = () => {
const el = table_ref.value.querySelector(".el-table__body-wrapper tbody");
// console.log('3333', el)
Sortable.create(el, {
animation: 150, // 動畫
onEnd: (event: SortableEvent) => {
if (event.oldIndex !== undefined && event.newIndex !== undefined) {
const curRow = state.tableData.splice(event.oldIndex, 1)[0];
state.tableData.splice(event.newIndex, 0, curRow);
emits("rowSort", state.tableData);
}
},
});
};
</script>
在 .Vue 文件中的使用 及 頁面父傳子的數(shù)據(jù)
1. 導入組件
import dragTable from "@/components/DragTable/index.vue";
2. 使用組件
<dragTable :table="tableList" :headers="initialHeaders" @rowSort="handleRowSort" />
3. 在 .Vue 文件里的數(shù)據(jù)及方法
列表數(shù)據(jù)就根據(jù)自己后端返回的數(shù)據(jù)直接傳遞就行。
表頭數(shù)據(jù)如下:
let initialHeaders = reactive([
{
id: "1",
property: "id",
width: "88",
label: "ID",
show: true,
},
{
id: "2",
property: "name",
width: "121",
label: "111",
show: true,
},
{
id: "3",
property: "thumb",
width: "139",
label: "222",
show: true,
},
{
id: "4",
property: "icon",
width: "99",
label: "333",
show: true,
},
]);handleRowSort() 這個事件每次更改列表的排序就會觸發(fā),在使用組件的 .Vue 文件上就能進行一些相應的需求操作
const handleRowSort = () => {
console.log("應用的.Vue 文件做相應的操作");
};組件使用的效果圖如下:
樣式可以根據(jù)自己需求進行調整,這個小問題啦,功能實現(xiàn)就好。

總結
到此這篇關于Vue3+El-Plus實現(xiàn)表格行拖拽功能的文章就介紹到這了,更多相關Vue3+El-Plus表格行拖拽功能內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue props對象validator自定義函數(shù)實例
今天小編就為大家分享一篇vue props對象validator自定義函數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
解決Vue路由導航報錯:NavigationDuplicated:?Avoided?redundant?navig
這篇文章主要給大家介紹了關于解決Vue路由導航報錯:NavigationDuplicated:?Avoided?redundant?navigation?to?current?location的相關資料,這是最近做項目時候遇到的一個問題,現(xiàn)將解決辦法分享出來,需要的朋友可以參考下2023-01-01
vue element el-form 多級嵌套驗證的實現(xiàn)示例
本文主要介紹了vue element el-form 多級嵌套驗證的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08
Vue3監(jiān)聽reactive對象中屬性變化的方法
在 Vue 3 中,如果你想監(jiān)聽 reactive 對象中的某個屬性發(fā)生的變化,你可以使用 watch 函數(shù)進行監(jiān)聽,watch 函數(shù)允許你觀察 reactive 對象的某個屬性或者整個對象,所以本文給大家介紹了Vue3監(jiān)聽reactive對象中屬性變化的方法,需要的朋友可以參考下2024-08-08

