vue+element table合并相同行
getSpanArr (data) { console.log(data)//从后台获取的数据 this.spanArr = [] this.spanCodeArr = [] for (var i = 0; i < data.length; i++) { if (i === 0) { //如果是第一条记录(即索引是0的时候),向数组中加入1 this.spanArr.push(1) this.spanCodeArr.push(1); this.pos = 0 this.codePos = 0 } else { if (data[i].areaName === data[i-1].areaName) { //如果areaName相等就累加,并且push 0 this.spanArr[this.pos] += 1 this.spanArr.push(0) } else { //不相等push 1 this.spanArr.push(1) this.pos = i } if (data[i].deptCode === data[i-1].deptCode) { //如果deptCode相等就累加,并且push 0 this.spanCodeArr[this.codePos] += 1 this.spanCodeArr.push(0) } else { //不相等push 1 this.spanCodeArr.push(1) this.codePos = i } } } console.log(this.spanArr) console.log(this.spanCodeArr) }, objectSpanMethod({ row, column, rowIndex, columnIndex }){ if(columnIndex==1){ const _row = this.spanArr[rowIndex] const _col = _row > 0 ? 1 : 0 return { rowspan: _row, colspan: _col } } if(columnIndex==2){ const _row = this.spanCodeArr[rowIndex] const _col = _row > 0 ? 1 : 0 return { rowspan: _row, colspan: _col } } }