data() { return { table: [{ id: '1', checkRoom: 'CTROOM', checkProject: '颈椎MRICT', checkMoney: '300.22', attention: '检查前空腹', appointmentTime: '' },{ id: '1', checkRoom: 'CTROOM', checkProject: '颈椎MRICT1', checkMoney: '303.22', attention: '检查前空腹', appointmentTime: '' },{ id: '1', checkRoom: 'CTROOM', checkProject: '颈椎MRICT22', checkMoney: '340.22', attention: '检查前空腹', appointmentTime: '2019-5-29 10:30' },{ id: '1', checkRoom: 'DR', checkProject: '鼻骨', checkMoney: '500.22', attention: '检查前空腹', appointmentTime: '2019-5-29 9:30' }] }; }, created(){ // 给table赋值,重新遍历新增rowSpan属性,checkRoom,appointmentTime为table里面需要合并的属性名称 this.table = mergeTableRow(this.table, ['checkRoom', 'appointmentTime']) }, methods: { objectSpanMethod({ row, column, rowIndex, columnIndex }) { const span = column['property'] + '-span' if(row[span]){ return row[span] } } }
/** * 方法一 * table合并行通用 */ export function mergeTableRow(data, merge) { if (!merge || merge.length === 0) { return data } merge.forEach((m) => { const mList = {} data = data.map((v, index) => { const rowVal = v[m] if (mList[rowVal]) { mList[rowVal]++ data[index - (mList[rowVal] - 1)][m + '-span'].rowspan++ v[m + '-span'] = { rowspan: 0, colspan: 0 } } else { mList[rowVal] = 1 v[m + '-span'] = { rowspan: 1, colspan: 1 } } return v }) }) return data }
/** * 方法二 * 改良后(不相邻的数据相互不受影响) * table合并行通用 */ export function mergeTableRow(data, merge) { if (!merge || merge.length === 0) { return data } merge.forEach((m) => { const mList = {} data = data.map((v, index) => { const rowVal = v[m] if (mList[rowVal] && mList[rowVal].newIndex === index) { mList[rowVal]['num']++ mList[rowVal]['newIndex']++ data[mList[rowVal]['index']][m + '-span'].rowspan++ v[m + '-span'] = { rowspan: 0, colspan: 0 } } else { mList[rowVal] = { num: 1, index: index, newIndex: index + 1 } v[m + '-span'] = { rowspan: 1, colspan: 1 } } return v }) }) return data }
转载出处:原文地址