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

}

});

})

*/

相关推荐
_r0bin_3 小时前
前端面试准备-7
开发语言·前端·javascript·fetch·跨域·class
IT瘾君3 小时前
JavaWeb:前端工程化-Vue
前端·javascript·vue.js
zhang98800003 小时前
JavaScript 核心原理深度解析-不停留于表面的VUE等的使用!
开发语言·javascript·vue.js
potender3 小时前
前端框架Vue
前端·vue.js·前端框架
拉不动的猪6 小时前
都25年啦,还有谁分不清双向绑定原理,响应式原理、v-model实现原理
前端·javascript·vue.js
狂炫一碗大米饭6 小时前
一文打通TypeScript 泛型
前端·javascript·typescript
清幽竹客8 小时前
vue-18(使用 Vuex 插件实现高级功能)
前端·vue.js·前端框架·vue
exploration-earth9 小时前
本地优先的状态管理与工具选型策略
开发语言·前端·javascript
哈贝#9 小时前
vue和uniapp聊天页面右侧滚动条自动到底部
javascript·vue.js·uni-app
Lazy_zheng10 小时前
🚀 前端开发福音:用 json-server 快速搭建本地 Mock 数据服务
前端·javascript·vue.js