sortable中el-table拖拽及點擊箭頭上下移動row效果
更新時間:2024年08月26日 10:08:52 作者:初七初七
這篇文章主要介紹了sortable中el-table拖拽及點擊箭頭上下移動row效果,本文通過實例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
效果

安裝
npm install sortablejs --save
引入
import Sortable from "sortablejs";
<el-table
:data="tableBody"
border
ref="tableRef"
:stripe="true"
:key="tableKey"
>
<el-table-column type="index" label="排序" width="150" key="index" />
<el-table-column prop="category" label="大類名稱" key="category">
<template #default="{ row, $index }">
<div class="title">{{ row.category }}</div>
<div class="icon">
<el-icon
:class="{ active: activeButton === `up-${$index}` }"
@click="moveItem($index, 'up')"
><CaretTop
/></el-icon>
<el-icon
:class="{ active: activeButton === `down-${$index}` }"
@click="moveItem($index, 'down')"
><CaretBottom
/></el-icon>
</div>
</template>
</el-table-column>
</el-table>
<script setup>
const activeButton = ref();
//行拖拽
const rowDrop=()=> {
const tbody = tableRef.value?.$el.querySelector(
".el-table__body-wrapper tbody"
);
Sortable.create(wrapperTr, {
animation: 150,
ghostClass: "blue-background-class",
onEnd: async (evt: any) => {
handleDragEnd(evt);
},
});
}
const handleDragEnd = async (event: any) => {
const { oldIndex, newIndex } = event;
if (oldIndex !== newIndex) {
const movedItem = tableBody.value.splice(oldIndex, 1)[0];
tableBody.value.splice(newIndex, 0, movedItem);
tableKey.value += 1;
const url = "./?_m=&_a=";
const formData = new FormData();
formData.append("id", globalData.id);
formData.append("category", movedItem.category);
formData.append("xh", newIndex + 1);
const response = await post(url, formData);
if (response.code == 0) {
ElMessage({
showClose: true,
message: "排序成功",
type: "success",
});
}
}
};
const moveItem = async (index: any, direction: any) => {
const newIndex = direction == "up" ? index - 1 : index + 1;
if (newIndex >= 0 && newIndex < tableBody.value.length) {
const item = tableBody.value.splice(index, 1)[0];
tableBody.value.splice(newIndex, 0, item);
activeButton.value = `${direction}-${index}`;
setTimeout(() => (activeButton.value = null), 200);
const url = "./?_m=&_a=";
const formData = new FormData();
formData.append("id", globalData.id);
formData.append("category", item.category);
formData.append("xh", newIndex + 1);
const response = await post(url, formData);
if (response.code == 0) {
ElMessage({
showClose: true,
message: "排序成功",
type: "success",
});
}
}
};
</script>點擊箭頭加顏色
.active {
color: #009688; /* 例如,活動狀態(tài)的顏色 */
}到此這篇關(guān)于sortable中el-table拖拽及點擊箭頭上下移動row的文章就介紹到這了,更多相關(guān)sortable el-table拖拽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS實現(xiàn)來回出現(xiàn)文字的狀態(tài)欄特效代碼
這篇文章主要介紹了JS實現(xiàn)來回出現(xiàn)文字的狀態(tài)欄特效代碼,針對文字的定義及狀態(tài)欄的定時顯示等實現(xiàn)方法備有詳細(xì)的文字說明,需要的朋友可以參考下2015-10-10
json_decode 索引為數(shù)字時自動排序問題解決方法
這篇文章主要介紹了使用son_encode 給前端返回數(shù)據(jù),結(jié)果順序不對,經(jīng)debug調(diào)試,發(fā)現(xiàn)是json_encode 函數(shù)的問題,變成 " " + 數(shù)字即可,需要的朋友可以參考下2020-03-03
js中symbol類型以及symbol的三大應(yīng)用場景詳解
Symbol是ES6新推出的一種基本類型,它表示獨(dú)一無二的值,它可以接受一個字符串作為參數(shù),帶有相同參數(shù)的兩個Symbol值不相等,這個參數(shù)只是表示Symbol值的描述而已,下面這篇文章主要給大家介紹了關(guān)于js中symbol類型以及symbol的三大應(yīng)用場景,需要的朋友可以參考下2022-09-09
ImageZoom 圖片放大鏡效果(多功能擴(kuò)展篇)
上一篇ImageZoom已經(jīng)對圖片放大效果做了詳細(xì)的分析,這次在ImageZoom的基礎(chǔ)上進(jìn)行擴(kuò)展,實現(xiàn)更多的效果。2010-04-04
JS實現(xiàn)完全語義化的網(wǎng)頁選項卡效果代碼
這篇文章主要介紹了JS實現(xiàn)完全語義化的網(wǎng)頁選項卡效果代碼,可實現(xiàn)基于鼠標(biāo)滑過及點擊的選項卡切換效果,非常具有實用價值,需要的朋友可以參考下2015-09-09
JavaScript算法題之如何將一個數(shù)組旋轉(zhuǎn)k步
這篇文章主要給大家介紹了關(guān)于JavaScript算法題之如何將一個數(shù)組旋轉(zhuǎn)k步的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-03-03

