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

el-table合并单元格

这个是后端返回的数据结构,按id字段,把相同内容的单元格的进行合并

<el-tableborderv-loading="tableLoading"element-loading-text="数据加载中":data="tableData":span-method="mergeRows":height="tableMaxHeight"><el-table-column type="selection" prop="index" width="55"></el-table-column><el-table-column label="序号" prop="index" width="50" fixed="left"></el-table-column><el-table-column label="单据编号" prop="documentCode" min-width="150" :show-overflow-tooltip="true" /><el-table-column label="单据类型" prop="documentType" :show-overflow-tooltip="true"><template slot-scope="scope"><dict-tag :options="dict.type.document_type" :value="scope.row.documentType"></dict-tag></template></el-table-column><el-table-column label="单据日期" prop="documentDate" min-width="120" :show-overflow-tooltip="true" /><el-table-column label="单据状态" prop="status" :show-overflow-tooltip="true"><template slot-scope="scope"><dict-tag :options="dict.type.document_status" :value="scope.row.status"></dict-tag></template></el-table-column><el-table-column label="物料编码" prop="partCode" min-width="100" :show-overflow-tooltip="true" /><el-table-column label="物料名称" prop="partName" :show-overflow-tooltip="true" /><el-table-column label="申购数量" prop="quantityNum" :show-overflow-tooltip="true" /><el-table-column label="项目主数据" prop="projectData" min-width="110" :show-overflow-tooltip="true" /><el-table-column label="项目名称" prop="projectName" :show-overflow-tooltip="true" /><el-table-column label="申请人员" prop="applicant" min-width="100" :show-overflow-tooltip="true" /><el-table-column label="申请部门" prop="deptName" min-width="120" :show-overflow-tooltip="true" /><el-table-column label="采购人员" prop="purchasePerson" min-width="120" :show-overflow-tooltip="true" /><el-table-column label="已转采购数" prop="convertedNum" min-width="110" :show-overflow-tooltip="true" /><el-table-column label="未转采购数" prop="unconvertedNum" min-width="110" :show-overflow-tooltip="true" /></el-table>
data() {return {//需要合并的字段mergeFields: ["index","documentCode","documentType","documentDate","status","applicant","deptName","purchasePerson","tool"],// 合并规则mergeRules: {},}
},
methods: {
/*** 计算合并规则* @param {String} groupKey 分组依据字段(这里用id)*/calcMergeRules(groupKey) {const rules = {};// 初始化需要合并的字段规则this.mergeFields.forEach((field) => (rules[field] = []));let sameCount = 1; // 相同id的连续行数this.tableData.forEach((row, index) => {if (index === 0) {// 第一行默认占1行this.mergeFields.forEach((field) => rules[field].push(1));} else {// 判断与上一行是否同组const isSameGroup = row[groupKey] === this.tableData[index - 1][groupKey];if (isSameGroup) {sameCount++;// 当前行设为0(不显示),上一行累加this.mergeFields.forEach((field) => {rules[field].push(0);rules[field][index - sameCount + 1] = sameCount;});} else {// 不同组则重置计数sameCount = 1;this.mergeFields.forEach((field) => rules[field].push(1));}}});return rules;},/*** 合并单元格方法*/mergeRows({ column, rowIndex }) {// 找到当前列对应的字段const field = column.property;// 如果是需要合并的字段,则应用规则if (this.mergeFields.includes(field)) {const rowspan = this.mergeRules[field][rowIndex];return { rowspan, colspan: rowspan > 0 ? 1 : 0 };}// 不需要合并的列返回默认值return { rowspan: 1, colspan: 1 };},// 调接口获取数据getList() {this.tableLoading = true;queryPurchaseReqList(this.queryParams).then((res) => {if (res.code === 200) {this.tableData = res.rows || [];this.total = res.total;this.tableLoading = false;// 初始化合并规则(按id分组)this.mergeRules = this.calcMergeRules("id");}}).catch((err) => null);},
}

效果

通过以上方法就能实现合并了,主要用到了el-table的  span-method

这世界很喧嚣,做你自己就好

http://www.dtcms.com/a/339956.html

相关文章:

  • 接口自动化测试大全(python+pytest+allure)
  • Angular极速入门
  • 【CUDA教程--3】通过简单的矩阵运算入门CUDA
  • cursor+mcp-clickhouse进行数据分析
  • Spring循环依赖源码调试详解,用两级缓存代替三级缓存
  • JB4-9-任务调度
  • 网络通信基础:从数据链路层到传输层
  • 如何解决pip安装报错ModuleNotFoundError: No module named ‘paramiko’问题
  • Leetcode 3652. Best Time to Buy and Sell Stock using Strategy
  • 【20250819】mathtype的使用
  • Sklearn 机器学习 房价预估 计算房价和特征值的相关性
  • 差分约束系统SPFA判负环
  • 【自动驾驶】8月 端到端自动驾驶算法论文(arxiv20250819)
  • 决策树1.1
  • 设计模式笔记_行为型_解释器模式
  • 集成电路学习:什么是Thresholding阈值处理
  • PowerBI VS FineBI VS QuickBI实现帕累托分析
  • Go 并发入门:从 goroutine 到 worker pool
  • 用 C++ 构建高性能测试框架:从原型到生产实战指南
  • Python 项目里的数据预处理工作(数据清洗步骤与实战案例详解)
  • 在线客服系统访客表的设计与实现-增加最新消息字段
  • Task01: CAMEL环境配置及第一个Agent
  • Kubernetes Ingress实战:从环境搭建到应用案例
  • C语言基础:(十九)数据在内存中的存储
  • Java线程池参数配置的坑:`corePoolSize=0` + `LinkedBlockingQueue`直接变成串行执行
  • Python爬虫第二课:爬取HTML静态网页之《某某小说》 小说章节和内容完整版
  • 智驾-AEB
  • 羟氯喹通过抑制抗磷脂综合征诱导的绒毛外滋养细胞过度自噬
  • 【模版匹配】基于深度学习
  • 洛谷 P2834 纸币问题 3-普及-