Button for closing all open child rows

Button for closing all open child rows

silkspinsilkspin Posts: 141Questions: 32Answers: 5

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

Example: http://live.datatables.net/vipaqeje/1/edit

This question has an accepted answers - jump to answer

Answers

This discussion has been closed.