Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 522
norixxx Avatar asked Mar 23 '23 23:03

norixxx


1 Answers

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.

like image 198
SachinGutte Avatar answered Apr 01 '23 06:04

SachinGutte