Expanding and collapsing table row (tr) using jquery

12,844

Solution 1

Here's the code to toggle the table rows which are "inactive". It's better to rely on "closest()" than "parent()" or "parents()", because it's faster and it makes more sense to use it in this case.

$(document).ready(function() {

  // Save all the inactive rows
  var inactive_rows = '';
  $('.InActive').closest("tr").each(function() {
    inactive_rows += '<tr class="alertRow">';
    inactive_rows += $(this).html();
    inactive_rows += '</tr>';
  });
  console.log(inactive_rows);
  // Save all the active rows
  var active_rows = "";
  $('.Active').closest("tr").each(function() {
    active_rows += '<tr class="alertRow">';
    active_rows += $(this).html();
    active_rows += '</tr>';
  });
  // Empty the table
  $('.alertsList').html("");
  // Load the new table
  var table_html = '<table width="100%"><thead><th></th><th>Id</th><th>From</th><th>Action</th><th>State</th><th>time</th></thead><tbody>';
  table_html += active_rows;
  table_html += inactive_rows;
  table_html += '</tbody></table><a href="" class="toggleInactiveRows">Toggle Inactive Rows</a>';
  $('.alertsList').append(table_html);

  // Hide the inactive rows when the page loads
  $('.InActive').closest("tr").hide();

  // Toggle the inactive rows
  $('.toggleInactiveRows').click(function() {
    $('.InActive').closest("tr").slideToggle();
    return false;
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="alertsList">
  <table width="100%">
    <thead>
      <th></th>
      <th>Id</th>
      <th>From</th>
      <th>Action</th>
      <th>State</th>
      <th>time</th>
    </thead>
    <tbody>
      <tr class="alertRow">
        <td></td>
        <td>1025973</td>
        <td>SYSTEM</td>
        <td>false</td>
        <td class="Active">Active</td>
        <td>2014-09-23T00:59:26.92</td>
      </tr>
      <tr class="alertRow">
        <td></td>
        <td>1025974</td>
        <td>SYSTEM</td>
        <td>false</td>
        <td class="Active">Active</td>
        <td>2014-09-23T00:59:26.92</td>
      </tr>
      <tr class="alertRow">
        <td></td>
        <td>1025974</td>
        <td>SYSTEM</td>
        <td>false</td>
        <td class="InActive">InActive</td>
        <td>2014-09-23T00:59:26.92</td>
      </tr>
      <tr class="alertRow">
        <td></td>
        <td>1025974</td>
        <td>SYSTEM</td>
        <td>false</td>
        <td class="InActive">InActive</td>
        <td>2014-09-23T00:59:26.92</td>
      </tr>
      <tr class="alertRow">
        <td></td>
        <td>1025974</td>
        <td>SYSTEM</td>
        <td>false</td>
        <td class="Active">Active</td>
        <td>2014-09-23T00:59:26.92</td>
      </tr>
    </tbody>
  </table>
  <a href="" class="toggleInactiveRows">Toggle Inactive Rows</a>
</div>

Solution 2

First of all, Jquery doesn't properly handle height animations on table elements, So we'll inject <div>'s inside the <td>'s to be animated, as mentioned in this answer.

Secondly, Since you want to toggle the inactive items as a group, We should group them together.

  • We'll find the inactive rows using has() method, wrap them in a <tfoot> using wrapAll() and insert it before the <tbody>, for which we use insertBefore().

    I'm using insertBefore() since it is the better practice due to the advantage mentioned in this answer. We can use insertAfter() as well, since it is semantically okay to include the <tfoot> after the <tbody>,

  • Then we find the <td>'s inside them and wrap their content in a hidden <div> using wrapInner() method.

    You can avoid this step by populating the table accordingly in serverside

  • We then add the option for toggling before the first inactive row.

  • on click of the toggle option, we use closest() to access it's parent <tr> and then nextAll() to target all the following <tr> - which are inactive. We then find the <div's which we injected inside them and call slideToggle() for the toggle functionality.

    We can use parent() as well for accessing the parent <tr> according to current markup. I'm using closest so that it'll work event if you bind the event handler to an element inside <td> - say for example, a button.

    We can use siblings() as well for accessing the following <tr>'s, since we separated the active and inactive rows into different containers.

    Note that we need to delegate the event handler using on() since we're injecting the toggle option dynamically.

  • Finally we use the CSS pseudo element ::before to display the corresponding icon based on a class, which is toggled when the user clicks the toggle option using toggleClass()

Other than these,

  • I've wrapped the title columns inside <thead> which is more semantically appropriate.
  • I've set a minimum width for the <td> to avoid the "jerk" due to the change in width of State column when the visibility of all Inactive items are toggled
  • I've removed the empty <td> and added some CSS for the sake of demo

We end up with the following semantically cool structure which is collapsible:

 <table>
   <thead>
     <tr>
          <th> title</th>
     </tr>
   </thead>
   <tfoot>
    <tr>
      <td>Toggle Switch</td>
    </tr>
    <tr>
      <td><div> Collapsible content</div></td>
     </tr>
   </tfoot>
   <tbody>
     <tr>
          <td>body content</td>
     </tr>
   </tbody>
</table>

$('.alertRow').has(".InActive")
// group them together in a tfoot and insert it before tbody
         .insertBefore(".alertsList table tbody").wrapAll("<tfoot></tfoot>")
// inject the hidden divs inside the columns for animating
         .find("td").wrapInner("<div style='display:none'/>").end()
// insert the toggle option before the first inactive row
         .first().before("<tr><td class='toggle' colspan='5'> Inactive Items</td></tr>");

$(".alertsList table").on("click", ".toggle", function () {
   // following class keeps track of the cuurrent state
   $(this).toggleClass("expanded")
   // find all the injected divs and toggle them
      .closest("tr").nextAll("tr").find('td  div').slideToggle(700);
});
*{
  margin:0;
  padding:0;
}
th,td{
  min-width:80px;
  text-align:center;
}
.toggle{
  background:dodgerblue;
}
.toggle::before{
    content:"+"
}
.toggle.expanded::before{
    content:"-"
}
tfoot tr{
  background:#eee;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="alertsList">
    <table width="100%">
        <thead>
          <tr>
                <th>Id</th>
                <th>From</th>
                <th>Action</th>
                <th>State</th>
                <th>time</th>
            </tr>
        </thead>
        <tbody>
            <tr class="alertRow">
                <td>1025973</td>
                <td>SYSTEM</td>
                <td>false</td>
                <td class="Active">Active</td>
                <td>2014-09-23T00:59:26.92</td>
            </tr>
            <tr class="alertRow">
                <td>1025974</td>
                <td>SYSTEM</td>
                <td>false</td>
                <td class="Active">Active</td>
                <td>2014-09-23T00:59:26.92</td>
            </tr>
            <tr class="alertRow">
                <td>1025974</td>
                <td>SYSTEM</td>
                <td>false</td>
                <td class="InActive">InActive</td>
                <td>2014-09-23T00:59:26.92</td>
            </tr>
            <tr class="alertRow">
                <td>1025974</td>
                <td>SYSTEM</td>
                <td>false</td>
                <td class="InActive">InActive</td>
                <td>2014-09-23T00:59:26.92</td>
            </tr>
            <tr class="alertRow">
                <td>1025974</td>
                <td>SYSTEM</td>
                <td>false</td>
                <td class="Active">Active</td>
                <td>2014-09-23T00:59:26.92</td>
            </tr>
        </tbody>
    </table>
</div>

Solution 3

You had a couple problems with your selectors:

$('.alertRow .InActive').parent().click(function () {
    $(this).slideToggle(1000);
});

This should work the way you intended.

Unfortunately, as far the animation of the slideToggle is concerned, tables tend to have a restricted style to them. In this case the slideToggle will not animate due to the line-height of the tr elements. You could set the tr line-height and height properties manually in the CSS, but it might cause you some other formatting pain in the future. Here's an example fiddle.

Solution 4

As mentioned in a previous post, you will need to set the line-height and height style properties of the tr tag for the animation effects to work.

Here is an example that toggles all 'InActive' rows using a button.

CSS:

tr {
    height: 15px;
    line-height: 0px;
}

HTML:

<div class="alertsList">
<table width="100%">
    <tbody>
        <th></th>
        <th>Id</th>
        <th>From</th>
        <th>Action</th>
        <th>State</th>
        <th>time</th>
        <tr class="alertRow">
            <td></td>
            <td>1025973</td>
            <td>SYSTEM</td>
            <td>false</td>
            <td class="Active">Active</td>
            <td>2014-09-23T00:59:26.92</td>
        </tr>
        <tr class="alertRow">
            <td></td>
            <td>1025974</td>
            <td>SYSTEM</td>
            <td>false</td>
            <td class="Active">Active</td>
            <td>2014-09-23T00:59:26.92</td>
        </tr>
        <tr class="alertRow">
            <td></td>
            <td>1025974</td>
            <td>SYSTEM</td>
            <td>false</td>
            <td class="InActive">InActive</td>
            <td>2014-09-23T00:59:26.92</td>
        </tr>
        <tr class="alertRow">
            <td></td>
            <td>1025974</td>
            <td>SYSTEM</td>
            <td>false</td>
            <td class="InActive">InActive</td>
            <td>2014-09-23T00:59:26.92</td>
        </tr>
        <tr class="alertRow">
            <td></td>
            <td>1025974</td>
            <td>SYSTEM</td>
            <td>false</td>
            <td class="Active">Active</td>
            <td>2014-09-23T00:59:26.92</td>
        </tr>
    </tbody>
</table>
<input id="btnShowHide" type="button" value="Show/hide" />

JS:

 $('#btnShowHide').click(function () {
    $('.InActive').parent('tr').slideToggle(1000);
});

Solution 5

As T J said in his answer, it is complicated to animate the height of a table row (see this answer or this answer for hacks around it, which involve wrapping each cell's content in a <div>).

You could skip the animation altogether, and simply hide or show the inactive rows :

$('#btnToggle').click(function() {
    $('td.InActive').closest('tr').toggle();
});

If you want some visual candy to signal the hiding to the user, I would suggest using .fadeToggle() which will work straight away :

$('#btnToggle').click(function() {
    $('td.InActive').closest('tr').fadeToggle(1000);
});

fiddle


As an extra note : if you can modify the code that generates the table (e.g. : the PHP or ruby (or wathever) code), try putting the active / inactive class on the row (the <tr> node) rather than the inner cell (the <td> node) :

<div class="alertsList">
    <table width="100%">
        <tbody>
            <tr>
                <th></th>
                <th>Id</th>
                <th>From</th>
                <th>Action</th>
                <th>State</th>
                <th>time</th>
            </tr>
            <tr class="alertRow active">
                ...
            </tr>
            <tr class="alertRow active">
                ...
            </tr>
            <tr class="alertRow inactive">
                ...
            </tr>
            <tr class="alertRow inactive">
                ...
            </tr>
            <tr class="alertRow active">
                ...
            </tr>
        </tbody>
    </table>
</div>

This would make for easier rules in your css and js :

//css
tr.inactive {font-style: italic; background-color: #eee}

//js
$('tr.inactive').fadeToggle(1000);
Share:
12,844
Kurkula
Author by

Kurkula

Updated on June 30, 2022

Comments

  • Kurkula
    Kurkula almost 2 years

    enter image description here

    I have a table with active and inactive items. This table is dynamically populated from database. I am trying to add a toggle for only inactive items in my table and display all active items. I mean I want to show all active items and slide toggle only inactive items in my table.

    <div class="alertsList">
        <table width="100%">
            <tbody>
                <tr>
                    <th></th>
                    <th>Id</th>
                    <th>From</th>
                    <th>Action</th>
                    <th>State</th>
                    <th>time</th>
                    <tr class="alertRow">
                        <td></td>
                        <td>1025973</td>
                        <td>SYSTEM</td>
                        <td>false</td>
                        <td class="Active">Active</td>
                        <td>2014-09-23T00:59:26.92</td>
                    </tr>
                    <tr class="alertRow">
                        <td></td>
                        <td>1025974</td>
                        <td>SYSTEM</td>
                        <td>false</td>
                        <td class="Active">Active</td>
                        <td>2014-09-23T00:59:26.92</td>
                    </tr>
                    <tr class="alertRow">
                        <td></td>
                        <td>1025974</td>
                        <td>SYSTEM</td>
                        <td>false</td>
                        <td class="InActive">InActive</td>
                        <td>2014-09-23T00:59:26.92</td>
                    </tr>
                    <tr class="alertRow">
                        <td></td>
                        <td>1025974</td>
                        <td>SYSTEM</td>
                        <td>false</td>
                        <td class="InActive">InActive</td>
                        <td>2014-09-23T00:59:26.92</td>
                    </tr>
                    <tr class="alertRow">
                        <td></td>
                        <td>1025974</td>
                        <td>SYSTEM</td>
                        <td>false</td>
                        <td class="Active">Active</td>
                        <td>2014-09-23T00:59:26.92</td>
                    </tr>
            </tbody>
        </table>
    </div>
    
    $('.alertRow.InActive').Parent.click(function () {
    
        $(this).nextUntil('tr.td.InActive').slideToggle(1000);
    });
    

    My Fiddle code

    How can I do that..?