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 });

}

});

})

*/

相关推荐
tyro曹仓舒5 分钟前
干了10年前端,才学会使用IntersectionObserver
前端·javascript
李剑一1 小时前
我用Trae生成了一个Echarts 3D柱状图的Demo
前端·vue.js·trae
mine_mine1 小时前
油猴脚本拦截fetch和xhr请求,实现修改服务端接口功能
javascript
一 乐1 小时前
宠物猫店管理|宠物店管理|基于Java+vue的宠物猫店管理管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·后端·宠物管理
熊猫比分管理员2 小时前
【全栈源码解决方案】Vue+Java四端齐全,一周交付可运行项目!
java·前端·vue.js
o***74172 小时前
【Nginx 】Nginx 部署前端 vue 项目
前端·vue.js·nginx
一 乐2 小时前
考公|考务考试|基于SprinBoot+vue的考公在线考试系统(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·课程设计
林太白2 小时前
跟着TRAE SOLO全链路看看项目部署服务器全流程吧
前端·javascript·后端
特级业务专家2 小时前
把 16MB 中文字体压到 400KB:我写了一个 Vite 字体子集插件
javascript·vue.js·vite
humor2 小时前
Quill 2.x 从 0 到 1 实战 - 为 AI+Quill 深度结合铺路
前端·vue.js