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. 深挖 Python 元组 pt.1

    哈喽大家好,我是咸鱼 好久不见甚是想念,2023 年最后一次法定节假日已经结束了,不知道各位小伙伴是不是跟咸鱼一样今天就开始"搬砖"了呢? 我们知道元组(tuple)是 Pytho ...

  2. 漏洞扫描与安全加固之Apache Axis组件

    一.Apache Axis组件高危漏洞自查及整改 Apache Axis组件存在由配置不当导致的远程代码执行风险. 1. 影响版本 Axis1 和Axis2各版本均受影响 2. 处置建议 1)禁用此服 ...

  3. Markdown 包含其他文件静态渲染工具

    1. 前言 在 GitHub 上写文档,很多时候要插入 uml,像 mermaid 这种可以直接在 GitHub/GitLab 中渲染的一般直接写个 code block 进去,但是这样造成一个问题就 ...

  4. .Net核心级的性能优化(GC篇)

    1.前言 大部分人对于.Net性能优化,都停留在业务层面.或者简单的.Net框架配置层面.本篇来看下.Net核心部分GC垃圾回收配置:保留VM,大对象,独立GC,节省内存等.Net8里面有很多的各种G ...

  5. 再学Blazor——概述

    简介 Blazor 是一种 .NET 前端 Web 框架,同时支持服务器端呈现和客户端交互性. 使用 C# 语言创建丰富的交互式 UI 共享前后端应用逻辑 可以生成混合桌面和移动应用 受益于 .NET ...

  6. dig 简明教程

    哈喽大家好,我是咸鱼 不知道大家在日常学习或者工作当中用 dig 命令多不多 dig 是 Domain Information Groper 的缩写,对于网络管理员和在域名系统(DNS)领域工作的小伙 ...

  7. 微软发布开源平台 Radius:高效构建、运行基于Dapr 云原生应用程序

    Microsoft Azure 孵化团队很高兴地宣布[1]推出一个名为 Radius 的新开放应用程序平台,该平台将应用程序置于每个开发阶段的中心,重新定义应用程序的构建.管理和理解方式.Radius ...

  8. 简单实现.NET Hook与事件模拟

    最近玩<星露谷物语>上瘾,本来是看着个休闲游戏,现在玩成修仙游戏了,上百个小时浑身是肝,中午午休习惯都强行给改了. 虽然挺有意思,但是太肝了,入坑前请谨慎.补充一下,这个游戏应该是基于 X ...

  9. Instagram 早期技术架构

    哈喽大家好,我是咸鱼 想必大家都听说过 Instagram ,它是全球最受欢迎的社交媒体平台之一,拥有数十亿的活跃用户 Instagram 诞生于 2010 年,上线一周就坐拥 10 万注册用户,一年 ...

  10. 【pwn】整数溢出

    这是ctfshow上面的一道题 这边v1和v2定义时都是int,有符号整数,想让v1-v2=9,可以考虑负数,但是这个函数过滤了负号   if ( strchr(s, 45) )    return ...