1、定义合并单元格的方法
const strategySpanMethod = ( { row, column, rowIndex, columnIndex } : any) : any => { if ( columnIndex == 0 || columnIndex == 1 || columnIndex == 2 || columnIndex == 3 || columnIndex == 6 ) { let spanArr = getSpanArr ( tableData. value, column. property) const _row = spanArr[ rowIndex] const _col = _row > 0 ? 1 : 0 return { rowspan : _row, colspan : _col} }
} const getSpanArr = ( data : any, spanKey : any) => { console. log ( spanKey) let spanArr = [ ] let pos : any = '' for ( let i = 0 ; i < data. length; i++ ) { if ( i === 0 ) { spanArr. push ( 1 ) pos = 0 } else { if ( data[ i] [ spanKey] === data[ i - 1 ] [ spanKey] ) { spanArr[ pos] += 1 spanArr. push ( 0 ) } else { spanArr. push ( 1 ) pos = i} } } return spanArr
}
2、模板中使用
< el- table : data= "tableData.value" : span- method= "strategySpanMethod" : max- height= "height" : header- cell- style= "{ textAlign : 'center' , background : '#f5f5f5' , color : '#000000' , } " border :cell-style=" { textAlign : 'center' } ">
< / el- table>