1. 需求效果图:

2. 接口数据格式:

点击查看代码
  const list = [
{
contractNo: "CAI-20220801001",
contractItem: "用户质量指数",
count: 15234,
customerItems: [
{
contractNo: null,
contractItem: "反欺诈分",
price: null,
priceType: null,
count: 7302,
amount: null,
},
{
contractNo: null,
contractItem: "逾期评估分",
price: null,
priceType: null,
count: 8234,
amount: null,
},
],
},
{
contractNo: "CAI-20220801001",
contractItem: "价值经营分",
count: 900,
customerItems: [
{
contractNo: null,
contractItem: "价值经营分",
price: null,
priceType: null,
count: 15234,
amount: null,
},
],
},
];

3. 实现过程:

element plus 官网中有描述合并表格行列的方法:点此查看文档

表格部分代码如下:

  <el-table
class="table-box"
border
m-t-27px
:data="tableData"
style="width: 100%"
height="605px"
:header-cell-style="{
background: '#FAFAFA',
color: 'rgba(0, 0, 0, 0.40)',
}"
:span-method="spanMethod"
>
<el-table-column prop="contractNo" label="合同编号" />
<el-table-column prop="contractItem" label="合同项:" /> <el-table-column prop="customerItemsContractItem" label="对客合同项" />
<el-table-column
prop="customerItemsCount"
label="当天收费量(次)"
align="right"
>
<template #default="scope">
<span>{{ $utils.addCommas(scope.row.customerItemsCount) }}</span>
</template>
</el-table-column> <el-table-column prop="count" label="合同项当天收费量" align="center">
<template #default="scope">
<span>{{ $utils.addCommas(scope.row.count) }}</span>
</template>
</el-table-column>
</el-table>

根据element文档所给的示例结合需求分析,需要把接口返回的数据进行格式化处理,把嵌套数据进行拆分,这里开始我是这样实现的(坑):

const formatListFn = (list) => {
let arr = [];
for (let i = 0; i < list.length; i++) {
const item = list[i];
for (let j = 0; j < item["customerItems"].length; j++) {
const itm = item["customerItems"][j];
arr.push({
customerItemsContractItem: itm.contractItem,
customerItemsCount: itm.count,
...item,
});
}
}
// console.log(arr);
return arr;
};

此处把嵌套数组中需要使用的字段提取了出来,自定义了属性名customerItemsContractItemcustomerItemsCount。其他属性原样复制即可,不需要进行额外操作。

然后定义并给 table 传入span-method方法来实现合并行或列:

const spanMethod = ({ row, column, rowIndex, columnIndex }) => {
const len = row["customerItems"].length;
if (columnIndex > 3 || columnIndex < 2) {
return {
rowspan: len,
colspan: 1,
};
}
};

根据索引给需要合并的部分设置rowspan,值为嵌套数组的长度。

此时以为大功告成,结果保存后查看页面如下:



可以看到框选处的展示是有问题的,对比需求效果如下:

4. 问题排查处理:

经过多处排查后发现问题出在spanMethod中。在spanMethod方法中,只判断了列索引,所以同一列可能会重复多次执行rowspan,导致合并错乱。

嵌套行被拆分后,原来两行数据被拆分成了三行,因此rowspan操作会多次执行。

解决办法就是让多行嵌套数据只执行一次rowspan操作。可以给每行数据添加标识,区分是否进行合并操作。这里我是这样处理的:

首先给每行数据设置rowspan值

const formatListFn = (list) => {
let arr = [];
for (let i = 0; i < list.length; i++) {
const item = list[i];
for (let j = 0; j < item["customerItems"].length; j++) {
const itm = item["customerItems"][j];
arr.push({
customerItemsContractItem: itm.contractItem,
customerItemsCount: itm.count,
...item,
rowspan: j === 0 ? item.customerItems.length : 0,
});
}
}
// console.log(arr);
return arr;
};

注意这行 rowspan: j === 0 ? item.customerItems.length : 0,,每次遍历customerItems时,给第一项设置rowspan为customerItems.length。否则设置为0,不让它合并。

然后修改spanMethod方法

const spanMethod = ({ row, column, rowIndex, columnIndex }) => {
if (columnIndex > 3 || columnIndex < 2) {
return {
rowspan: row.rowspan,
colspan: 1,
};
}
};

注意这行rowspan: row.rowspan

刷新页面查看,问题解决:

el-table 设置合并行或列时,显示错乱问题的更多相关文章

  1. ABAP ALV 颜色设置(行,列,单元格)

    BCALV_EDIT_03 http://blog.sina.com.cn/s/blog_a87b19300102who3.html 关于ALV表格颜色,这种需求在项目中会经常用到. 列颜色 列的颜色 ...

  2. ALV 颜色设置(行,列,单元格)

    [转自:https://www.cnblogs.com/mingdashu/p/color_alv.html] BCALV_EDIT_03 http://blog.sina.com.cn/s/blog ...

  3. Unity中调用Windows窗口句柄以及根据需求设置并且解决扩展屏窗体显示错乱/位置错误的Bug

    问题背景: 现在在搞PC端应用开发,我们开发中需要调用系统的窗口以及需要最大化最小化,缩放窗口拖拽窗口,以及设置窗口位置,去边框等功能 解决根据: 使用user32.dll解决 具体功能: Unity ...

  4. js 创建Table,每行3列的方式

    table: <table style="width:100%" id="appDatas"></table> 1. var table ...

  5. WebDataGrid设置某行某列的值

    <ig:WebDataGrid ID="grid"  OnRowSelectionChanged="grid_RowSelectionChanged" O ...

  6. table中超过长度的列,显示省略号

    <style type="text/css"> .table-ellipsis { table-layout: fixed; width: 100%; } .table ...

  7. Unity3d 帧率设置 及在游戏执行时显示帧率

    在Unity3d 中能够通过代码设置 来限定游戏帧率. Application.targetFrameRate=-1; 设置为 -1 表示不限定帧率. 转自http://blog.csdn.net/h ...

  8. SQL查询语句行转列横向显示

    http://blog.163.com/dreamman_yx/blog/static/26526894201121595846270/

  9. layui点击table表格的每一格时显示相应的内容

    $(document).on('click','.layui-table-cell',function(){ // $("p").css({"background-col ...

  10. 用JavaScript,获取Table中指定的行、列

    前言: 先要谢谢George Wing的慷慨赠书<悟透JavaScript>,让我更加感受到了技术交流的重要性,呵呵~ 进入正题,面试题中有一题:如何通过JavaScript获取Table ...

随机推荐

  1. Ds100p -「数据结构百题」11~20

    11.P3203 [HNOI2010]弹飞绵羊 某天,\(Lostmonkey\) 发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏. 游戏一开始,\(Lostmonkey ...

  2. 兴达易控modbus转profinet网关三菱变频器通讯

    兴达易控modbus转profinet网关与三菱变频器通讯 本案例分享兴达易控modbus转profinet网关(MDPN100)连接西门子1200plc,实现三菱变频器485通讯兼容转modbusT ...

  3. JAVA中三种I/O框架——BIO、NIO、AIO

    一.BIO(Blocking I/O) BIO,同步阻塞IO模型,应用程序发起系统调用后会一直等待数据的请求,直至内核从磁盘获取到数据并拷贝到用户空间: 在一般的场景中,多线程模型下的BIO是成本较低 ...

  4. fatal: 无法访问 'https://github.com/nmww/lingyun.git/':Failed to connect to github.com port 443 after 13 ms: Connection refused

    fatal: 无法访问 'https://github.com/nmww/lingyun.git/':Failed to connect to github.com port 443 after 13 ...

  5. 【源码解读(一)】EFCORE源码解读之创建DBContext查询拦截

    引言 在网上很少看到有关于系统讲解EFCore源码的,可能大概也许是因为EFCore的源码总体是没有asp.net web的源码流程清晰,正如群友所说,EFCore的源码大致看起来有点凌乱,与其说凌乱 ...

  6. [ABC321C] 321-like Searcher

    Problem 题目简述 给你一个 \(K\),求出 \([1 \sim K]\) 区间内有多少个 321-like Number. 321-like Number 的定义: 每一位上的数字从左到右严 ...

  7. WebSocket魔法师:打造实时应用的无限可能

    1.背景 在开发一些前端页面的时候,总是能接收到这样的需求:如何保持页面并实现自动更新数据呢?以往的常规做法,是前端使用定时轮询后端接口,获取响应后重新渲染前端页面,这种做法虽然能达到类似的效果,但是 ...

  8. Guess-the-Number

    第一次做压缩包逆向 了解到的用jd-gui打开 得到的简单代码可以在在线平添运行

  9. 手撕Vue-Router-提取路由信息

    前言 好了经过上一篇的学习,我们已经知道了如何监听 Hash 的变化,如何监听路径的一个变化,本篇我们就可以来实现我们自己的 VueRouter 了, 那么怎么实现呢,在实现之前我们先来回顾一下官方的 ...

  10. Cadence SPB 22.1 --学习基础01Day

    1.电路图设计 ①.原理图设计 原理图符号-->原理图库:代替实际电子元器件的符号,主要就是引脚数目.引脚序号与实物对应: ②.PCB设计 PCB符合-->PCB封装库:电子元器件的各种实 ...