element ui ---table 多级表头嵌套以及合并单元格(一)
最近做的项目中有个合并表头和内容的表格,觉得新鲜,记录一下知识点。
要实现的效果如下:
第一行和第二行分别是表头,第三行和第四行第一列实现多行合并。具体代码实现如下(代码是直接摘取项目):
<el-row style="margin-top:20px" v-for="(item,index) in detailData" :key="index"> <el-table :span-method="arraySpanMethod" :data="item.typeList"> <el-table-column prop="" :label="item.labelName | sliceNumbers"> <el-table-column prop="infoTypeName" label=" " align="center"></el-table-column> <el-table-column prop="organizationCode" label="门店编码" align="center"></el-table-column> <el-table-column prop="unitDesc" width="120" label="基本计量单位描述" align="center"></el-table-column> <el-table-column prop="controlTypeDesc" label="管控描述" align="center"></el-table-column> <el-table-column prop="brand" label="品牌" align="center"></el-table-column> <el-table-column prop="specs" label="规格" align="center"></el-table-column> <el-table-column prop="goodsModel" label="型号" align="center"></el-table-column> <el-table-column prop="isHighValue" label="高低值" align="center"></el-table-column> <el-table-column prop="batchSwitch" label="批次管理" align="center"></el-table-column> <el-table-column prop="isBargain" label="是否划扣" align="center"></el-table-column> <el-table-column prop="effectiveDate" width="100" label="有效期" align="center"></el-table-column> <el-table-column prop="locationTypeDesc" label="库存来源" align="center"></el-table-column> <el-table-column prop="stockAddressDesc" label="仓库编码" align="center"></el-table-column> <el-table-column prop="amount" label="申请/发放数量" align="center"></el-table-column> </el-table-column> <el-table-column :label="item.statusDesc" align="center"> <el-table-column align="center" label="操作"> <template slot-scope="scope"> <el-button v-show="scope.row.buttonType == 1" :disabled="scope.row.buttonStatus == 0" @click="handleClick('provide',{index:scope.$index,data:scope.row})" type="text" v-preventReClick size="small"> 发放 </el-button> <el-button v-show="scope.row.buttonType == 2" :disabled="scope.row.buttonStatus == 0" @click="handleClick('amend',{index:scope.$index,data:scope.row})" type="text" v-preventReClick size="small"> 修改 </el-button> <el-button v-show="scope.row.buttonType == 3" :disabled="scope.row.buttonStatus == 0" v-preventReClick @click="handleClick('delete',{index:scope.$index,data:scope.row})" type="text" size="small"> 删除 </el-button> </template> </el-table-column> </el-table-column> </el-table> </el-row>
js部分:
this.$get(window.jxLocal,params).then(res=>{ this.detailData = res.data; if(this.detailData.length>0){ this.detailData.forEach(item =>{ this.$set(item,'labelName',(item.materialCode+item.materialDesc)) if(item.typeList && item.typeList.length>0){ item.typeList = this.handleTableArr(item.typeList) } }); } })
初始化后台返回的数据
// 初始化详情表格数据格式 handleTableArr(data){ let arr = []; data.forEach(element => { let count = 0; element.infoList.forEach((item,index)=>{ count++; this.stockapplyId = element.infoList[0].id; let deductChildInfo = { span_name: index==0? element.infoList.length:0, id:item.id, organizationCode: item.organizationCode, unitDesc:item.unitDesc, controlTypeDesc: item.controlTypeDesc, brand: item.brand, specs: item.specs, goodsModel: item.goodsModel, isHighValue: item.isHighValue, batchSwitch: item.batchSwitch, isBargain: item.isBargain, effectiveDate: item.effectiveDate, locationTypeDesc: item.locationTypeDesc, stockAddressDesc: item.stockAddressDesc, stockReceiveApplyId:item.stockReceiveApplyId, amount: item.amount, status:item.status, statusDesc: item.statusDesc, buttonType:item.buttonType, //按钮类型 1发放 2修改 3删除 buttonStatus:item.buttonStatus, //按钮可用状态 0不可用 1可用 infoTypeName: element.infoTypeName, infoType:element.infoType } arr.push(deductChildInfo); }) arr[arr.length - count]["count"] = count; }); return arr; },
表格合并方法:
// 表格合并的方法 arraySpanMethod({row, column, rowIndex, columnIndex}) { // 第一列 if (columnIndex === 0){ if (row.count){ return [row.count, 1] }else{ return [0, 0] } } },
标签:
ElementUI
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本