views:

317

answers:

2

For this HTML code:

<table>
    <tr><td>Visible</td></tr>
    <tr><td>Visible</td></tr>
    <tr><td>Visible</td></tr>
    <!-- etc... -->
    <tr style="display:none"><td>Hidden</td></tr>
    <tr style="display:none"><td>Hidden</td></tr>
    <tr style="display:none"><td>Hidden</td></tr>
    <!-- etc... -->
    <tr><td><a class="show-5-more">Show 5 More</a></td></tr>
</table>

Here is the jQuery code I've got so far:

//show 5 more table rows (that were previously hidden)
$('a.show-5-more').click(function() {
    alert('clicked!');
    return false;
});

How can I do this:

  • Select the first hidden table row, then slideDown() the table row
  • select the next one, slide it down
  • select the next one, etc...
  • I want to do this for 5 hidden table rows
+2  A: 

This will slide down the first five hidden rows in the same table in which the link is clicked:

$('a.show-5-more').click(function() {
  $(this).closest("table").find("tr:hidden:lt(5)").slideDown();
  return false;
});

If you want to slide them down one after the other you have a couple of options. Probably the easiest is to use timeouts to effectively chain them together:

$('a.show-5-more').click(function() {
  $(this).closest("table").find("tr:hidden:lt(5)").stop().each(function(i, val) {
    slide_down(this, i, 600);
  });
  return false;
});

function slide_down(el, i, delay) {
  setTimeout(function() {
    $(el).slideDown(delay);
  }, i * delay);
}

Alternatively you could try chaining the callbacks on slideDown() together.

What I would advise in this scenario however is to use <tbody> to make this much easier to implement:

<table>
<tbody>
  <tr><td>Visible</td></tr>
  <tr><td>Visible</td></tr>
  <tr><td>Visible</td></tr>
  ...
</tbody>
<tbody style="display: none;">
  <tr><td>Hidden</td></tr>
  <tr><td>Hidden</td></tr>
  <tr><td>Hidden</td></tr>
  ...
</tbody>
<tbody style="display: none;">
  <tr><td>Hidden</td></tr>
  <tr><td>Hidden</td></tr>
  <tr><td>Hidden</td></tr>
  ...
</tbody>
<tr><td><a class="show-5-more">Show 5 More</a></td></tr>
</table>

and then:

$('a.show-5-more').click(function() {
  $(this).closest("table").find("tbody:hidden:first").slideDown("slow");
  return false;
});

That's a much more straightforward of grouping rows for this kind of thing.

cletus
this works, but they all slide down at the same time, not one after another. Is there a way to wait until the animation has finished before starting the next slide animation?
Andrew
Ah, looks like I misunderstood. +1 this is better.
womp
all the effects have a callback function...is there a way you can use the callback function to show the next one after the current table row has finished showing?
Andrew
A: 

To show 5 a time you can use

$(function() {
  $('.show-5-more').click(function() {
    $(this).closest('table').data('row', 0);
    slideDownNext.call(this);
  });
});

function slideDownNext() {
  var $table = $(this).closest('table');
  if($table.data('row') < 5) {
    $table.data('row', $table.data('row') + 1);
    $table.find('tr:hidden:first').slideDown('', slideDownNext);
  }
}
Yuriy Faktorovich
this looks like it shows all the the table rows...how can you show only 5 at a time?
Andrew
@Andrew I've fixed it.
Yuriy Faktorovich