2

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');
        }
    });

});

1 Answer 1

4

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();
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly. Datatables should do this simple by adding a class to row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.