当前位置: 首页 > news >正文

vue.js el-table 动态单元格列合并

一、业务需求:

一个展示列表,表格中有一部分列是根据后端接口动态展示,对于不同类型的数据展示效果不一样。如果接口返回数据是’类型1‘的,则正常展示,如果是’类型2‘的数据,则合并当前数据的动态表格。

二、实现思路:

1、先将普通表格实现,不考虑合并效果;

2、在表格上对要合并的单元格类型进行区分;

3、 在表格上使用:span-method="arraySpanMethod"方法触发表格;

4、在arraySpanMethod方法内接收数据处理合并,确定从哪一列开始合并到哪一列合并结束;

三、代码展示:

<el-tableref="table"size="mini"height="100%":data="tableData":span-method="arraySpanMethod":header-cell-style="{background: '#f5f7fa',fontWeight: 'bold',color: '#303133'}"border><el-table-columntype="index"header-align="center"align="center"label="序号"width="50"></el-table-column><el-table-columnwidth="120"prop="indexShowName"label="名称"show-overflow-tooltip></el-table-column><el-table-columnwidth="80"prop="type"label="类型种类"show-overflow-tooltip><template slot-scope="scope">{{ scope.row.type === '1' ? '类型1' : '类型2' }}</template></el-table-column><el-table-columnv-for="(item, index) in tableColumns":key="index"width="80":label="item.year"show-overflow-tooltip><template slot-scope="scope"><!-- 类型1展示name --><divv-if="scope.row.type === '1'"style="text-align: center">{{scope.row.uploadValueList[index]?.uploadValueName}}</div><!-- 类型2展示value --><div v-else>{{ scope.row.uploadValueList[index].uploadValue }}</div></template></el-table-column><el-table-columnwidth="160"prop="reportDate"label="上报时间"show-overflow-tooltip></el-table-column><el-table-columnwidth="195"label="操作"header-align="center"align="center"fixed="right"><template slot-scope="scope"><el-buttonsize="small"style="color: #409eff; padding: 0"type="text"@click="detailClick(scope.row)">数据明细</el-button></template></el-table-column></el-table>// --------------methods方法--------------------// 右侧表格initTable() {const params = {pageNum: this.pages.pageIndex,pageSize: this.pages.pageSize,}this.tableLoading = true//api接口调用xxxxxx(params).then((res) => {if (res.code === 200) {const { total } = res.result// const { records, total } = res.result//后端接口数据返回形式如下:const records = [{indexShowName: '测试001',type: '1',reportDate: '2023-12-01 15:53:46',uploadValueList: [{id: '1',year: '2021年',uploadValue: '0',uploadValueName: '完全符合'},{id: '2',year: '2022年',uploadValue: '0',uploadValueName: '完全符合'},{id: '3',year: '2023年',uploadValue: '0',uploadValueName: '完全符合'},{id: '4',year: '2024年',uploadValue: '0',uploadValueName: '完全符合'}]},{indexShowName: '测试002',type: '2',reportDate: '2023-12-01 13:45:53',uploadValueList: [{id: '5',year: '2021年',uploadValue: '99.00'},{id: '6',year: '2022年',uploadValue: '98.00'},{id: '7',year: '2023年',uploadValue: '77.00'},{id: '8',year: '2024年',uploadValue: '34.00'}]}]if (records && records.length > 0) {// 使用第一个元素的 uploadValueList 来创建列this.tableColumns = records[0].uploadValueList.map((item) => ({year: item.year, // 使用 year 作为列的标签id: item.id // 用于做key}))}this.tableData = recordsthis.pages.total = total} else {this.$message.error(res.message)}}).finally(() => {this.tableLoading = false})},// 单元格合并 {当前行row、当前列column、当前行号rowIndex、当前列号columnIndex}arraySpanMethod({ row, column, rowIndex, columnIndex }) {// 类型1,且动态数据长度>1if (row.type === '1' && row?.uploadValueList?.length > 1) {const len = row?.uploadValueList?.length// 合并从下标为0开始的【下标为3的第四列,动态数据长度】if ( columnIndex > 2 && columnIndex <= 2 + Number(len) ) {return {rowspan: 1,colspan: columnIndex === 3 ? len : 0}}}},

相关文章:

  • 【改进YOLOv8】融合高效网络架构 CloAtt的焊缝识别系统
  • Linux下Redis安装及配置
  • 好用免费的AI换脸5个工具
  • form表单的数据校验可以自定义规则,定义验证器(validator)
  • 【USRP】5G / 6G 原型系统 5g / 6G prototype system
  • Large Language Models areVisual Reasoning Coordinators
  • CSS新手入门笔记整理:CSS列表样式
  • 如何判断数据库慢 SQL 查询?
  • Windows+WSL开发环境下微服务注册(Consul)指定IP
  • ros来保存图像和保存记录视频的方法---gmsl相机保存视频和图片
  • linux学习之详解文件
  • git 配置多端多个账号(码云、github、gitlab)
  • vue3 中使用 sse 最佳实践,封装工具
  • #HarmonyOS:软件安装window和mac预览Hello World
  • Java中富文本转markdown
  • 230. Kth Smallest Element in a BST
  • Codepen 每日精选(2018-3-25)
  • const let
  • emacs初体验
  • js学习笔记
  • php中curl和soap方式请求服务超时问题
  • Promise初体验
  • Sass 快速入门教程
  • Spring Boot快速入门(一):Hello Spring Boot
  • tab.js分享及浏览器兼容性问题汇总
  • vue2.0一起在懵逼的海洋里越陷越深(四)
  • vuex 学习笔记 01
  • vue和cordova项目整合打包,并实现vue调用android的相机的demo
  • 阿里云应用高可用服务公测发布
  • 从重复到重用
  • 基于webpack 的 vue 多页架构
  • 区块链将重新定义世界
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 手机端车牌号码键盘的vue组件
  • 一些关于Rust在2019年的思考
  • 译有关态射的一切
  • 自定义函数
  • 看到一个关于网页设计的文章分享过来!大家看看!
  • 7行Python代码的人脸识别
  • 如何正确理解,内页权重高于首页?
  • 小白应该如何快速入门阿里云服务器,新手使用ECS的方法 ...
  • ​LeetCode解法汇总1410. HTML 实体解析器
  • #NOIP 2014# day.2 T2 寻找道路
  • #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
  • (0)Nginx 功能特性
  • (1)(1.9) MSP (version 4.2)
  • (12)Hive调优——count distinct去重优化
  • (13)Latex:基于ΤΕΧ的自动排版系统——写论文必备
  • (C++17) optional的使用
  • (HAL)STM32F103C6T8——软件模拟I2C驱动0.96寸OLED屏幕
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (附源码)spring boot网络空间安全实验教学示范中心网站 毕业设计 111454
  • (论文阅读32/100)Flowing convnets for human pose estimation in videos
  • (免费分享)基于springboot,vue疗养中心管理系统
  • (转)linux 命令大全