Expand Child Rows of Responsive Datatables automatically

erikduvet picture erikduvet · Jun 18, 2015 · Viewed 14.2k times · Source

I have a responsive dataTable (Responsive doc.) in the following format:

var dataTableInfo = j$('[id$="dataTableInfo"]').DataTable({
                 responsive: {
                    "autoWidth": true,
                    details: {
                        type: 'column',
                        target: 0
                    }
                },
                columnDefs: [ {
                    className: 'control',
                    orderable: false,
                    targets:   0
                } ]
            });

I fill this with data through a search to an external data source and I then have a second table inside the DataTable with additional data (in a child Row) that comes automatically from my instaniation. I can click on the icon in the first column to expand and show the child row, everything works fine.

What I want to accomplish is to automatically expand the child rows through Javascript of this DataTable, once all the data has been loaded (I know when this occurs in a callback function).

I have tried multiple variation of the following:

function ExpandTable(){
        var tab = j$('[id$="dataTableInfo"]').DataTable();
        alert(tab);

        tab.rows().every( function () {

            this.child.show();

        } );
    }

But my table simply won't expand its child rows. Nothing happens and no error messages in the console.

Can anyone help me in explaining how I for example can simulate a button click according to this:

$('#example tbody').on( 'click', 'tr', function () {
var child = table.row( this ).child;

if ( child.isShown() ) {
    child.hide();
}
else {
    child.show();
}} );

or in any other way automating this expanding of the child rows.

Ciao!

Answer

Gyrocode.com picture Gyrocode.com · Jun 18, 2015

CAUSE

It seems that Responsive plug-in does its own child row management, maybe that's why row().child.show() does not work.

SOLUTION

I am using undocumented responsive._detailsDisplay() method to trigger showing of a child row.

// Enumerate all rows
table.rows().every(function(){
    // If row has details collapsed
    if(!this.child.isShown()){
        // Expand row details
        table.settings()[0].responsive._detailsDisplay(this, false);
    }
});

EXAMPLE

See this example for code and demonstration.

LINKS

See jQuery DataTables: jQuery DataTables: How to expand/collapse all child rows for more examples and information.