sortable中el-table拖拽及點擊箭頭上下移動row效果
更新時間:2024年08月26日 10:08:52 作者:初七初七
這篇文章主要介紹了sortable中el-table拖拽及點擊箭頭上下移動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>
點擊箭頭加顏色
.active { color: #009688; /* 例如,活動狀態(tài)的顏色 */ }
到此這篇關于sortable中el-table拖拽及點擊箭頭上下移動row的文章就介紹到這了,更多相關sortable el-table拖拽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JS實現(xiàn)來回出現(xiàn)文字的狀態(tài)欄特效代碼
這篇文章主要介紹了JS實現(xiàn)來回出現(xiàn)文字的狀態(tài)欄特效代碼,針對文字的定義及狀態(tài)欄的定時顯示等實現(xiàn)方法備有詳細的文字說明,需要的朋友可以參考下2015-10-10json_decode 索引為數(shù)字時自動排序問題解決方法
這篇文章主要介紹了使用son_encode 給前端返回數(shù)據(jù),結(jié)果順序不對,經(jīng)debug調(diào)試,發(fā)現(xiàn)是json_encode 函數(shù)的問題,變成 " " + 數(shù)字即可,需要的朋友可以參考下2020-03-03JS實現(xiàn)完全語義化的網(wǎng)頁選項卡效果代碼
這篇文章主要介紹了JS實現(xiàn)完全語義化的網(wǎng)頁選項卡效果代碼,可實現(xiàn)基于鼠標滑過及點擊的選項卡切換效果,非常具有實用價值,需要的朋友可以參考下2015-09-09JavaScript算法題之如何將一個數(shù)組旋轉(zhuǎn)k步
這篇文章主要給大家介紹了關于JavaScript算法題之如何將一個數(shù)組旋轉(zhuǎn)k步的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-03-03