vue3 elemnetPlus table 数据id 相同的合并单元格

<template>

<el-table :data="tableData" :span-method="arraySpanMethod" style="width: 100%">

<el-table-column prop="id" label="ID" width="180"></el-table-column>

<el-table-column prop="name" label="Name" width="180"></el-table-column>

<!-- 其他列 -->

</el-table>

</template>

<script>

import { ref } from 'vue';

export default {

setup() {

const tableData = ref([

{ id: 1, name: 'Alice' },

{ id: 1, name: 'Bob' },

{ id: 2, name: 'Charlie' },

{ id: 2, name: 'David' }

]);

const idMap = new Map(); // 用于存储每个id连续出现的次数和起始行索引

tableData.value.forEach((row, index) => {

if (!idMap.has(row.id)) {

idMap.set(row.id, { count: 1, startIndex: index });

} else {

const { count, startIndex } = idMap.get(row.id);

idMap.set(row.id, { count: count + 1, startIndex });

}

});

const arraySpanMethod = ({ row, column, rowIndex }) => {

if (column.property === 'id') {

const { count, startIndex } = idMap.get(row.id);

if (rowIndex === startIndex) {

return [count, 1]; // 在起始行,跨越count行,1列

} else {

return [0, 0]; // 其他行,不显示(被合并了)

}

} else {

return [1, 1]; // 其他列,正常显示

}

};

return {

tableData,

arraySpanMethod,

};

},

};

</script>

/*

//动态添加行或列 需要 在监听处清空map idMap.clear();

watchEffect(() => {

idMap.clear();

tableData.value = final.value.list;

tableData.value.forEach((row, index) => {

let id = getpropertyId(row)

if (!idMap.has(id)) {

idMap.set(id, { count: 1, startIndex: index });

} else {

const { count, startIndex } = idMap.get(id);

idMap.set(id, { count: count + 1, startIndex });

}

});

})

*/

相关推荐
VX:Fegn089511 小时前
计算机毕业设计|基于ssm + vue超市管理系统(源码+数据库+文档)
前端·数据库·vue.js·spring boot·后端·课程设计
2401_8920005212 小时前
Flutter for OpenHarmony 猫咪管家App实战 - 添加提醒实现
前端·javascript·flutter
Yolanda9412 小时前
【项目经验】vue h5移动端禁止缩放
前端·javascript·vue.js
VX:Fegn089513 小时前
计算机毕业设计|基于springboot + vue酒店管理系统(源码+数据库+文档)
vue.js·spring boot·课程设计
EndingCoder14 小时前
案例研究:从 JavaScript 迁移到 TypeScript
开发语言·前端·javascript·性能优化·typescript
Irene199114 小时前
Vue3 中使用的命名规则 和 实际开发命名规范总结
vue.js·命名规范
Amumu1213815 小时前
Vue脚手架(二)
前端·javascript·vue.js
lichenyang45316 小时前
从零开始构建 React 文档系统 - 完整实现指南
前端·javascript·react.js
比特森林探险记16 小时前
Hooks、状态管理
前端·javascript·react.js
比特森林探险记17 小时前
组件通信 与 ⏳ 生命周期
前端·javascript·vue.js