Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery to slideToggle a group of Table Rows

I'm fairly new to javaScript and jQuery, so hopefully this will be a quick fix. I need to display a table containing data that can be grouped into several categories, and I'd like to implement a slideToggle that hides/reveals all of the observations in each given category.

The code below should (ideally) display a table with 4 columns and 9 rows, with every group of 3 rows preceded by a green "Section i" row. I would like each Section header to work as a slideToggle that expands or collapses all of the rows beneath it. Right now, nothing is collapsing. Any thoughts?

<head>
  <style type="text/css">
    td{padding:5px;}
  </style>

  <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/javascript"> 
      $(document).ready(function(){
      $(".flip").click(function(){
          $(this).next(".section").slideToggle();
      });
  });
  </script>

</head>

<body>
    <p>
        <table id="main_table">
        <thead>
            <tr class="firstline">
                <th>Column1</th>
                <th>Column2</th>
                <th>Column3</th>
                <th>Column4</th>
            </tr>
        </thead>
        <tbody>
            <tr style="background-color:green; color:white"> 
                <td  colspan="4" class="flip"> Section 1 </td> 
            </tr>
            <div class="section">
            <tr>
                <td>item 111</td>
                <td>item 112</td>
                <td>item 113</td>
                <td>item 114</td>
            </tr>
            <tr>
                <td>item 121</td>
                <td>item 122</td>
                <td>item 123</td>
                <td>item 124</td>
            </tr>
            <tr>
                <td>item 131</td>
                <td>item 132</td>
                <td>item 133</td>
                <td>item 134</td>
            </tr>
            </div>
            <tr style="background-color:green; color:white"> 
                <td  colspan="4" class="flip"> Section 2 </td> 
            </tr>
            <div class="section">
            <tr>
                <td>item 211</td>
                <td>item 212</td>
                <td>item 213</td>
                <td>item 214</td>
            </tr>
            <tr>
                <td>item 221</td>
                <td>item 222</td>
                <td>item 223</td>
                <td>item 224</td>
            </tr>
            <tr>
                <td>item 231</td>
                <td>item 232</td>
                <td>item 233</td>
                <td>item 234</td>
            </tr>
            </div>
            <tr style="background-color:green; color:white"> 
                <td  colspan="4" class="flip"> Section 3 </td> 
            </tr>
            <div class="section">
            <tr>
                <td>item 311</td>
                <td>item 312</td>
                <td>item 313</td>
                <td>item 314</td>
            </tr>
            <tr>
                <td>item 321</td>
                <td>item 322</td>
                <td>item 323</td>
                <td>item 324</td>
            </tr>
            <tr>
                <td>item 331</td>
                <td>item 332</td>
                <td>item 333</td>
                <td>item 334</td>
            </tr>
            </div>
        </tbody>
        </table>
    </p>
</body>
like image 934
CCC Avatar asked Feb 24 '11 23:02

CCC


People also ask

How to slide toggle of a row in the table?

You can slide toggle of a row in the table . Please try this The jQuery code is here on click of "a" href, the table will be toggled. For particular index row you can use $ ('table tr:eq (rowindex) td').

How to group rows in table using jQuery UI?

Create a standard Html table and group the table rows using data-level attribute. 3. Load jQuery, jQuery UI and the jQuery Tabelizer plugin's script at the bottom of the document. 4. Initialize the plugin. 5. Full plugin options. This awesome jQuery plugin is developed by powerconsulting.

Is there a way to group data in an HTML table?

It’s easy to group data in SQL statement, but if you have to display it using HTML table, It’s not drag and drop as in reporting environment. One way to group on server side and draw tr td accordingly. But If it is already created with tons of functionality attached, It’s risky to change. Don’t worry, you can do it easily with jQuery.

What is the difference between the methods slidetoggle () and Slidedown ()?

The slideToggle () method toggles between slideUp () and slideDown () for the selected elements. This method checks the selected elements for visibility. slideDown () is run if an element is hidden. slideUp () is run if an element is visible - This creates a toggle effect. Optional. Specifies the speed of the slide effect.


1 Answers

You can't mix <div>s into a <table> like that, use additional <tbody> elements instead. In your callback, this is the <td> element which has no siblings so .next does nothing useful; you want to go back up until you get to something that is at the same depth as the .section you're interested in and then call .next from there.

Your HTML should look more like this:

<table id="main_table">
    <thead>
        <tr class="firstline">
            <th>Column1</th>
            <th>Column2</th>
            <th>Column3</th>
            <th>Column4</th>
        </tr>
    </thead>
    <tbody>
        <tr style="background-color:green; color:white">
            <td  colspan="4" class="flip"> Section 1 </td>
        </tr>
    </tbody>
    <tbody class="section">
        <tr>
            <td>item 111</td>
            <td>item 112</td>
            <td>item 113</td>
            <td>item 114</td>
        </tr>
        <!-- ... -->

And your click handler like this:

$('.flip').click(function() {
    $(this)
        .closest('tbody')
        .next('.section')
        .toggle('fast');
});

The .closest call goes back up your ancestors until it finds a <tbody> and then you call .next on that.

Updated jsfiddle: http://jsfiddle.net/ambiguous/Udxyb/4/

like image 200
mu is too short Avatar answered Sep 29 '22 19:09

mu is too short