el-table实现表格合并与v-for多层循环嵌套失效解决办法

在写项目过程中,有一个el-table表格固定列合并,且与v-for结合循环多层数组的需求。主要是解决循环数组中每项item里的list,实现合并表格。

1. 下图为整体实现效果

2. 下面为v-for循环的数据结构,以及el-table的展示,绑定el-table的表格合并的方法为::span-method

<div v-for="(item, index) in tableDataAll" :key="index">
      <!-- 表格部分 -->
          <el-table
            :data="item.aeromatBoxLocationList"
            :span-method="
              ({ row, column, rowIndex, columnIndex }) =>
                objectSpanMethod(
                  row,
                  column,
                  rowIndex,
                  columnIndex,
                  item.aeromatBoxLocationList
                )
            "
            border
            style="width: 100%"
          >
            <el-table-column
              prop="squareCode"
              label="栅格序号"
              min-width="90"
              align="center"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <span>
                  {{ scope.row.squareCode ? scope.row.squareCode : "暂无" }}
                </span>
              </template>
            </el-table-column>
            <el-table-column
              prop="subBoxFloorage"
              label="栅格底面积"
              min-width="90"
              align="center"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <span>
                  {{
                    scope.row.subBoxFloorage ? scope.row.subBoxFloorage : "暂无"
                  }}
                </span>
              </template>
            </el-table-column>
            <el-table-column
              prop="subBoxHeight"
              label="栅格高"
              min-width="90"
              align="center"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <span>
                  {{ scope.row.subBoxHeight ? scope.row.subBoxHeight : "暂无" }}
                </span>
              </template>
            </el-table-column>
            <el-table-column
              prop="ceSeq"
              label="册序号"
              align="center"
              min-width="90"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <span>
                  {{ scope.row.ceSeq ? scope.row.ceSeq : "暂无" }}
                </span>
              </template>
            </el-table-column>
            <el-table-column
              prop="model"
              label="型号"
              align="center"
              min-width="90"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <span>
                  {{ scope.row.model ? scope.row.model : "暂无" }}
                </span>
              </template>
            </el-table-column>
            <el-table-column
              prop="importance"
              label="重要度"
              align="center"
              min-width="90"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <span>
                  {{ scope.row.importance ? scope.row.importance : "暂无" }}
                </span>
              </template>
            </el-table-column>
            <el-table-column
              prop="weight"
              label="重量(kg)"
              align="center"
              min-width="90"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <span>
                  {{ scope.row.weight ? scope.row.weight : "暂无" }}
                </span>
              </template>
            </el-table-column>
            <el-table-column
              prop="volume"
              label="体积(cm³)"
              align="center"
              min-width="90"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <span>
                  {{ scope.row.volume ? scope.row.volume : "暂无" }}
                </span>
              </template>
            </el-table-column>
            <el-table-column
              prop="floorage"
              label="底面积(cm²)"
              align="center"
              min-width="90"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <span>
                  {{ scope.row.floorage ? scope.row.floorage : "暂无" }}
                </span>
              </template>
            </el-table-column>
            <el-table-column
              prop="maxSide"
              label="最长边(cm)"
              align="center"
              min-width="90"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <span>
                  {{ scope.row.maxSide ? scope.row.maxSide : "暂无" }}
                </span>
              </template>
            </el-table-column>
          </el-table>
    </div>

3. el-table整体绑定的tableDataAll数据格式

    tableDataAll: [
        {
            "uuid": "1",
            "schemeId": 2,
            "aeromatBoxLocationList": [
                {
                    "uuid": "1",
                    "squareCode": 1,
                    "subBoxFloorage": 224440.0,
                    "subBoxHeight": 492.0,
                    "ceSeq": "1030000107",
                    "name": "1111",
                    "model": "TS3-2281",
                    "importance": "3",
                    "weight": 67.0,
                    "volume": 2800.0,
                    "floorage": 400.0,
                    "maxSide": 20.0
                },
                {
                    "uuid": "1",
                    "squareCode": 1,
                    "subBoxFloorage": 224440.0,
                    "subBoxHeight": 492.0,
                    "ceSeq": "1030000050",
                    "name": "2222",
                    "model": "TS3-228",
                    "importance": "3",
                    "weight": 2.0,
                    "volume": 4200.0,
                    "floorage": 600.0,
                    "maxSide": 30.0
                },
                {
                    "uuid": "1",
                    "squareCode": 2,
                    "subBoxFloorage": 448880.0,
                    "subBoxHeight": 492.0,
                    "ceSeq": "1030000103",
                    "name": "3333",
                    "model": "KFS-281",
                    "importance": "1",
                    "weight": 5.0,
                    "volume": 1000.0,
                    "floorage": 1000.0,
                    "maxSide": 50.0
                },
                {
                    "uuid": "1",
                    "squareCode": 2,
                    "subBoxFloorage": 448880.0,
                    "subBoxHeight": 492.0,
                    "ceSeq": "1030000103",
                    "name": "4444",
                    "model": "KFS-281",
                    "importance": "1",
                    "weight": 5.0,
                    "volume": 1000.0,
                    "floorage": 1000.0,
                    "maxSide": 50.0
                },
                {
                    "uuid": "1",
                    "squareCode": 2,
                    "subBoxFloorage": 448880.0,
                    "subBoxHeight": 492.0,
                    "ceSeq": "1030000103",
                    "name": "5555",
                    "model": "KFS-281",
                    "importance": "1",
                    "weight": 5.0,
                    "volume": 1000.0,
                    "floorage": 1000.0,
                    "maxSide": 50.0
                },
                {
                    "uuid": "1",
                    "squareCode": 2,
                    "subBoxFloorage": 448880.0,
                    "subBoxHeight": 492.0,
                    "ceSeq": "1030000050",
                    "name": "6666",
                    "model": "TS3-228",
                    "importance": "3",
                    "weight": 2.0,
                    "volume": 4200.0,
                    "floorage": 600.0,
                    "maxSide": 30.0
                },
                {
                    "uuid": "1",
                    "squareCode": 2,
                    "subBoxFloorage": 448880.0,
                    "subBoxHeight": 492.0,
                    "ceSeq": "1030000050",
                    "name": "7777",
                    "model": "TS3-228",
                    "importance": "3",
                    "weight": 2.0,
                    "volume": 4200.0,
                    "floorage": 600.0,
                    "maxSide": 30.0
                }
            ]
        },
        {
            "uuid": "2",
            "schemeId": 2,
            "aeromatBoxLocationList": [
                {
                    "uuid": "2",
                    "squareCode": 1,
                    "subBoxFloorage": 224440.0,
                    "subBoxHeight": 492.0,
                    "ceSeq": "1030000048",
                    "name": "1111",
                    "model": "CYT-168",
                    "importance": "3",
                    "weight": 5.0,
                    "volume": 2000.0,
                    "floorage": 400.0,
                    "maxSide": 20.0
                },
                {
                    "uuid": "2",
                    "squareCode": 1,
                    "subBoxFloorage": 224440.0,
                    "subBoxHeight": 492.0,
                    "ceSeq": "1030000048",
                    "name": "2222",
                    "model": "CYT-168",
                    "importance": "3",
                    "weight": 5.0,
                    "volume": 2000.0,
                    "floorage": 400.0,
                    "maxSide": 20.0
                },
                {
                    "uuid": "2",
                    "squareCode": 1,
                    "subBoxFloorage": 224440.0,
                    "subBoxHeight": 492.0,
                    "ceSeq": "1030000048",
                    "name": "3333",
                    "model": "CYT-168",
                    "importance": "3",
                    "weight": 5.0,
                    "volume": 2000.0,
                    "floorage": 400.0,
                    "maxSide": 20.0
                },
                {
                    "uuid": "2",
                    "squareCode": 1,
                    "subBoxFloorage": 224440.0,
                    "subBoxHeight": 492.0,
                    "ceSeq": "1030000107",
                    "name": "4444",
                    "model": "TS3-2281",
                    "importance": "3",
                    "weight": 67.0,
                    "volume": 2800.0,
                    "floorage": 400.0,
                    "maxSide": 20.0
                },
                {
                    "uuid": "2",
                    "squareCode": 2,
                    "subBoxFloorage": 448880.0,
                    "subBoxHeight": 492.0,
                    "ceSeq": "1030000050",
                    "name": "5555",
                    "model": "TS3-228",
                    "importance": "3",
                    "weight": 2.0,
                    "volume": 4200.0,
                    "floorage": 600.0,
                    "maxSide": 30.0
                },
                {
                    "uuid": "2",
                    "squareCode": 2,
                    "subBoxFloorage": 448880.0,
                    "subBoxHeight": 492.0,
                    "ceSeq": "1030000050",
                    "name": "6666,
                    "model": "TS3-228",
                    "importance": "3",
                    "weight": 2.0,
                    "volume": 4200.0,
                    "floorage": 600.0,
                    "maxSide": 30.0
                },
                {
                    "uuid": "2",
                    "squareCode": 2,
                    "subBoxFloorage": 448880.0,
                    "subBoxHeight": 492.0,
                    "ceSeq": "1030000103",
                    "name": "7777",
                    "model": "KFS-281",
                    "importance": "1",
                    "weight": 5.0,
                    "volume": 1000.0,
                    "floorage": 1000.0,
                    "maxSide": 50.0
                },
                {
                    "uuid": "2",
                    "squareCode": 2,
                    "subBoxFloorage": 448880.0,
                    "subBoxHeight": 492.0,
                    "ceSeq": "1030000103",
                    "name": "8888",
                    "model": "KFS-281",
                    "importance": "1",
                    "weight": 5.0,
                    "volume": 1000.0,
                    "floorage": 1000.0,
                    "maxSide": 50.0
                },
                {
                    "uuid": "2",
                    "squareCode": 2,
                    "subBoxFloorage": 448880.0,
                    "subBoxHeight": 492.0,
                    "ceSeq": "1030000103",
                    "name": "9999",
                    "model": "KFS-281",
                    "importance": "1",
                    "weight": 5.0,
                    "volume": 1000.0,
                    "floorage": 1000.0,
                    "maxSide": 50.0
                }
            ]
        }
]

4. 下面是el-table中自带的span-method方法绑定的函数objectSpanMethod

row.squareCode代表每一行数据的唯一标识,也是合并数据时判断的唯一标识

objectSpanMethod(row, column, rowIndex, columnIndex, data) {
      // 获取当前行的唯一标识
      let squareCode = row.squareCode;
     // columnIndex 代表每一列的index值,<=2是指前三列
      if (columnIndex <= 2) {
        if (!rowIndex || data[rowIndex - 1].squareCode !== squareCode) {
          let _row,
            _col = 1;
          let startIndex = rowIndex; // 找到每行相同标识的起始位置
          let endIndex = data.findLastIndex(
            (item) => item.squareCode === squareCode
          );  // 找到每行相同标识的末尾位置
          _row = endIndex - startIndex + 1;
          console.log('[_row, _col]',[_row, _col]);
          return [_row, _col];
        } else {
          return [0, 0];
        }
      }
    },

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
el-tableElement UI框架中的一个表格组件,可以用于展示和编辑数据。要实现Excel中的合并行和合并列功能,可以使用el-table的特定属性和方法。 要实现合并行,可以使用el-table的span-method属性。该属性接受一个函数,用于计算每个单元格的合并行数。在函数中,可以根据当前单元格的值和相邻单元格的值来判断是否需要合并行,并返回合并行数。 要实现合并列,可以使用el-table-column的prop属性。该属性指定了当前列对应数据对象中的字段名。如果多个列的prop属性相同,那么这些列将会合并为一列。 下面是一个示例代码,演示了如何使用el-table实现合并行和合并列: ```html <template> <el-table :data="tableData" :span-method="handleSpanMethod"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="gender" label="性别"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 18, gender: '男' }, { name: '李四', age: 20, gender: '女' }, { name: '王五', age: 18, gender: '男' }, { name: '赵六', age: 22, gender: '女' }, ], }; }, methods: { handleSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 0) { // 判断当前列是否为第一列 if (rowIndex === 0 || row.name !== this.tableData[rowIndex - 1].name) { // 判断当前行是否为第一行或者与上一行的name字段值不同 const rowspan = this.getRowspan(row.name, columnIndex); if (rowspan > 1) { return { rowspan, colspan: 1, }; } } } }, getRowspan(name, columnIndex) { let rowspan = 1; for (let i = 1; i < this.tableData.length; i++) { if (this.tableData[i].name === name) { rowspan++; } else { break; } } return rowspan; }, }, }; </script> ``` 在上述代码中,handleSpanMethod函数用于计算每个单元格的合并行数。getRowspan函数用于计算每个姓名相同的单元格的合并行数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值