elementui——表格的相同内容单元格合并
在今天工作中遇到了相同单元格需要合并的一个需求,实现记录如下。
实现效果:
任务要求:
对表中体系这一列相同的体系进行合并。
思路:
定义一个空数组:[]
定义一个变量:0
遍历数据如果有相同数据 在空数组添加一个0(相同数据的起始位加1),不相同在数据push 一个1(变量改成当前索引)
Table部分代码:

<el-table ref="TaskListDistributionDetailTable" border :data="value.dataList" class="materialInfoTable clear-input-v" :span-method="objectSpanMethod"> <el-table-column type="index" align="center" label="序号" width="60"> <template slot-scope="scope"> {{scope.$index+1}} </template> </el-table-column> <el-table-column align="center" label="体系" min-width="120"> <template slot-scope="scope"> <span>{{scope.row.systemTypeName}}</span> </template> </el-table-column> <el-table-column align="center" label="部门" min-width="120"> <template slot-scope="scope"> <span>{{scope.row.deptName}}</span> </template> </el-table-column> <el-table-column align="left" label="年度考核得分" min-width="100"> <template slot-scope="scope"> <span>{{scope.row.assessmentScore}}</span> </template> </el-table-column> <el-table-column align="left" label="考核排名" min-width="100"> <template slot-scope="scope"> <span>{{scope.row.assessmentRank}}</span> </template> </el-table-column> <el-table-column align="left" label="扣分原因" min-width="300"> <template slot-scope="scope"> <span>{{scope.row.deductionReason}}</span> </template> </el-table-column> <el-table-column align="left" label="备注" min-width="300"> <template slot-scope="scope"> <span>{{scope.row.remark}}</span> </template> </el-table-column> </el-table>
Data部分代码:
data: function () { return { spanArr:[], }; },
Methods部分代码:
objectSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 1) { if(this.spanArr[rowIndex]){ return { rowspan:this.spanArr[rowIndex], colspan:1 } }else{ return { rowspan: 0, colspan: 0 } } } }, flitterData:function () { let contactDot = 0; let spanArr = []; $this.value.dataList.forEach((item, index) => { if (index === 0) { spanArr.push(1) } else {
//注释:systemTypeName 是对应体系,value.dataList 对应table绑定的数据源
if (item.systemTypeName === this.value.dataList[index - 1].systemTypeName) {
spanArr[contactDot] += 1; spanArr.push(0) } else { contactDot = index spanArr.push(1) } } }) this.spanArr = spanArr; },
在合适的地方调用 flitterData方法 即可,我在项目中是获取数据源之后调用的
原文:https://blog.csdn.net/weixin_44202166/article/details/110471420
参考写法,方式调用与原文不同
标签:
Vue elementui
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异