在写项目过程中,有一个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];
}
}
},