norixxx
norixxx

Reputation: 3169

jQuery Table row toggle show/hide associated with buttons

Need a little help with jQuery and HTML(table).

Pls take a look at my jsfiddle page to see what I am gonna try to do.
http://jsfiddle.net/nori2tae/U25EK/

Each TD contains certain value (from 01 to 06) and has CLASS name which is related to its value and the lists of buttons above the TABLE. When the page initially loaded, all those buttons are enabled that means all TABLE DATA are visible.

When I click to turn on/off the button, i want jQuery to observe ON/OFF status of buttons and if the status matches to the values of each table row, i want jQuery to toggleSlide(show/hide) TABLE ROW. (sorry about my poor english and explanation...)

For example, if I turn off button01 the row1 will be hidden. Then I turn button4 and button6 off, the row5 will be hidden. And so on or vice versa...

HTML:

<ul id="listBtns">
<li><a href="#" class="on">01</a></li>
<li><a href="#" class="on">02</a></li>
<li><a href="#" class="on">03</a></li>
<li><a href="#" class="on">04</a></li>
<li><a href="#" class="on">05</a></li>
<li><a href="#" class="on">06</a></li>
</ul>

<table id="tblResult">
<thead>
    <tr>
        <th></th>
        <th>01</th>
        <th>02</th>
        <th>03</th>
        <th>04</th>
        <th>05</th>
        <th>06</th>
    </tr>
</thead>
<tbody>
    <tr>
        <th>row1</th>
        <td class="val01">01</td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <th>row2</th>
        <td class="val01">01</td>
        <td class="val02">02</td>
        <td class="val03">03</td>
        <td class="val04">04</td>
        <td class="val05">05</td>
        <td class="val06">06</td>
    </tr>
    <tr>
        <th>row3</th>
        <td class="val03">03</td>
        <td class="val05">05</td>
        <td class="val04">04</td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <th>row4</th>
        <td class="val02">02</td>
        <td class="val04">04</td>
        <td class="val05">05</td>
        <td class="val06">06</td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <th>row5</th>
        <td class="val04">04</td>
        <td class="val06">06</td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <th>row6</th>
        <td class="val03">03</td>
        <td class="val02">02</td>
        <td class="val04">04</td>
        <td class="val06">06</td>
        <td class="val05">05</td>
        <td></td>
    </tr>
</tbody>
</table>

JS:

$('#listBtns a').click(function() {
    $(this).toggleClass('on off');
    //and some function continues...
});

I am kinda stack and only can come up with very poor and inefficient codes(that perhaps might not work completely...) and I dont know which jQuery selector to use. please help me out.

Thanx.

Upvotes: 4

Views: 8340

Answers (2)

Rikin Adhyapak
Rikin Adhyapak

Reputation: 483

You can also use some thing like below, give an id to your table like "my_table" so that in case of multiple table s in a page it will affect only specific table. and by using 2,3 you can refer row number #2 and #3 should be hide and #4, #5 means row number #4 and #5 should be display.

     $("#my_table tr:nth-child(2)").hide();
     $("#my_table tr:nth-child(3)").hide();

     $("#my_table tr:nth-child(4)").show();
     $("#my_table tr:nth-child(5)").show();

Call it on click event of any thing and check.

Upvotes: 0

SachinGutte
SachinGutte

Reputation: 7055

Need one line only --

$(function() {
    $('#listBtns a').click(function() {
       $(this).toggleClass('on off');        
       $("#tblResult tr").eq($(this).text()).toggle('on off');
    });
});

See demo

This will work as long as text and row number matches.

Upvotes: 7

Related Questions