本人处理合并踩坑小记
踩坑注意事项:expand-change 树形展开折叠 至少要在 2.9.2 以上版本才有返回
2.8.2
2.9.2+
首先看官网的table 树形懒加载案例
需求:1、根据num 返回值 为表格序列号 ,父子级中的num值相同 ,序列号相同 合并单元格
思路: 1、利用官网树形数据与懒加载例子
2、无序序号,将type=""设置为空 防止箭号防止第一格
3、 expand-change折叠展开触发事件、:span-method 处理合并单元格
4、考虑是可多出展开折叠转态,遍历数组 父级数据都添加expanded:false默认折叠,根据点击当前行expand-change 返回状态 修改对应的expanded值 达到管理展开折叠的目的
5、如果是load懒加载 可根据lload返回row的值 遍历对应当前二级数据 对应的父级 添加cound记录子级长度;如果数组中直接children时,直接遍历获取children的长度即可。
6、综上,根据expanded和cound计算第一列相同序列号合并的目的
代码如下:
<template>
<div>
<!--<el-table-->
<!--ref="refTable"-->
<!--:data="tableData1"-->
<!--style="width: 100%"-->
<!--row-key="id"-->
<!--border-->
<!--lazy-->
<!--:load="load"-->
<!--:tree-props="{children: 'children', hasChildren: 'hasChildren'}"-->
<!--@expand-change="expandChange"-->
<!--:span-method="objectSpanMethod">-->
<!--<el-table-column type="" width="50" label="序号" fixed align="center">-->
<!--<template slot-scope="scope">-->
<!--<span>{{scope.row.num}}</span>-->
<!--</template>-->
<!--</el-table-column>-->
<!--<el-table-column-->
<!--prop="date"-->
<!--label="日期"-->
<!--align="center" fixed-->
<!--width="180">-->
<!--</el-table-column>-->
<!--<el-table-column-->
<!--prop="name"-->
<!--label="姓名" fixed-->
<!--width="120">-->
<!--</el-table-column>-->
<!--<el-table-column label="配送信息">-->
<!--<el-table-column-->
<!--prop="name"-->
<!--label="姓名">-->
<!--</el-table-column>-->
<!--<el-table-column label="地址">-->
<!--<el-table-column-->
<!--prop="province"-->
<!--label="省份"-->
<!--width="120">-->
<!--</el-table-column>-->
<!--<el-table-column-->
<!--prop="city"-->
<!--label="市区"-->
<!--width="120">-->
<!--</el-table-column>-->
<!--<el-table-column-->
<!--prop="address"-->
<!--label="地址"-->
<!--width="300">-->
<!--</el-table-column>-->
<!--<el-table-column-->
<!--prop="zip"-->
<!--label="邮编"-->
<!--width="520">-->
<!--</el-table-column>-->
<!--</el-table-column>-->
<!--</el-table-column>-->
<!--</el-table>-->
<el-table
:data="tableData"
style="width: 100%;margin-bottom: 20px;"
row-key="id"
border
:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
@expand-change="expandChange"
:span-method="objectSpanMethod">
<el-table-column type="" width="50" label="序号" fixed align="center">
<template slot-scope="scope">
<span>{{scope.row.num}}</span>
</template>
</el-table-column>
<el-table-column
prop="date"
label="日期"
sortable
width="180">
</el-table-column>
<el-table-column
prop="name"
label="姓名"
sortable
width="180">
</el-table-column>
<el-table-column
prop="address"
label="地址">
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
name: 'ReportTable',
data() {
return {
expandNum: 0, // 折叠 展开序列号
expandTrue: false, // 展开 true 折叠 false
tableData1: [
{
id: 100,
num: '',
date: '净利润',
name: '王小虎0',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
},
{
id: 101,
num: '',
date: '营业收入',
name: '王小虎1',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
}, {
id: 102,
num: '',
date: '一级费用总合计',
name: '王小虎2',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
}, {
id: 1,
num: 1,
date: '2016-05-03',
name: '王小虎3',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
}, {
id: 2,
num: 2,
date: '2016-05-03',
name: '王小虎4',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
}, {
id: 3,
num: 3,
date: '2016-05-01',
name: '王小虎5',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1519 弄',
hasChildren: true
}, {
id: 4,
num: 4,
date: '2016-05-03',
name: '王小虎6',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
}],
tableData: [
{
id: 100,
num: '',
date: '净利润',
name: '王小虎0',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333,
},
{
id: 101,
num: '',
date: '营业收入',
name: '王小虎1',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
},
{
id: 102,
num: '',
date: '一级费用总合计',
name: '王小虎2',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
},
{
id: 1,
num: 1,
expandTrue: false,
date: '2016-05-02',
name: '王小虎',
address: '上海市普陀区金沙江路 1518 弄'
},
{
id: 2,
num: 2,
expandTrue: false,
date: '2016-05-04',
name: '王小虎',
address: '上海市普陀区金沙江路 1517 弄',
children: [
{
id: 21,
num: 2,
date: '2016-05-01',
name: '王小虎',
address: '上海市普陀区金沙江路 1519 弄'
}
]
},
{
id: 3,
num: 3,
expandTrue: false,
date: '2016-05-01',
name: '王小虎',
address: '上海市普陀区金沙江路 1519 弄',
children: [
{
id: 31,
num: 3,
date: '2016-05-01',
name: '王小虎',
address: '上海市普陀区金沙江路 1519 弄'
},
{
id: 32,
num: 3,
date: '2016-05-01',
name: '王小虎',
address: '上海市普陀区金沙江路 1519 弄'
}
]
},
{
id: 4,
num: 4,
expandTrue: false,
date: '2016-05-03',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄'
}
],
newTableData: [],
}
},
mounted(){
this.newTableData = JSON.parse(JSON.stringify(this.tableData));
this.changeData();
},
methods: {
expandChange(row, expanded) {
this.expandNum = row.num;
this.expandTrue = expanded;
let i = this.findExpandChange(row);
this.tableData[i].expandTrue = this.expandTrue; // expandTrue 折叠/展开的状态
},
// 返回当前折叠/展开 在数组下标
findExpandChange(row) {
let id1 = this.tableData.findIndex((arr) => arr.num == row.num); //id1为对象的数组下标
return id1
},
load(tree, treeNode, resolve) {
let arr = [
{
id: 31,
num: 3,
date: '2016-05-01',
name: '王小虎31',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
}, {
id: 32,
num: 3,
date: '2016-05-01',
name: '王小虎32',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
}
]
resolve(arr);
},
// changeData 用于记录子级长度 来计算合并单元格个数
changeData () {
this.newTableData.forEach((item, itemIndex) => {
let count = 0;
if (item.children && item.children.length) {
count += item.children.length;
}
this.tableData[itemIndex].count = count;
})
},
objectSpanMethod({row, column, rowIndex, columnIndex}) {
if (row.num !== '') {
if (columnIndex === 0) {
if(row.count==0 || row.count > 0) {
let rowspan = 0
if (row.expandTrue) {
rowspan = row.count + 1;
} else {
rowspan = 1;
}
return [rowspan, 1]
} else {
return [0, 0];
}
}
} else {
if (columnIndex === 0) {
return [0, 0];
} else if (columnIndex === 1) {
return [1, 2];
} else if (columnIndex > 1) {
return [1, 1];
} else {
return [0, 0];
}
}
}
}
}
</script>
<style scoped>
</style>