jQuery Datatables rowGroup Collapse/Expand

后端 未结 4 1600
无人及你
无人及你 2021-01-03 04:44

I\'m using jQuery Datatables and I would like to incorporate row grouping into the table.

I have attempted to incorporate it myself by adding rows and a click event

4条回答
  •  佛祖请我去吃肉
    2021-01-03 05:24

    I have found my own answer so I want to share it if anyone else has future problems with this.

    Use the below code to implement row Grouping where index is the column index that you want to group by:

    var dataSrc = g_oTable.columns(index).dataSrc();
    
    g_oTable.order([index, "asc"]).draw();
    g_oTable.order.fixed({
        pre: [ index, 'asc' ]
    }).draw();
    
    g_oTable.rowGroup().dataSrc(dataSrc[0]);
    g_oTable.rowGroup().enable().draw();
    
    $('.group').each(function(i, obj) {
        $(obj).nextUntil('tr.group').each(function(i) {
            $(this).toggle();
        });
    });
    g_oTable.draw();
    

    Then add a click event handler to your row Groups:

    $( document ).on("click", "tr.group", function(){
        $(this).nextUntil('tr.group').toggle();
    });
    

提交回复
热议问题