您的位置:首页 > 健康 > 美食 > sortable中el-table拖拽及点击箭头上下移动row

sortable中el-table拖拽及点击箭头上下移动row

2024/12/21 23:41:06 来源:https://blog.csdn.net/weixin_63442947/article/details/141392441  浏览:    关键词:sortable中el-table拖拽及点击箭头上下移动row

效果

安装 

npm install sortablejs --save

引入

import Sortable from "sortablejs";
 <el-table:data="tableBody"borderref="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; /* 例如,活动状态的颜色 */
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com