上一篇
Element UI 本身未直接提供表格列拖拽排序功能,但可通过第三方库实现:
npm install sortablejs --save
import Sortable from 'sortablejs'; export default { mounted() { // 列拖拽逻辑 const headerTr = document.querySelector('.el-table__header-wrapper tr'); Sortable.create(headerTr, { animation: 150, // 动画效果 onEnd: (evt) => { const movedCol = this.columns.splice(evt.oldIndex, 1)[0]; this.columns.splice(evt.newIndex, 0, movedCol); // 更新列顺序 } }); } }
<el-table :data="tableData" row-key="id"> <el-table-column v-for="(col, index) in columns" :key="index" :prop="col.prop" :label="col.label" /> </el-table>
安装依赖:
npm install vuedraggable sortablejs
使用示例:
<template> <draggable v-model="columns" @end="onDragEnd"> <el-table-column v-for="col in columns" :key="col.prop" :prop="col.prop" :label="col.label" /> </draggable> </template> <script> import draggable from 'vuedraggable'; export default { components: { draggable }, methods: { onDragEnd(evt) { // 拖拽结束后的逻辑(如保存顺序) } } } </script>
row-key
(如唯一ID),避免拖拽后数据错乱。.handle { cursor: move; background: url('drag-icon.svg') center no-repeat; }
Sortable.create(headerTr, { animation: 150, // 添加平滑动画 ghostClass: 'sortable-ghost' // 拖拽时的占位样式 });
通过以上方案,您可快速实现 Element UI 表格列的拖拽排序功能,并保持代码的简洁与可维护性!🎉
本文由 业务大全 于2025-08-26发表在【云服务器提供商】,文中图片由(业务大全)上传,本平台仅提供信息存储服务;作者观点、意见不代表本站立场,如有侵权,请联系我们删除;若有图片侵权,请您准备原始证明材料和公证书后联系我方删除!
本文链接:https://cloud.7tqx.com/wenda/732674.html
发表评论