el-table实现行拖拽效果(Vue3.0)

本文展示了如何在Vue3.0中利用Sortable.js库实现el-table组件的行拖拽功能。通过绑定row-key并设置唯一值,确保数据正确更新。在onEnd回调中,根据拖拽前后的位置调整数据数组,完成行的重新排序。

el-table实现行拖拽(Vue3.0)


*注意!!row-key绑定的值要唯一!!

<template>
  <div class="page_eight">
    <el-table :data="tableData" row-key="id">
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column prop="date" label="日期"></el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, reactive, toRefs } from "vue";
import Sortable from "sortablejs";
export default defineComponent({
  name: "pageSeven",
  components: {},
  setup() {
    const state = reactive({
      tableData: [
        {
          id: "1",
          date: "2016-05-02",
          name: "王小虎1",
          address: "上海市普陀区金沙江路 100 弄",
        },
        {
          id: "2",
          date: "2016-05-04",
          name: "王小虎2",
          address: "上海市普陀区金沙江路 200 弄",
        },
        {
          id: "3",
          date: "2016-05-01",
          name: "王小虎3",
          address: "上海市普陀区金沙江路 300 弄",
        },
        {
          id: "4",
          date: "2016-05-03",
          name: "王小虎4",
          address: "上海市普陀区金沙江路 400 弄",
        },
      ],
    });
    // 行拖拽
    function rowDrop() {
      const tbody = document.querySelector( ".el-table__body-wrapper tbody" ) as HTMLElement;
      Sortable.create(tbody, {
        animation: 180,
        delay: 0,
        // 结束拖拽后的回调函数
        onEnd: (evt: { newIndex: any; oldIndex: any }) => {
          const currentRow = state.tableData.splice(evt.oldIndex, 1)[0];
          state.tableData.splice(evt.newIndex, 0, currentRow);
        },
      });
    }
    onMounted(() => {
      rowDrop();
    });
    return {
      ...toRefs(state),
      rowDrop,
    };
  },
});
</script>

<style lang="scss">
.page_eight {
  width: 100%;
  height: 100%;
}
</style>

<think>我们被要求实现Vueel-table拖拽排序功能。根据引用[1]和[2],我们可以使用Sortable.js库来实现。下面将分步骤说明实现方法。 ### 步骤1: 安装Sortable.js 首先,在项目中安装Sortable.js依赖: ```bash npm install sortablejs --save ``` ### 步骤2: 创建可拖拽的表格组件 在Vue组件中,我们需要在`el-table`上使用Sortable.js来使表格拖拽。注意,我们需要在表格渲染完成后初始化Sortable。 #### 示例代码 ```vue <template> <el-table :data="tableData" row-key="id" ref="tableRef"> <el-table-column prop="date" label="日期" width="180" /> <el-table-column prop="name" label="姓名" width="180" /> <el-table-column prop="address" label="地址" /> </el-table> </template> <script> import { ref, onMounted } from &#39;vue&#39; import Sortable from &#39;sortablejs&#39; export default { setup() { const tableData = ref([ { id: 1, date: &#39;2016-05-02&#39;, name: &#39;王小虎&#39;, address: &#39;上海市普陀区金沙江路 1518 弄&#39; }, { id: 2, date: &#39;2016-05-04&#39;, name: &#39;王小虎&#39;, address: &#39;上海市普陀区金沙江路 1517 弄&#39; }, { id: 3, date: &#39;2016-05-01&#39;, name: &#39;王小虎&#39;, address: &#39;上海市普陀区金沙江路 1519 弄&#39; }, { id: 4, date: &#39;2016-05-03&#39;, name: &#39;王小虎&#39;, address: &#39;上海市普陀区金沙江路 1516 弄&#39; } ]) const tableRef = ref(null) onMounted(() => { initSortable() }) const initSortable = () => { // 获取表格的tbody元素(注意:el-table内部结构,实际可能有多层,这里通过ref获取表格实例,然后找到tbody) const el = tableRef.value.$el.querySelectorAll(&#39;.el-table__body-wrapper > table > tbody&#39;)[0] Sortable.create(el, { animation: 150, // 动画时长 ghostClass: &#39;sortable-ghost&#39;, // 拖拽时占位符的样式类 onEnd: (evt) => { // 拖拽结束事件 const { oldIndex, newIndex } = evt if (oldIndex === newIndex) return // 处理数据顺序 const currentRow = tableData.value.splice(oldIndex, 1)[0] tableData.value.splice(newIndex, 0, currentRow) } }) } return { tableData, tableRef } } } </script> <style> /* 拖拽时的样式 */ .sortable-ghost { opacity: 0.5; background: #c8ebfb; } </style> ``` ### 关键点说明 1. **获取表格的DOM元素**:通过`tableRef.value.$el`获取表格根元素,然后使用`querySelector`找到tbody元素。注意,Element Plus的表格结构,实际拖拽的是`tbody`。 2. **初始化Sortable**:在`onMounted`生命周期钩子中初始化Sortable,确保表格已经渲染。 3. **拖拽事件处理**:在`onEnd`事件中,根据拖拽的旧索引和新索引,更新数据数组`tableData`。注意,这里直接操作数组,Vue的响应式系统会自动更新视图。 4. **样式**:添加拖拽时的占位符样式,提升用户体验。 ### 注意事项 - 如果表格有分页、筛选或排序功能,需要额外处理,因为拖拽排序会直接改变原始数据顺序。建议在拖拽完成后将新的顺序发送到服务器保存。 - 如果表格数据量很大,拖拽时可能会有性能问题,可以考虑使用虚拟滚动表格。 ### 优化建议 1. **添加拖拽手柄**:默认整拖拽,可以指定拖拽手柄(如一个图标),在Sortable配置中设置`handle`属性。 ```js Sortable.create(el, { handle: &#39;.drag-handle&#39;, // 指定拖拽手柄的CSS选择器 // ...其他配置 }) ``` 然后在表格列中添加一个使用该类的元素: ```vue <el-table-column label="操作"> <template #default> <i class="drag-handle" style="cursor: move;">::</i> </template> </el-table-column> ``` 2. **数据持久化**:拖拽完成后,可以将新的数据顺序发送到服务器保存。 ### 完整示例 下面是一个带有拖拽手柄的完整示例: ```vue <template> <el-table :data="tableData" row-key="id" ref="tableRef"> <el-table-column label="排序" width="80"> <template #default> <i class="drag-handle" style="cursor: move; font-size: 16px">⋮</i> </template> </el-table-column> <el-table-column prop="date" label="日期" width="180" /> <el-table-column prop="name" label="姓名" width="180" /> <el-table-column prop="address" label="地址" /> </el-table> </template> <script> import { ref, onMounted } from &#39;vue&#39; import Sortable from &#39;sortablejs&#39; export default { setup() { const tableData = ref([...]) // 数据 const tableRef = ref(null) onMounted(() => { initSortable() }) const initSortable = () => { const el = tableRef.value.$el.querySelectorAll(&#39;.el-table__body-wrapper > table > tbody&#39;)[0] Sortable.create(el, { handle: &#39;.drag-handle&#39;, // 指定拖拽手柄 animation: 150, ghostClass: &#39;sortable-ghost&#39;, onEnd: (evt) => { const { oldIndex, newIndex } = evt if (oldIndex === newIndex) return const currentRow = tableData.value.splice(oldIndex, 1)[0] tableData.value.splice(newIndex, 0, currentRow) } }) } return { tableData, tableRef } } } </script> <style> .sortable-ghost { opacity: 0.5; background: #c8ebfb; } </style> ``` 这样,用户只能通过拖拽手柄来拖动,避免误操作。 ### 引用说明 本文参考了Sortable.js的官方文档和Element Plus表格组件的使用,结合Vue3的响应式特性实现拖拽排序功能[^1][^2]。 §§相关问题§§ 1. 如何在拖拽排序后保存新的顺序到服务器? 2. 如果表格有分页,如何实现跨页拖拽排序? 3. 如何优化大型数据表格的拖拽性能?</think># Vue el-table 拖拽排序实现方案 在Vue项目中实现el-table拖拽排序功能,可以通过Sortable.js库轻松实现。下面我将详细介绍实现方法和完整代码示例。 ## 实现思路 1. 使用Sortable.js实现拖拽功能 2. 在表格渲染完成后初始化Sortable实例 3. 处理拖拽结束事件,更新数据顺序 4. 添加视觉反馈效果提升用户体验 ## 完整实现代码 ```vue <template> <div class="drag-sort-container"> <h1>Vue el-table 拖拽排序实现</h1> <div class="instructions"> <p>拖拽左侧的图标即可重新排序表格数据</p> </div> <el-table :data="tableData" row-key="id" ref="tableRef" style="width: 100%" border stripe class="drag-table" > <el-table-column width="50"> <template #default> <div class="drag-handle"> <el-icon><Rank /></el-icon> </div> </template> </el-table-column> <el-table-column prop="name" label="姓名" width="180" /> <el-table-column prop="position" label="职位" /> <el-table-column prop="department" label="部门" /> <el-table-column prop="salary" label="薪资" align="right" /> <el-table-column label="操作" width="120"> <template #default="scope"> <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button> </template> </el-table-column> </el-table> <div class="data-preview"> <h3>当前数据顺序:</h3> <div v-for="(item, index) in tableData" :key="item.id" class="data-item"> <span class="index">{{ index + 1 }}.</span> {{ item.name }} - {{ item.position }} </div> </div> </div> </template> <script setup> import { ref, onMounted } from &#39;vue&#39; import Sortable from &#39;sortablejs&#39; import { Rank } from &#39;@element-plus/icons-vue&#39; // 表格数据 const tableData = ref([ { id: 1, name: &#39;张三&#39;, position: &#39;前端工程师&#39;, department: &#39;技术部&#39;, salary: 15000 }, { id: 2, name: &#39;李四&#39;, position: &#39;后端工程师&#39;, department: &#39;技术部&#39;, salary: 16000 }, { id: 3, name: &#39;王五&#39;, position: &#39;UI设计师&#39;, department: &#39;设计部&#39;, salary: 14000 }, { id: 4, name: &#39;赵六&#39;, position: &#39;产品经理&#39;, department: &#39;产品部&#39;, salary: 18000 }, { id: 5, name: &#39;钱七&#39;, position: &#39;测试工程师&#39;, department: &#39;质量部&#39;, salary: 12000 } ]) const tableRef = ref(null) // 初始化拖拽功能 const initSortable = () => { // 获取表格的tbody元素 const el = tableRef.value.$el.querySelector(&#39;.el-table__body-wrapper tbody&#39;) // 创建Sortable实例 Sortable.create(el, { handle: &#39;.drag-handle&#39;, // 指定拖拽手柄 animation: 150, // 动画时长 ghostClass: &#39;ghost-row&#39;, // 拖拽时的样式类 chosenClass: &#39;chosen-row&#39;, // 选中的样式类 dragClass: &#39;drag-row&#39;, // 拖拽时的样式类 onEnd: (evt) => { // 拖拽结束事件处理 const { oldIndex, newIndex } = evt if (oldIndex === newIndex) return // 更新数据顺序 const currentRow = tableData.value.splice(oldIndex, 1)[0] tableData.value.splice(newIndex, 0, currentRow) } }) } // 编辑操作 const handleEdit = (row) => { console.log(&#39;编辑:&#39;, row) ElMessage.success(`开始编辑: ${row.name}`) } // 组件挂载后初始化拖拽 onMounted(() => { initSortable() }) </script> <style scoped> .drag-sort-container { max-width: 1000px; margin: 0 auto; padding: 20px; } h1 { text-align: center; color: #2c3e50; margin-bottom: 30px; } .instructions { background-color: #f8f9fa; padding: 15px; border-radius: 8px; margin-bottom: 20px; text-align: center; color: #6c757d; } .drag-table { margin-bottom: 30px; box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1); } .drag-handle { cursor: move; color: #409EFF; display: flex; align-items: center; justify-content: center; height: 100%; } .data-preview { background-color: #f8f9fa; padding: 20px; border-radius: 8px; margin-top: 20px; } .data-item { padding: 10px 15px; border-bottom: 1px solid #eee; display: flex; align-items: center; } .data-item:last-child { border-bottom: none; } .index { display: inline-block; width: 30px; height: 30px; line-height: 30px; text-align: center; background-color: #409EFF; color: white; border-radius: 50%; margin-right: 15px; } /* 拖拽样式 */ .ghost-row { background-color: #e6f7ff !important; } .chosen-row { background-color: #f0f7ff; box-shadow: 0 0 10px rgba(64, 158, 255, 0.3); } .drag-row { opacity: 0.8; background-color: #d9ecff; } </style> ``` ## 关键实现步骤 1. **安装Sortable.js库** ```bash npm install sortablejs --save ``` 2. **引入Sortable.js** ```javascript import Sortable from &#39;sortablejs&#39; ``` 3. **获取表格DOM元素** ```javascript const el = tableRef.value.$el.querySelector(&#39;.el-table__body-wrapper tbody&#39;) ``` 4. **初始化Sortable实例** ```javascript Sortable.create(el, { handle: &#39;.drag-handle&#39;, // 指定拖拽手柄 animation: 150, ghostClass: &#39;ghost-row&#39;, onEnd: (evt) => { // 更新数据顺序 const { oldIndex, newIndex } = evt const currentRow = tableData.value.splice(oldIndex, 1)[0] tableData.value.splice(newIndex, 0, currentRow) } }) ``` 5. **添加拖拽手柄** ```html <el-table-column width="50"> <template #default> <div class="drag-handle"> <el-icon><Rank /></el-icon> </div> </template> </el-table-column> ``` ## 优化建议 1. **添加拖拽视觉反馈**:使用ghostClass、chosenClass等属性提供拖拽时的视觉反馈 2. **保存排序结果**:拖拽完成后可以将新的顺序发送到服务器保存 3. **性能优化**:对于大型表格,可以使用虚拟滚动技术提高性能 4. **跨页拖拽**:如果需要跨页拖拽,需要实现分页数据合并与分割 ## 常见问题解决 1. **拖拽无效**: - 确保在onMounted钩子中初始化Sortable - 检查DOM选择器是否正确获取到tbody元素 2. **样式问题**: - 添加适当的CSS样式提供拖拽视觉反馈 - 使用!important覆盖Element Plus默认样式 3. **数据更新问题**: - 确保表格数据是响应式的(使用ref或reactive) - 在onEnd事件中正确更新数据顺序 ## 相关资源 - [Sortable.js官方文档](https://github.com/SortableJS/Sortable) - [Element Plus表格文档](https://element-plus.org/zh-CN/component/table.html)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值