之前做个一个单元格合并的,孙果宝宝的项目、因为只2个就实现了。正确的方式是用单元格。
原理如下。
首先用一个返回生成一个数组。
这个数组是一个一维数组
比如[3,0,0,2,0]
如果元素值大于1,就合并。合并行是数值。合并列是1.如果是0,就删除,就是0,0。 如何{1,1}就是占用1行,1列。
if (columnIndex === 0) {
if (rowIndex === 0) {
// => 合并第一行到第四行,从第一行开始,共4行
return {
rowspan: 4,
colspan: 1
}
} else if (rowIndex === 4) {
// => 合并第五行到第九行,从第五行开始,共5行
return {
rowspan: 5,
colspan: 1
}
} else if (rowIndex === 9) {
// => 合并第10行到第14行,从第10行开始,共5行
return {
rowspan: 5,
colspan: 1
}
} else {
// => 其余被合并的行,诸如1、2、3、5、6、7、8、10、11、12、13全都设为0
return {
rowspan: 0,
colspan: 0
}
}
}
<template>
<div>
<el-table :data="tableData" :span-method="objectSpanMethod" border style="width: 100%; margin-top: 20px">
<el-table-column prop="sysName" label="系统名称">
</el-table-column>
<el-table-column prop="tenantName" label="租户列表">
</el-table-column>
<el-table-column prop="projectName" label="项目">
</el-table-column>
<el-table-column prop="parkNodes" label="小区">
<template slot-scope="scope">
<div v-for="(item,x) in scope.row.parkNodes" :key="x">{{item.name}}</div>
</template>
</el-table-column>
<el-table-column prop="" label="权限设置">
<template>
<span>权限设置</span>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
data() {
return {
mergeSpanArr: [],
mergeSpanArrIndex: 0,
zuhuSpanArr: [], //租户合并列表
zuhuSpanArrIndex: 0, //租户合并索引
tableData: [
{
sysId: '1',
sysName: '物业系统',
tenantName: '高兴物业',
projectName: '项目A',
checked: true,
parkNodes: [
{ name: '和鑫园1期', checked: true },
{ name: '和鑫园2期', checked: true },
{ name: '和鑫园3期', checked: true },
],
},
{
sysId: '1',
sysName: '物业系统',
tenantName: '高兴物业',
projectName: '项目B',
checked: true,
parkNodes: [
{ name: '和鑫园1期', checked: true },
{ name: '和鑫园1期', checked: true },
{ name: '和鑫园1期', checked: true },
{ name: '和鑫园1期', checked: true },
{ name: '和鑫园1期', checked: true },
],
},
{
sysId: '1',
sysName: '物业系统',
tenantName: '汇城物业',
projectName: '项目C',
checked: true,
parkNodes: [{ name: '和鑫园1期' }],
},
{
sysId: '2',
sysName: '物业系统',
tenantName: '汇城物业',
projectName: '项目D',
checked: true,
parkNodes: [
{ name: '和鑫园1期', checked: true },
{ name: '和鑫园1期', checked: true },
{ name: '和鑫园1期', checked: true },
],
},
{
sysId: '2',
sysName: '招商系统',
tenantName: '汇成企业广场',
projectName: '项目E',
checked: true,
parkNodes: [
{ name: '和鑫园1期', checked: true },
{ name: '和鑫园1期', checked: true },
{ name: '和鑫园1期', checked: true },
{ name: '和鑫园1期', checked: true },
],
},
],
}
},
created() {
this.setMergeArr(this.tableData)
this.setMergeArrZuHu(this.tableData)
},
methods: {
setMergeArr(data) {
for (var i = 0; i < data.length; i++) {
if (i === 0) {
this.mergeSpanArr.push(1)
this.mergeSpanArrIndex = 0
} else {
// 判断当前元素与上一个元素是否相同
if (data[i].sysName === data[i - 1].sysName) {
//itemInfo代表根据哪个相同的属性去合并
this.mergeSpanArr[this.mergeSpanArrIndex] += 1
this.mergeSpanArr.push(0)
} else {
this.mergeSpanArr.push(1)
this.mergeSpanArrIndex = i
}
}
}
},
setMergeArrZuHu(data) {
for (var i = 0; i < data.length; i++) {
if (i === 0) {
this.zuhuSpanArr.push(1)
this.zuhuSpanArrIndex = 0
} else {
// 判断当前元素与上一个元素是否相同
if (data[i].tenantName === data[i - 1].tenantName) {
//itemInfo代表根据哪个相同的属性去合并
this.zuhuSpanArr[this.zuhuSpanArrIndex] += 1
this.zuhuSpanArr.push(0)
} else {
this.zuhuSpanArr.push(1)
this.zuhuSpanArrIndex = i
}
}
}
},
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
if (columnIndex === 0) {
const _row = this.mergeSpanArr[rowIndex]
const _col = _row > 0 ? 1 : 0
return {
rowspan: _row,
colspan: _col,
}
}
if (columnIndex === 1) {
const _row = this.zuhuSpanArr[rowIndex]
const _col = _row > 0 ? 1 : 0
return {
rowspan: _row,
colspan: _col,
}
}
},
objectSpanMethod2({ row, column, rowIndex, columnIndex }) {
if (columnIndex === 0) {
if (rowIndex === 0) {
return {
rowspan: 3,
colspan: 1,
}
} else if (rowIndex == 3) {
return {
rowspan: 2,
colspan: 1,
}
} else {
return {
rowspan: 0,
colspan: 0,
}
}
}
},
},
}
</script>