I want to be able to show/hide the rows in a table using jquery. Ideally I want to have buttons above the table to sort the table with.
i.e [Show rows with id:black] [Show rows with id:white] [Show all rows]
I have searched all over but cannot find a solution. Anyone know how i can do this with jquery and make it cross browser compatible?
Thanks (Code below)
<table class="someclass" border="0" cellpadding="0" cellspacing="0" summary="bla bla bla">
<caption>bla bla bla</caption>
<thead>
<tr id="black">
<th>Header Text</th>
<th>Header Text</th>
<th>Header Text</th>
<th>Header Text</th>
<th>Header Text</th>
<th>Header Text</th>
</tr>
</thead>
<tbody>
<tr id="white">
<td>Some Text</td>
<td>Some Text</td>
<td>Some Text</td>
<td>Some Text</td>
<td>Some Text</td>
<td>Some Text</td>
</tr>
<tr id="black">
<td>Some Text</td>
<td>Some Text</td>
<td>Some Text</td>
<td>Some Text</td>
<td>Some Text</td>
<td>Some Text</td>
</tr>
</tbody>
Change your black and white IDs to classes instead (duplicate IDs are invalid), add 2 buttons with the proper IDs, and do this:
It uses the
filter()[docs] method to filter the rows with theblackorwhiteclass (depending on the button).Then it uses the
not()[docs] method to do the opposite filter, excluding theblackorwhiterows that were previously found.EDIT: You could also pass a selector to
.not()instead of the previously found set. It may perform better that way:…or better yet, just keep a cached set of both right from the start: