在一些项目中会用到表格,这些表格有的是组件中的表格,今天在这里我就以element-table举例: 在项目中如果遇到需要合并单元格的操作,只需用到一下代码,需要修改的是红色字体部分,改成自己页面中对应接收数据的数组名称即可 countJson: function (json) { const obj = json const len = obj.length const result = [] const mergerowsarr = [] for (let i = 0; i < len; i++) { const id = obj[i].showId if (result[id]) { result[id] = result[id] + 1 } else { result[id] = 1 } } if (result.length > 0) { result.reverse().forEach(function (item) { mergerowsarr.push({'len': item, 'startIndex': 0}) }) return mergerowsarr } return '统计无结果' }, objectSpanMethod ({row, column, rowIndex, columnIndex}) { const _self = this const mergerowsarr = _self.countJson(_self.getAccountList) if (columnIndex === 0 || columnIndex === 9) { let sum = 0 mergerowsarr.forEach(item => { item.startIndex = sum sum += item.len }) let getSign = false for (let i = 0; i < mergerowsarr.length; i++) { if (rowIndex === mergerowsarr[i].startIndex) { getSign = true return { rowspan: mergerowsarr[i].len, colspan: 1 } } } if (!getSign) { return { rowspan: 0, colspan: 0 } } } }