sortable中el-table拖拽及點(diǎn)擊箭頭上下移動(dòng)row效果
效果

安裝
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>點(diǎn)擊箭頭加顏色
.active {
color: #009688; /* 例如,活動(dòng)狀態(tài)的顏色 */
}到此這篇關(guān)于sortable中el-table拖拽及點(diǎn)擊箭頭上下移動(dòng)row的文章就介紹到這了,更多相關(guān)sortable el-table拖拽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js 將圖片連接轉(zhuǎn)換成base64格式的簡單實(shí)例
下面小編就為大家?guī)硪黄猨s 將圖片連接轉(zhuǎn)換成base64格式的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08
js實(shí)現(xiàn)隨機(jī)點(diǎn)名程序
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)隨機(jī)點(diǎn)名程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
jQuery?事件綁定及取消?bind?live?delegate?on?one區(qū)別解析
這篇文章主要介紹了jquery?事件綁定及取消?bind?live?delegate?on?one區(qū)別解析,本文給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11
TypeScript中type和interface的區(qū)別及注意事項(xiàng)
type的類型別用可以用戶其他的類型,比如聯(lián)合類型、元祖類型、基本類型,interface不行,下面這篇文章主要給大家介紹了關(guān)于TypeScript中type和interface的區(qū)別及注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下2022-10-10
分享一個(gè)插件實(shí)現(xiàn)水珠自動(dòng)下落效果
本篇文章給大家分享一個(gè)插件制作水珠自動(dòng)下落效果,效果非常逼真,感興趣的朋友可以添加下面代碼運(yùn)行看看效果哦2016-06-06

