使用Vue3優(yōu)雅地實(shí)現(xiàn)表格拖動(dòng)排序
在 Vue.js 中主要通過(guò)第三方庫(kù)實(shí)現(xiàn)表格拖動(dòng)排序功能,其中最常用的庫(kù)是 SortableJS。
1. 安裝 sortablejs
npm i sortablejs --save
2. 初始化表格數(shù)據(jù)
這里使用 ElementPlus 作為前端組件庫(kù)
注意要給 el-table 綁定 row-key 屬性,并設(shè)置 ref。
<el-table
ref="tableRef"
border
:data="tableData"
:row-key="(row) => row.id"
style="width: 100%"
>
<el-table-column type="index" label="序號(hào)" width="55" />
<el-table-column prop="id" label="id" width="100"> </el-table-column>
<el-table-column prop="age" label="年齡" width="100"> </el-table-column>
<el-table-column prop="name" label="姓名" width="120"> </el-table-column>
</el-table>
js 部分
<script setup>
import { ref, onMounted, onBeforeUnmount } from "vue";
const tableRef = ref();
// 表格數(shù)據(jù)
const tableData = ref([
{ id: 110, age: 20, name: "李白" },
{ id: 120, age: 21, name: "杜甫" },
{ id: 130, age: 22, name: "白居易" },
{ id: 130, age: 27, name: "知否君" },
]);
</script>
3. 使用 sortablejs
3.1 導(dǎo)入 sortablejs
import Sortable from "sortablejs";
3.2 使用 sortablejs 創(chuàng)建拖動(dòng)表格方法
const sortableRow = ref(null);
const sortableColumn = ref(null);
// 拖動(dòng)表格行
const onSortableRow = () => {
sortableRow.value = Sortable.create(
tableRef.value.$el.querySelector(".el-table__body-wrapper tbody"),
{
animation: 150,
onEnd: ({ newIndex, oldIndex }) => {
// 獲取新的行位置
const currRow = tableData.value.splice(oldIndex, 1)[0];
// 重新排列行位置
tableData.value.splice(newIndex, 0, currRow);
},
}
);
};
// 拖動(dòng)表格列
const onSortableColumn = () => {
sortableColumn.value = Sortable.create(
tableRef.value.$el.querySelector(".el-table__header-wrapper thead tr"),
{
animation: 150,
onEnd: ({ newIndex, oldIndex }) => {
// 1.獲取表格列
const table = tableRef.value;
const oldColumns = table.store.states.columns;
// 2. 重新排列列的順序
const newColumns = [...oldColumns.value];
const movedColumn = newColumns.splice(oldIndex, 1)[0];
newColumns.splice(newIndex, 0, movedColumn);
oldColumns.value = newColumns;
},
}
);
};
3.3 在周期函數(shù)中調(diào)用方法
// 組件掛載之后執(zhí)行
onMounted(() => {
onSortableRow();
onSortableColumn();
});
3.4 銷(xiāo)毀實(shí)例
// 銷(xiāo)毀
onBeforeUnmount(() => {
if (sortableRow.value) {
sortableRow.value.destroy();
sortableRow.value = null;
}
if (sortableColumn.value) {
sortableColumn.value.destroy();
sortableColumn.value = null;
}
});
4. 完整代碼
<template>
<div>
<el-card style="width: 30%">
<el-table
ref="tableRef"
border
:data="tableData"
:row-key="(row) => row.id"
style="width: 100%"
>
<el-table-column type="index" label="序號(hào)" width="55" />
<el-table-column prop="id" label="id" width="100"> </el-table-column>
<el-table-column prop="age" label="年齡" width="100"> </el-table-column>
<el-table-column prop="name" label="姓名" width="120"> </el-table-column>
</el-table>
</el-card>
</div>
</template>
<script setup>
import Sortable from "sortablejs";
import { ref, onMounted, onBeforeUnmount } from "vue";
const tableRef = ref();
// 表格數(shù)據(jù)
const tableData = ref([
{ id: 110, age: 20, name: "李白" },
{ id: 120, age: 21, name: "杜甫" },
{ id: 130, age: 22, name: "白居易" },
{ id: 130, age: 27, name: "知否君" },
]);
// 組件掛載之后執(zhí)行
onMounted(() => {
onSortableRow();
onSortableColumn();
});
const sortableRow = ref(null);
const sortableColumn = ref(null);
// 拖動(dòng)表格行
const onSortableRow = () => {
sortableRow.value = Sortable.create(
tableRef.value.$el.querySelector(".el-table__body-wrapper tbody"),
{
animation: 150,
onEnd: ({ newIndex, oldIndex }) => {
// 獲取新的行位置
const currRow = tableData.value.splice(oldIndex, 1)[0];
// 重新排列行位置
tableData.value.splice(newIndex, 0, currRow);
},
}
);
};
// 拖動(dòng)表格列
const onSortableColumn = () => {
sortableColumn.value = Sortable.create(
tableRef.value.$el.querySelector(".el-table__header-wrapper thead tr"),
{
animation: 150,
onEnd: ({ newIndex, oldIndex }) => {
// 1.獲取表格列
const table = tableRef.value;
const oldColumns = table.store.states.columns;
// 2. 重新排列列的順序
const newColumns = [...oldColumns.value];
const movedColumn = newColumns.splice(oldIndex, 1)[0];
newColumns.splice(newIndex, 0, movedColumn);
oldColumns.value = newColumns;
},
}
);
};
// 銷(xiāo)毀
onBeforeUnmount(() => {
if (sortableRow.value) {
sortableRow.value.destroy();
sortableRow.value = null;
}
if (sortableColumn.value) {
sortableColumn.value.destroy();
sortableColumn.value = null;
}
});
</script>
<style scoped></style>效果圖

到此這篇關(guān)于使用Vue3優(yōu)雅地實(shí)現(xiàn)表格拖動(dòng)排序的文章就介紹到這了,更多相關(guān)Vue3表格拖動(dòng)排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2從數(shù)據(jù)變化到視圖變化之nextTick使用詳解
這篇文章主要為大家介紹了vue2從數(shù)據(jù)變化到視圖變化之nextTick使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
vue通信方式EventBus的實(shí)現(xiàn)代碼詳解
這篇文章主要介紹了vue通信方法EventBus的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
Vue頁(yè)面加載后和刷新后的樣式差異問(wèn)題的解決方案
在使用?Vue?構(gòu)建的單頁(yè)面應(yīng)用中,頁(yè)面的樣式可能因?yàn)槁酚汕袚Q、組件加載順序或樣式的動(dòng)態(tài)加載導(dǎo)致樣式混亂,尤其是在復(fù)雜的應(yīng)用中,隨著頁(yè)面的切換或者刷新,按鈕、文字、布局等可能表現(xiàn)出不同的樣式,所以本文給大家介紹了Vue頁(yè)面加載后和刷新后的樣式差異問(wèn)題的解決方案2025-01-01
Vue?Baidu?Map之自定義點(diǎn)圖標(biāo)bm-marker的示例
這篇文章主要介紹了Vue?Baidu?Map之自定義點(diǎn)圖標(biāo)bm-marker,文中給大家介紹了vue-baidu-api地圖標(biāo)記點(diǎn)(自定義標(biāo)記圖標(biāo)),設(shè)置標(biāo)記點(diǎn)的優(yōu)先級(jí)問(wèn)題,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08

