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

}

});

})

*/

相关推荐
陈皮话梅糖@2 小时前
使用 Provider 和 GetX 实现 Flutter 局部刷新的几个示例
开发语言·javascript·flutter
Yolo@~6 小时前
个人网站:基于html、css、js网页开发界面
javascript·css·html
斯~内克6 小时前
Electron 菜单系统深度解析:从基础到高级实践
前端·javascript·electron
清风絮柳6 小时前
51. “闲转易”交易平台小程序(基于springboot&vue)
vue.js·spring boot·小程序·毕业设计·校园二手交易平台·二手交易小程序·闲转易交易系统
dr李四维6 小时前
vue生命周期、钩子以及跨域问题简介
前端·javascript·vue.js·websocket·跨域问题·vue生命周期·钩子函数
旭久7 小时前
react+antd中做一个外部按钮新增 表格内部本地新增一条数据并且支持编辑删除(无难度上手)
前端·javascript·react.js
朴拙数科8 小时前
技术长期主义:用本分思维重构JavaScript逆向知识体系(一)Babel、AST、ES6+、ES5、浏览器环境、Node.js环境的关系和处理流程
javascript·重构·es6
拉不动的猪9 小时前
vue与react的简单问答
前端·javascript·面试
旭久10 小时前
react+antd封装一个可回车自定义option的select并且与某些内容相互禁用
前端·javascript·react.js