博客
关于我
Vue3+element plus+sortablejs实现table列表拖拽
阅读量:801 次
发布时间:2023-02-16

本文共 1567 字,大约阅读时间需要 5 分钟。

Vue项目中使用Sortable.js实现表格拖拽功能

一、基本配置

1. 安装依赖

通过npm安装Sortable.js:

npm install sortablejs --save

2. 引入库文件

在项目中引入Sortable.js:

import Sortable from 'sortablejs';

二、核心实现

1. 表格结构设计

在Vue项目中,确保表格结构如下:

2. 集成拖拽功能

行拖拽配置

onMounted(() => {
columnDrop();
rowDrop();
});
// 列拖拽方法
const mykey = ref(0);
function columnDrop() {
const wrapperTr = document.querySelector('.el-table__header-wrapper tr');
Sortable.create(wrapperTr, {
animation: 180,
delay: 0,
onEnd: evt => {
const empty = 1;
const oldItem = tableHeaderList.value[evt.oldIndex - empty];
tableHeaderList.value.splice(evt.oldIndex - empty, 1);
tableHeaderList.value.splice(evt.newIndex - empty, 0, oldItem);
mykey.value = Math.floor(Math.random() * 1000 + 1);
}
});
}
// 行拖拽方法
function rowDrop() {
const tbody = document.querySelector('.el-table__body-wrapper tbody');
Sortable.create(tbody, {
onEnd({ newIndex, oldIndex }) {
console.log('拖动了行,当前序号:' + newIndex);
const currentRow = receiptDataList.value.splice(oldIndex, 1)[0];
receiptDataList.value.splice(newIndex, 0, currentRow);
}
});
}

3. 表格字段配置

核心字段说明

  • row-key:为表格行赋予唯一标识符,避免数据错乱
  • sortable:表头是否支持拖拽排序

表格列配置示例

三、注意事项

  • 表头拖拽:确保表头的col类没有冲突的CSS
  • 行拖拽:建议在行拖拽时设置row-key以避免数据重复
  • 性能优化:在大数据量情况下,行拖拽可能导致性能问题
  • 四、完整代码示例

    五、总结

    通过以上步骤,轻松实现Vue项目中的表格拖拽功能。记得在实际使用中根据项目需求进行适当调整,并注意性能优化。

    转载地址:http://aojfk.baihongyu.com/

    你可能感兴趣的文章
    node基础(二)_模块以及处理乱码问题
    查看>>
    node安装及配置之windows版
    查看>>
    Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
    查看>>
    Node读取并输出txt文件内容
    查看>>
    node防xss攻击插件
    查看>>
    noi 7827 质数的和与积
    查看>>
    NOIp2005 过河
    查看>>
    NOIp模拟赛二十九
    查看>>
    NOPI读取Excel
    查看>>
    NoSQL&MongoDB
    查看>>
    NoSQL介绍
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>