vue單元格多列合併的實現
阿新 • • 發佈:2020-11-29
一.多列合併
1.在el-table中新增:span-method="objectSpanMethod"
屬性來控制合併單元格,如下圖
2.合併程式碼,每一列都要設定一個不同的key,這樣可以防止合併的時候上下內容一樣導致錯誤的問題
objectSpanMethod({ row,column,rowIndex,columnIndex }) { if (columnIndex === 0) { if (this.myObj[row.channel_type].start === rowIndex) { return { rowspan: this.myObj[row.channel_type].step,colspan: 1 }; } else { return { rowspan: 0,colspan: 0 }; } } if (columnIndex === 1) { if ( this.myObj_two[row.channel_name_chinese + row.channel_type].start === rowIndex ) { return { rowspan: this.myObj_two[row.channel_name_chinese + row.channel_type] .step,colspan: 0 }; } } },// 合併單元格第一列 resolveData(arr) { var obj = {}; arr.forEach((val,key) => { if (!obj[val.channel_type]) { obj[val.channel_type] = { start: key,step: 1 }; } else { obj[val.channel_type].step++; } }); this.myObj = obj; console.log(obj); },// 合併單元格第二列 resolveData_two(arr) { var obj = {}; arr.forEach((val,key) => { if (!obj[val.channel_name_chinese + val.channel_type]) { obj[val.channel_name_chinese + val.channel_type] = { start: key,step: 1 }; } else { obj[val.channel_name_chinese + val.channel_type].step++; } }); this.myObj_two = obj; console.log(this.myObj_two,"this.myObj"); },
3.需要呼叫一下下面兩個函式,data為你所獲取的所有資料
this.resolveData_two(data); this.resolveData(data);
4.合併結果如下圖
到此這篇關於vue單元格多列合併的實現的文章就介紹到這了,更多相關vue單元格多列合併內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!