Button for closing all open child rows
Button for closing all open child rows

I have successfully managed to create a button that closes all open child rows, but I’m struggling to extend the functionality.
On loading the page I add the class .disable-button
to the button “Close Child Row”. This button is enabled whenever a child row is shown.
} else {
// open this row
row.child( format(row.data()) ).show();
tr.addClass('shown');
// enable "close all expanded rows" btn
$(".close-child-rows").removeClass("disable-button");
The “Close Child Row” button should only be disabled when no child rows are open. The code below only works when only one child row is open. If 2 or more are open it still disables the button when any child row is closed. This is my problem. I thought I was cycling through the rows and if just one class of .shown
is found then the button would still be active.
if ( row.child.isShown() ) {
// this row is already open - close it
row.child.hide();
tr.removeClass('shown');
// cycle through all rows
// if ANY row has the class "shown" enable the button
// if NO row has the class "shown" disable the button
table.rows().every(function () {
var rows = this;
if (rows.child.isShown()) {
$(".close-child-rows").removeClass("disable-button");
} else {
$(".close-child-rows").addClass("disable-button");
}
});
This question has an accepted answers - jump to answer
Answers
Thanks for the test case. I believe the problem is the last row will always be not shown (unless you open the row) resulting in the button being disabled. You can simplify your code by using the
row-selector
of the classshown
and thecount()
to get the count of open rows. Then set the button disabled based on this. For example:http://live.datatables.net/vipaqeje/1/edit
Kevin
Thanks for the reply Kevin. Did you have an example? The link you posted was the same as my version.
Sorry wrong link:
http://live.datatables.net/siyiyozu/1/edit
Kevin
Thanks again Kevin. That works perfectly! Much appreciated.