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

}

});

})

*/

相关推荐
FRing037 小时前
vue2干货总结12(loading效果、全局前置导航守卫、搜索历史管理)
开发语言·前端·javascript·vue.js·vue2
YIAN7 小时前
我踩完 DeepAgents 记忆模块的坑,才搞懂长期记忆和摘要不是一回事
前端·javascript·agent
YIAN8 小时前
用 Deep Agents 复现一个深度调研助手:从主 Agent、子 Agent 到 Skill
javascript·agent
map_3d_vis9 小时前
百度地图 JSAPI 4.0 是什么:版本说明与能力概览
前端·javascript·webgl·地图·百度地图
泯泷12 小时前
第一章:Lit简介 - 现代Web组件标准
前端·javascript·web components
kyriewen14 小时前
我让用户白等两秒,只为一个 16MB 的中文字体
前端·javascript·程序员
泯泷16 小时前
第二章:你的第一个Lit组件 - 从零到"Hello, World"
前端·javascript·web components
多加点辣也没关系18 小时前
JavaScript|第42章:安全与最佳实践
开发语言·javascript·安全
泯泷18 小时前
第三章:lit-html的威力 - 精通声明式模板
前端·javascript·web components
bai27319 小时前
npm 在 cmd 里能跑,Node 的 spawn 一调就 EINVAL:Windows 上 .cmd 的六种调法实测
javascript·后端