项目中使用el-table实现行合并及合并后序号不连续解决方案

前言

el-table作为前端开发中最常用的数据表格组件,行合并需求在实际项目中频繁出现,但官方文档介绍较为简略。本文结合开发中的实际需求,提供了一种简单、易用的el-table实现行合并的参考方案。

数据格式及实现效果图参考

1.实现效果图

2.数据格式

产品名称、sku编码、sku数量的数据来自数组orderProductVOS

具体实现

1.扁平化数组生成新的表格数据

js 复制代码
   flattenData(data) {
      const result = []
      data.forEach((order, index) => {
        const rowIndexObj = { rowIndex: index + 1 }  //记录索引值防止合并行后序号不连续
        order.orderProductVOS.forEach(product => {
          result.push({
            ...order,
            ...rowIndexObj,
            productName: product.productName,
            skuCode: product.productSkuCode,
            quantity: product.productQuantity
          })
        })
      })
      this.flattenedData = result
    }

2.合并行的计算方法

js 复制代码
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // 定义不需要合并的列(通过列名识别)
      const notMergeColumns = ['产品名称', 'sku编码', 'sku数量']

      // 判断当前列是否是不需要合并的列
      const isNotMergeColumn = notMergeColumns.includes(column.label)

      if (isNotMergeColumn) {
        // 不需要合并的列,保持原样
        return { rowspan: 1, colspan: 1 }
      } else {
        // 需要合并的列
        // 找到当前订单号(该表格的订单号是唯一的,可以根据情况取一个唯一的值)
        const currentOrderNumber = row.orderNumber

        // 计算当前订单在表格中的起始行索引
        let startRowIndex = rowIndex
        while (startRowIndex > 0 && this.flattenedData[startRowIndex - 1].orderNumber === currentOrderNumber) {
          startRowIndex--
        }

        // 如果当前行是订单的第一行,则合并整个订单的行数
        if (rowIndex === startRowIndex) {
          // 计算当前订单的总行数
          let rowCount = 0
          while (startRowIndex + rowCount < this.flattenedData.length &&
            this.flattenedData[startRowIndex + rowCount].orderNumber === currentOrderNumber) {
            rowCount++
          }

          return { rowspan: rowCount, colspan: 1 }
        } else {
          // 非第一行,不显示
          return { rowspan: 0, colspan: 0 }
        }
      }
    },

3.表格组件的模板代码

vue 复制代码
      <el-table 
      :data="flattenedData" 
      border 
      style="width: 100%" 
      :header-cell-style="{height:'45px',color:'#303133'}"
      header-align="left" 
      :max-height="tableHeight" 
      v-loading="loadingTable" 
      @selection-change="handleSelectionChange" 
      :span-method="objectSpanMethod"
      >
        <el-table-column type="selection" align="center" />
        <el-table-column label="序号" prop="rowIndex" width="50" align="center" fixed="left">
        </el-table-column>
        ...
      </el-table>

结语

按照上述方法即可实现表格的行合并效果。需要注意的是,如果在列属性中使用 type="index" 自动生成序号,行合并后可能会导致序号显示不连续。为解决这一问题,可以在外层循环中手动维护一个索引变量,通过该变量动态生成连续的序号,从而确保序号的连贯性。

相关推荐
葡萄城技术团队1 分钟前
一个单元格放置两个日期选择器:用 SpreadJS CellButtons 录入日期范围
前端
马可家的菠萝1 小时前
自动保存已经有了,为什么笔记软件还需要“历史版本”?
前端·后端·架构
半个落月1 小时前
从 "use client" 到 Route Handler:用 Todos 理解 Next.js 水合与全栈请求
前端·react.js·next.js
qq_452396232 小时前
第七篇:《大型前端项目的模块化与目录结构设计》
前端
你脑门上的脚印2 小时前
Vue 项目从零实现语音转文字、文字转语音功能(完整可用 + 踩坑指南)
前端·vue.js
paopaokaka_luck2 小时前
基于springboot3+vue3的车间生产管理系统(Echarts图形化分析、BI报表)
java·前端·spring boot·学习·echarts
程序员鱼皮2 小时前
3 大 DeepSeek Harness 进阶玩法,招多个大肥鱼帮我干活!
前端·后端·ai编程
KoPa2 小时前
HeySmart:大模型开源网关基座-请求生命周期与钩子引擎
前端·后端
七牛开发者2 小时前
Coding Agent 如何跑稳长任务?从上下文管理到运行时状态
前端·javascript·后端
半个落月3 小时前
从 CSR 到 Server Component:吃透 Next.js 16 App Router 路由、布局与 SEO
前端·next.js