user3332446
user3332446

Reputation: 79

how to write jQuery for expand/collapse of rows?

This is my html code: http://jsfiddle.net/Udxyb/406/

In the above jQuery code am getting the tables opened by default and when I click on it then it is closing. but what I want is by default the tables must be closed and when I click it then the tables must open???

Any help would be appreciated?

jQuery(function($) {
  $("#polls").on("click", ".flip", function() {
    $(this)
      .closest('tbody')
      .next('.section')
      .toggle('fast');
  }); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="polls" style=" border: 1px solid #ccc;">
  <table id="main_table" style="width: 1002px; border: 1px solid #ccc;">
    <tbody>
      <tr style="background-color:green; color:white">
        <td  colspan="" class="flip"> Section 1</td>
        <td  colspan="" class="flip"> Section 2 </td>
        <td  colspan="" class="flip"> Section 3 </td>
        <td  colspan="" class="flip"> Section 4 </td>
      </tr>
    </tbody>
    <tbody 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>
    </tbody>
    <tbody>
      <tr style="background-color:green; color:white">
        <td  colspan="" class="flip"> Section 1 </td>
        <td  colspan="" class="flip"> Section 2 </td>
        <td  colspan="" class="flip"> Section 3 </td>
        <td  colspan="" class="flip"> Section 4 </td>
      </tr>
    </tbody>
    <tbody 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>
    </tbody>
    <tbody>
      <tr style="background-color:green; color:white">
        <td  colspan="" class="flip"> Section 1 </td>
        <td  colspan="" class="flip"> Section 2 </td>
        <td  colspan="" class="flip"> Section 3 </td>
        <td  colspan="" class="flip"> Section 4 </td>
      </tr>
    </tbody>
    <tbody 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>
    </tbody>
  </table>
</div>

Upvotes: 0

Views: 125

Answers (5)

Jak
Jak

Reputation: 3233

I updated the code. Please check now

$('#main_table').find('.section').hide();

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

Upvotes: 0

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

Try

$(".section").hide();
  jQuery(function($) {
    $("#polls").on("click", ".flip", function() {
      $(this)
        .closest('tbody')
        .next('.section')
        .toggle('fast');
    });
  });
});

DEMO

Upvotes: 0

user3379655
user3379655

Reputation: 258

Before your jQuery(function($) code add this :

  $(".section").hide();

Upvotes: 0

Tushar
Tushar

Reputation: 4418

Just add

.section {
    display: none
}

in your CSS.

Upvotes: 2

Bluedevil678
Bluedevil678

Reputation: 120

This may help as it seems to be a rather simple guide, let me know.

http://www.jankoatwarpspeed.com/expand-table-rows-with-jquery-jexpand-plugin/

Upvotes: 0

Related Questions