el-table使用span-method合并行
1、html页面
<el-table height="650" :data="skuListInfo" ref="tableData" border :span-method="objectSpanMethod" :header-cell-style="{background:'#deebff',color:'#606266',fontWeight:'bold',textAlign:'center'}" highlight-current-row>
<el-table-column prop="deptName" align="center" label="序号" min-width="30">
<template slot-scope='scope'>
{{scope.row.tableSort}}
</template>
</el-table-column>
<el-table-column prop="levelName1" align="center" label="重点工作" min-width="80">
<template slot-scope='scope'>
{{scope.row.levelName1}}
</template>
</el-table-column>
<el-table-column prop="levelName2" align="center" label="主要项目" min-width="200">
<template slot-scope='scope'>
{{scope.row.levelName2}}
</template>
</el-table-column>
<el-table-column prop="levelName3" align="center" label="具体内容" min-width="180">
<template slot-scope='scope'>
{{scope.row.levelName3 ? scope.row.levelName3 : '\\'}}
</template>
</el-table-column>
<el-table-column prop="unitVal" align="center" label="单位" min-width="50">
<template slot-scope='scope'>
{{scope.row.unitVal}}
</template>
</el-table-column>
<el-table-column prop="dataTotal" align="center" label="数据" min-width="90">
<template slot-scope='scope'>
<el-input v-model="scope.row.dataTotal" @input="dataTotalChange(scope.row, 'dataTotal', 'targetTotal', scope.$index)" @keyup.native="proving($event)" :disabled="!isEdit"></el-input>
</template>
</el-table-column>
<el-table-column prop="dataTotal" align="center" label="大屏数据" min-width="70" v-if="isAdmin">
<template slot-scope='scope'>
<el-input v-model="scope.row.bigDataTotal" @input="dataTotalChange(scope.row, 'bigDataTotal', 'bigTargetTotal', scope.$index)" @keyup.native="proving($event)" :disabled="!isAdmin"></el-input>
</template>
</el-table-column>
<el-table-column prop="weight" align="center" label="权重" min-width="60" v-if="isAdmin">
<template slot-scope='scope'>
<div >{{scope.row.weight ? (scope.row.weight + '%') : '\\'}}</div>
</template>
</el-table-column>
<el-table-column prop="baseTotal" align="center" label="基数" min-width="60" v-if="isAdmin">
<template slot-scope='scope'>
<div >{{scope.row.baseTotal}}</div>
</template>
</el-table-column>
<el-table-column prop="targetTotal" align="center" label="指标数" min-width="60" v-if="isAdmin">
<template slot-scope='scope'>
<div>{{scope.row.targetTotal ? scope.row.targetTotal + '%' : ''}}</div>
</template>
</el-table-column>
<el-table-column prop="bigTargetTotal" align="center" label="大屏指标数" min-width="60" v-if="isAdmin">
<template slot-scope='scope'>
<div>{{scope.row.bigTargetTotal ? scope.row.bigTargetTotal + '%' : ''}}</div>
</template>
</el-table-column>
<el-table-column prop="caliberContent" align="left" label="填报口径说明" min-width="150">
<template slot-scope='scope'>
<span v-html="scope.row.caliberContent">{{scope.row.caliberContent}}</span>
</template>
</el-table-column>
</el-table>
2、javascript部分
<script>
export default {
data() {
return {
skuListInfo: [
{
baseTotal: null
bigDataTotal: null
bigTargetTotal: null
dataTotal: null
id: "aa790401jk7207041"
levelName1: "组织党员干部自学"
levelName2: "参加学习党员人数"
levelName3: ""
levelName4: ""
targetTotal: null
unitVal: "人"
weight: 40
},
{
baseTotal: null
bigDataTotal: null
bigTargetTotal: null
dataTotal: null
id: "aa790401jk7207042"
levelName1: "组织党员干部自学"
levelName2: "参加学习县处级以上党员干部人数"
levelName3: ""
levelName4: ""
targetTotal: null
unitVal: "人"
weight: 40
},
{
baseTotal: null
bigDataTotal: null
bigTargetTotal: null
dataTotal: null
id: "aa790401jk7207043"
levelName1: "组织党员干部自学"
levelName2: "所属基层党组织数量"
levelName3: ""
levelName4: ""
targetTotal: null
unitVal: "人"
weight: 40
},
{
baseTotal: null
bigDataTotal: null
bigTargetTotal: null
dataTotal: null
id: "aa79040hjk7207123"
levelName1: "组织党员干部自学"
levelName2: "发放教材、笔记本等资料数量"
levelName3: ""
levelName4: ""
targetTotal: null
unitVal: "人"
weight: 40
},
{
baseTotal: null
bigDataTotal: null
bigTargetTotal: null
dataTotal: null
id: "aa790401jk7207044"
levelName1: "开展专题学习"
levelName2: "党委(党组)理论学习中心组学习情况"
levelName3: "本级专题学习次数"
levelName4: ""
targetTotal: null
unitVal: "人"
weight: 40
},
{
baseTotal: null
bigDataTotal: null
bigTargetTotal: null
dataTotal: null
id: "aa790401jk7207045"
levelName1: "开展专题学习"
levelName2: "党委(党组)理论学习中心组学习情况"
levelName3: "下属各级专题学习次数"
levelName4: ""
targetTotal: null
unitVal: "人"
weight: 40
},
{
baseTotal: null
bigDataTotal: null
bigTargetTotal: null
dataTotal: null
id: "aa790401jk7207046"
levelName1: "开展专题学习"
levelName2: "基层党组织开展学习党史主题党日活动情况"
levelName3: "开展主题党日活动的基层党组织数量"
levelName4: ""
targetTotal: null
unitVal: "人"
weight: 40
},
{
baseTotal: null
bigDataTotal: null
bigTargetTotal: null
dataTotal: null
id: "aa790401jk7207047"
levelName1: "开展专题学习"
levelName2: "基层党组织开展学习党史主题党日活动情况"
levelName3: "开展主题党日活动的数量"
levelName4: ""
targetTotal: null
unitVal: "人"
weight: 40
},
{
baseTotal: null
bigDataTotal: null
bigTargetTotal: null
dataTotal: null
id: "aa790401jk7207048"
levelName1: "开展专题学习"
levelName2: "领导干部讲专题党课数量"
levelName3: "市厅级领导干部讲专题党课数量"
levelName4: ""
targetTotal: null
unitVal: "人"
weight: 40
},
{
baseTotal: null
bigDataTotal: null
bigTargetTotal: null
dataTotal: null
id: "aa790401jk7207049"
levelName1: "开展专题学习"
levelName2: "领导干部讲专题党课数量"
levelName3: "县处级领导干部讲专题党课数量"
levelName4: ""
targetTotal: null
unitVal: "人"
weight: 40
},
{
baseTotal: null
bigDataTotal: null
bigTargetTotal: null
dataTotal: null
id: "aa790401jk7207050"
levelName1: "开展专题学习"
levelName2: "领导干部讲专题党课数量"
levelName3: "基层党组织书记讲专题党课数量"
levelName4: ""
targetTotal: null
unitVal: "人"
weight: 40
}
],
typeNameArr: [],
typeNamePos: 0,
storeArr: [],
storePos: 0,
feeArr: [],
feePos: 0,
weightArr: [],
weightPos: 0
}
},
mounted() {
this.merage()
},
methods: {
merageInit() {
this.typeNameArr = []
this.typeNamePos = 0
this.storeArr = []
this.storePos = 0
this.feeArr = []
this.feePos = 0
this.weightArr = []
this.weightPos = 0
},
merage() {
this.merageInit()
this.tableSort = 1
for (let i = 0; i < this.skuListInfo.length; i += 1) {
if (i === 0) {
// 第一行必须存在
this.typeNameArr.push(1)
this.typeNamePos = 0
this.storeArr.push(1)
this.storePos = 0
this.feeArr.push(1)
this.feePos = 0
this.weightArr.push(1)
this.weightPos = 0
this.skuListInfo[i].tableSort = this.tableSort
} else {
// 判断当前元素与上一个元素是否相同,eg:this.typeNamePos 是 this.typeNameArr序号
// 第一二列
// eslint-disable-next-line no-lonely-if
if (this.skuListInfo[i].levelName1 === this.skuListInfo[i - 1].levelName1) {
this.typeNameArr[this.typeNamePos] += 1
this.skuListInfo[i].tableSort = this.tableSort
this.typeNameArr.push(0)
} else {
this.tableSort += 1
this.skuListInfo[i].tableSort = this.tableSort
this.typeNameArr.push(1)
this.typeNamePos = i
}
// 第三列
if (this.skuListInfo[i].levelName2 === this.skuListInfo[i - 1].levelName2 && this.skuListInfo[i].levelName1 ===
this.skuListInfo[i - 1].levelName1) {
this.storeArr[this.storePos] += 1
this.storeArr.push(0)
} else {
this.storeArr.push(1)
this.storePos = i
}
// 第四列
if (this.skuListInfo[i].levelName3 === this.skuListInfo[i - 1].levelName3 && this.skuListInfo[i].levelName2 ===
this.skuListInfo[i - 1].levelName2 && this.skuListInfo[i].levelName1 ===
this.skuListInfo[i - 1].levelName1) {
this.feeArr[this.feePos] += 1
this.feeArr.push(0)
} else {
this.feeArr.push(1)
this.feePos = i
}
// 第七列
if (this.skuListInfo[i].weight && this.skuListInfo[i].weight === this.skuListInfo[i - 1].weight) {
if ((this.skuListInfo[i].levelName1 === this.skuListInfo[i - 1].levelName1) && (this.skuListInfo[i].levelName1 === '组织专题培训' || this.skuListInfo[i].levelName1 === '组织专题宣讲' || this.skuListInfo[i].levelName1 === '开展“我为群众办实事”实践活动' || this.skuListInfo[i].levelName1 === '流动党员学习教育')) {
this.weightArr[this.weightPos] += 1
this.weightArr.push(0)
} else if (this.skuListInfo[i].levelName3) {
if (this.skuListInfo[i].levelName2 === this.skuListInfo[i - 1].levelName2) {
this.weightArr[this.weightPos] += 1
this.weightArr.push(0)
} else {
this.weightArr.push(1)
this.weightPos = i
}
} else {
this.weightArr.push(1)
this.weightPos = i
}
} else {
this.weightArr.push(1)
this.weightPos = i
}
}
}
},
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
const lastColumnIndex = this.isAdmin ? 11 : 6
if (columnIndex === 0 || columnIndex === 1 || columnIndex === lastColumnIndex) {
if (this.skuListInfo.length - rowIndex === 1) {
return [0, 0]
}
// 第一二列的合并方法
const row1 = this.typeNameArr[rowIndex]
const col1 = row1 > 0 ? 1 : 0 // 如果被合并了row = 0; 则他这个列需要取消
return {
rowspan: row1,
colspan: col1
}
} else if (columnIndex === 2) {
if (this.skuListInfo.length - rowIndex === 1) {
return [1, 0]
}
// 第三列的合并方法
const row2 = this.storeArr[rowIndex]
const col2 = row2 > 0 ? 1 : 0 // 如果被合并了row = 0; 则他这个列需要取消
return {
rowspan: row2,
colspan: col2
}
} else if (columnIndex === 3) {
// 第四列的合并方法
const row3 = this.feeArr[rowIndex]
const col3 = row3 > 0 ? 1 : 0 // 如果被合并了row = 0; 则他这个列需要取消
return {
rowspan: row3,
colspan: col3
}
} else if (columnIndex === 7 || columnIndex === 8 || columnIndex === 9 || columnIndex === 10) {
const row4 = this.weightArr[rowIndex]
const col4 = row4 > 0 ? 1 : 0
return {
rowspan: row4,
colspan: col4
}
}
},
}
}
</script>
3、效果图