使用DataTables合併行

效果以下:html

首先須要對數據進行分組查詢,使用COUNT(*) OVER(PARTITION BY column名稱) as columnCount 獲取每組的數據條數 columnCount;使用ROW_NUMBER() OVER(partition by column名稱 ORDER BY column名稱 DESC) as columnNum 爲每組的數據生成排序ID columnNum;

使用createdCell函數進行單元格合併,具體參考:www.datatables.club/reference/o…bash

"columnDefs":[{
    "targets": 0,
    "orderable" : false,
    "data":null,
    "createdCell": function(td, cellData, rowData, row, col) {
        if(rowData.columnNum > 1){
        	$(td).remove();
        }
        if(rowData.columnNum == 1){
        	$(td).attr("rowspan", rowData.columnCount);
        }
    }
}]
複製代碼

rowData爲每一行的數據內容,判斷當前行在分組內的columnNum,等於1的添加屬性rowspan,並設置其值爲columnCount;大於1的將td單元格刪除。 PS:在使用createdCell函數時老是報錯以下:函數

通過各類查找,在添加"data":null以後問題解決,具體緣由參考: datatables.net/forums/disc…

Add: data: null to your first column to tell DataTables not to try and source the data from anywhere. Otherwise it will use the column index (0 in this case).this

在第一列中添加:data:null,告訴數據表不要嘗試從任何地方獲取數據。不然,它將使用列索引(在本例中爲0)。spa

相關文章
相關標籤/搜索