相信你肯定看了 ElementUI 官方文檔了,沒看的話先去看下表格各個屬性的意義,方便下文閱讀:傳送門
但你會發現此例過於簡單,死數據,但我們開發的時候往往都是后台傳遞過來的數據,導致我們 rowspan 的參數需要自己做判斷,根據數據的相同行(或列)進行合並;
我們先看下結果:
代碼附上:
1 <template> 2 <div class=""> 3 <el-table 4 :data="listData" 5 :span-method="objectSpanMethod" 6 class="tableArea" 7 style="width: 100%"> 8 <el-table-column 9 prop="type" 10 label="序號" 11 align="center" 12 width="200"/> 13 <el-table-column 14 prop="sheetType" 15 label="工單類型" 16 /> 17 <el-table-column 18 prop="taskKey" 19 label="taskKey" 20 /> 21 <el-table-column 22 prop="templateUrl" 23 label="templateUrl" 24 /> 25 <el-table-column 26 label="操作" 27 > 28 <template slot-scope="scope"> 29 <el-tooltip class="item" effect="dark" content="修改" placement="top-start"> 30 <i class="el-icon-edit-outline" @click="modify(scope)" /> 31 </el-tooltip> 32 <el-tooltip class="item" effect="dark" content="刪除" placement="top-start"> 33 <i class="el-icon-delete" @click="deleteData(scope)" /> 34 </el-tooltip> 35 </template> 36 </el-table-column > 37 </el-table> 38 </div> 39 </template> 40 <script> 41 42 export default { 43 name: 'myNeedDeal', 44 data() { 45 return { 46 rowList: [], 47 spanArr: [], 48 position: 0, 49 listData: [] 50 } 51 }, 52 53 methods: { 54 queryData(){//查詢操作 55 this.listData = [ 56 { 57 id:1, 58 type:1, 59 sheetType: "事件單", 60 taskKey: "shijian_01", 61 templateUrl: "/shijian_01" 62 }, 63 { 64 id:2, 65 type:1, 66 sheetType: "事件單", 67 taskKey: "shijian_02", 68 templateUrl: "/shijian_02" 69 }, 70 { 71 id:3, 72 type:1, 73 sheetType: "事件單", 74 taskKey: "shijian_03", 75 templateUrl: "/shijian_04" 76 }, 77 { 78 id:4, 79 type:2, 80 sheetType: "問題單", 81 taskKey: "wenti_01", 82 templateUrl: "/wenti_01" 83 }, 84 { 85 id:5, 86 type:2, 87 sheetType: "問題單", 88 taskKey: "wenti_02", 89 templateUrl: "/wenti_02" 90 }, 91 { 92 id:6, 93 type:2, 94 sheetType: "問題單", 95 taskKey: "wenti_03", 96 templateUrl: "/wenti_03" 97 } 98 ]; 99 this.rowspan() 100 }, 101 rowspan() { 102 this.listData.forEach((item,index) => { 103 if( index === 0){ 104 this.spanArr.push(1); 105 this.position = 0; 106 }else{ 107 if(this.listData[index].type === this.listData[index-1].type ){ 108 this.spanArr[this.position] += 1; 109 this.spanArr.push(0); 110 }else{ 111 this.spanArr.push(1); 112 this.position = index; 113 } 114 } 115 }) 116 }, 117 objectSpanMethod({ row, column, rowIndex, columnIndex }) { //表格合並行 118 if (columnIndex === 0) { 119 const _row = this.spanArr[rowIndex]; 120 const _col = _row>0 ? 1 : 0; 121 return { 122 rowspan: _row, 123 colspan: _col 124 } 125 } 126 if(columnIndex === 1){ 127 const _row = this.spanArr[rowIndex]; 128 const _col = _row>0 ? 1 : 0; 129 return { 130 rowspan: _row, 131 colspan: _col 132 } 133 } 134 } 135 }, 136 mounted() { 137 this.queryData(); 138 } 139 } 140 </script> 141 <style lang="scss" scoped> 142 .el-select { 143 margin-right: 15px; 144 } 145 .el-input { 146 margin-right: 15px; 147 width: 200px; 148 } 149 .tableArea { 150 margin-top: 20px; 151 box-shadow: 0 0 8px 0 #aaa; 152 } 153 i[class^="el-icon"] { 154 margin-right: 5px; 155 font-size: 16px; 156 cursor: pointer; 157 } 158 .modify_table{ 159 td{ 160 padding: 10px ; 161 } 162 } 163 .item_title{ 164 text-align: right; 165 } 166 </style> 167
詳細說明:
:span-method="objectSpanMethod"
這個是官方給定的綁定屬性和對應的方法,objectSpanMethod 傳入了 { row, column, rowIndex, columnIndex }
row: 當前行
column: 當前列
rowIndex:當前行號
columnIndex :當前列號
該函數可以返回一個包含兩個元素的數組,第一個元素代表rowspan
,第二個元素代表colspan
。 也可以返回一個鍵名為rowspan
和colspan
的對象。
this.spanArr 數組 ,返回的是相對應的行合並行數
這個示例打印出的this.spanArr為 [3, 0, 0, 3, 0, 0],比如,第一個元素為3,表示第一行應該向下合並3行(即第一行的rowspan為3),第二個元素的rowspan為0,就讓它“消失”。
rowspan()這個函數就是用來返回 this.spanArr 數組的,定義每一行的 rowspan
rowspan()函數,if( index === 0),第一行,直接先給數組push進一個1,表示自己先占一行,this.position是數組元素的位置(此時是從數組元素的第一個開始,所以this.position為0), this.position為0意思表示的就是數組的第一個元素。
當到了index為2的時候,if(this.listData[index].type === this.listData[index-1].type ),讓第二行與第一行作比較,
如果第二行與第一行相等的話,this.position就+1,當有n行第一行相同,this.position就為n,表示向下合並n行;第二行自己就this.spanArr.push(0),表示第二行“消失”,因為第一行和第二行合並了啊。
如果第二行與第一行不相等的話,那么this.spanArr.push(1);就讓第二行自己獨占一行;this.position = index意思就是把指針拿到index這行來,表示設置數組this.spanArr[this.position]的元素值,然后定義從此行開始向下合並幾行(可能這句話我表述的不是很清楚你可以根據我這個示例研究下,當index為3時,this.position為3,當index為4時,第四行與第三行需要合並,那么在數組的this.position元素就要+1了,也就是this.spanArr[this.position] += 1)
還有最后一句話
const _col = _row>0 ? 1 : 0;
定義的這一個單元格列的合並,我們項目只合並行,不合並列;
_row:代表合並行的行數,_row的值要么是1,或者更大的自然正整數,要么是0。
1代表:獨占一行
更大的自然數:代表合並了若干行
0:代表“消失”的哪那一個單元格,后面的單元格向前推一格