el-table 行合並 span-method 方法封裝


getspan({ row, column, rowIndex, columnIndex }, dataSource) {
                var getSpanCount = function (dataItems, index, condition, isStart) {
                    if (index < dataItems.length
                        && (index == 0 || isStart || condition(dataItems[index - 1], dataItems[index])))
                        return 1 + getSpanCount(dataItems, index + 1, condition, false);
                    return 0;
                };
                
                var getRowSpanCount = function (rowIndex, dataItems, condition) {
                    if (rowIndex == 0 || !condition(dataItems[rowIndex - 1], dataItems[rowIndex])) {
                        return getSpanCount(dataItems, rowIndex, condition, true);
                    }
                    return 0;
                };

                if (columnIndex == 0) {  //合並第一列
                    return { rowspan: getRowSpanCount(rowIndex, dataSource, (x, y) => x.checkTypeId == y.checkTypeId), colspan: 1 }; 
                }

                return { rowspan: 1, colspan: 1 }
            }

this.result 為 el-table 綁定的數據集, 表格多行合並的條件是 x.checkTypeId == y.checkTypeId

只替換上圖中三處紅色地方的代碼,即可實現行合並。

html:

<el-table :data="dataSource" 
          :span-method="(param)=>getspan(param,dataSource)"

因我這里的el-table個數是動態的,數據源也是動態的,所以必須加一個參數,把 table 綁定的 data 傳進來,方法來自網友,這下這個方法就完美了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM