Vue3、el-table、sortablejs实现行拖动时出现的问题和解决方案

Vue3、el-table、sortablejs实现行拖动时出现的问题和解决方案

初始化

插件引入参考其他文章

<div class="header-set-main">
        <el-table
          :data="dictList"
          show-overflow-tooltip
          height="450"
          highlight-current-row
          ref="dictTable"
          :key="dictTableKey"
        >
          <el-table-column
            align="center"
            prop="name"
            label="数据类型"
            min-width="100"
          />
          <el-table-column
            align="center"
            prop="sort"
            label="排序"
            min-width="40"
          >
          </el-table-column>
        </el-table>
      </div>
/*
* 
*/
const rowDropInit = () => {
      Sortable.create(dictTable.value.$el.children[2].children[0].children[1], {
        animation: 150,
        delay: 0,
        onEnd: function (e) {
          const res = state.dictList.splice(e.oldIndex, 1)[0];
          state.dictList.splice(e.newIndex, 0, res);

          state.dictList.forEach((item, index) => {
            item.sort = index;
          });
        },
      });
    };

 onMounted(async () => {
      /*------------------
      * 异步加载table数据
      * ------------------
      */
      nextTick(() => {
        rowDropInit();
      });
    });

出现的问题

拖动后实际渲染的结果和table数据顺序不同,只要是因为虚拟dom和真实dom不同步

在这里插入图片描述
在这里插入图片描述

解决方案

给当前table绑定一个key值。当出现拖动事件即修改key值,强制整个table更新。但是需要在table更新后重新注册拖动功能

const rowDropInit = () => {
      Sortable.create(dictTable.value.$el.children[2].children[0].children[1], {
        animation: 150,
        delay: 0,
        onEnd: function (e) {
          const res = state.dictList.splice(e.oldIndex, 1)[0];
          state.dictList.splice(e.newIndex, 0, res);

          state.dictList.forEach((item, index) => {
            item.sort = index;
          });
          //通过更新el-table的key值来保证table排序的数据渲染正确。
          state.dictTableKey++;
        },
      });
    };
  
 /*
 * 监听table的key值,重新注册拖动功能
 */
 watch(
      () => state.dictTableKey,
      () => {
        nextTick(() => {
          rowDropInit();
        });
      }
    );

拖动后正确的排序结果:
在这里插入图片描述

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Vue 中使用 `el-table` 实现表间的拖拽功能,可以借助 `sortablejs` 插件。`sortablejs` 是一个基于原生 JavaScript 的拖拽排序库,其支持 Vue 等框架。 下面是实现步骤: 1. 安装 `sortablejs` 插件 ```bash npm install sortablejs --save ``` 2. 在 Vue 组件中引入 `sortablejs` ```javascript import Sortable from 'sortablejs'; ``` 3. 在 `mounted` 钩子中初始化 `sortablejs` ```javascript mounted() { const table = document.getElementById('table'); const options = { animation: 150, onEnd: this.onEndHandler }; Sortable.create(table, options); }, ``` 其中,`table` 是 `el-table` 对应的 DOM 元素,`options` 是 `Sortable` 的一些配置项。`onEndHandler` 是当拖拽结束的回调函数,需要自己实现。 4. 实现 `onEndHandler` 回调函数 ```javascript methods: { onEndHandler(event) { const { oldIndex, newIndex } = event; const newData = [...this.tableData]; const [removed] = newData.splice(oldIndex, 1); newData.splice(newIndex, 0, removed); this.tableData = newData; }, }, ``` 其中,`tableData` 是 `el-table` 的数据源,当拖拽结束后,根据 `oldIndex` 和 `newIndex` 获取被拖拽的元素和目标位置,然后在数据源中重新排序。 5. 在 `el-table` 中添加 `:row-key` 属性 ```html <el-table :data="tableData" :row-key="row => row.id" id="table"> ... </el-table> ``` 这里需要为 `el-table` 指定一个唯一的 `row-key`,以便在拖拽结束后能够正确地更新数据源。 完成以上步骤后,即可在 `el-table` 中实现表间的拖拽功能。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值