简体   繁体   中英

JQuery Datatables: How to add child row?

Does Datatables define the child row with a css marker? I have an HTML table that includes child rows. I would like to set these as child rows in Datatables so they stay with the parent row when sorted. I could not find a reference to a css class that marks a row as a child row.

All I could find was the JQuery row.child() function where you add child rows to the row but I am not good with JQuery and can't figure out how to add the row here.

Please see this JSFiddle . (Click "Program" to sort and the list icon to expand/collapse the child rows).

<table id="tprogram" class="table table-striped table-hover ">
<thead>
    <tr>
        <th class='icon_colunm no-sort'></th>
        <th>Program</th>

    </tr>
</thead>
<tbody>
    <tr>
        <td class="text-right"> <i class="btn btn-xs fa fa-list-ul" data-toggle="collapse" data-target=".collapsed1"></i>
        </td>
        <td class='name'>A</td>
    </tr>
    <tr class="collapse collapsed1">
        <td class="text-right"> <i class="fa fa-minus"></i></td>
        <td class='name'>a</td></tr>
    <tr>
        <td class="text-right"> <i class="btn btn-xs fa fa-list-ul" data-toggle="collapse" data-target=".collapsed2"></i>
        </td>
        <td class='name'>B</td></tr>
    <tr class="collapse collapsed2">
        <td class="text-right"> <i class="fa fa-minus"></i>
        </td>
        <td class='name'>a</td></tr>
    <tr class="collapse collapsed2">
        <td class="text-right"> <i class="fa fa-minus"></i>
        </td>
        <td class='name'>b</td></tr>
    <tr class="collapse collapsed2">
        <td class="text-right"> <i class="fa fa-minus"></i>
        </td>
        <td class='name'>c</td>
    </tr>
</tbody>

 $(document).ready(function() {
    $('#tprogram').dataTable({
        "bPaginate":    true,
        "bSort":        true,
        "bInfo":        false,
        "bFilter":      true,
        "bAutoWidth":   false,
        "LengthChange": false,
        "iDisplayLength": 50,
    });

    $('#tprogram').on('click', '.fa-list-ul', function () {
        var tr = $(this).closest('tr');

        var row = table.row(tr);

        if (row.child.isShown()) {
            // This row is already open - close it
            row.child.hide();
            tr.removeClass('shown');
        } else {
            // Open this row
            row.child(*?*).show();
            tr.addClass('shown');
        }
    });

});

I decided to insert my own class to the parent row and use the row.add() function to add the child rows manually with some JQuery.

    $('.parentrow').closest('tr').each(function(){
        var row = table.row(this);
        childrows = $(this).closest('tr').nextUntil('.parentrow');
        row.child(childrows).show();
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM