tags:

views:

47

answers:

3

I want to filter certain rows out of a table and am using classes to categorize the rows.

The below code enables me to show and hide row data categorized as "QUO" and "CAL" (eventually there will be other categories.

Can someone point me towards a more elegant solution, so I don't have to duplicate code for each category as I have below?

Thanks!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; 
<html xmlns="http://www.w3.org/1999/xhtml"&gt; 
<html>
<head>
    <title>Untitled</title>
    <style>

    </style>
    <script src="Javascript/jquery-1.4.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function () {
    $("#toggle_ac_cal").click(function()                
    {
        var checked_status = this.checked;
        if (checked_status==true)
        {
        $(".ac_cal").show()
        }
        else
        {
        $(".ac_cal").hide()
        }
    });     
    $("#toggle_ac_quo").click(function()                
    {
        var checked_status = this.checked;
        if (checked_status==true)
        {
        $(".ac_quo").show()
        }
        else
        {
        $(".ac_quo").hide()
        }
    });         
    });
    </script>
</head>
<body>
<input type="checkbox" id="toggle_ac_cal" checked="checked" />CAL<br/>
<input type="checkbox" id="toggle_ac_quo" checked="checked" />QUO<br/>
<table>
<tbody>
<tr class="ac_cal">
<td>CAL</td>
<td>10 Oct</td>
<td>John Barnes</td>
</tr>
<tr class="ac_cal">
<td>CAL</td>
<td>10 Oct</td>
<td>Neil Burton</td>
</tr>
<tr class="ac_quo">
<td>QUO</td>
<td>11 Oct</td>
<td>Neil Armstrong</td>
</tr>
</tbody>
</table>
</body>
</html>
+1  A: 
Pointy
Thanks for your answer, but your code doesn't seem to pluck the target correctly. If i look at the variable "target" it returns "toggler target:ac_cal" rather than just "ac_cal", which I think was the desired effect.
Neil Burton
I have to ask, why am I seeing `[this.checked ? "show" : "hide"]()` everywhere today instead of the shorter/simpler built-in methods, e.g. `.toggle(this.checked);`?
Nick Craver
@Neil doh sorry I'll fix that -- @Nick you're probably right but personally I distrust ".toggle"; irrational I know
Pointy
more: @Nick I distrust "toggle" because I want to be sure that I've got solid correlation between the "checked" state of the toggler and the current visibility of the controlled element. I know that they *shouldn't* get out of sync, but it makes me happier to lock it together more explicitly.
Pointy
@Pointy - `.toggle()` takes a boolean, on of off like I have, that's why I ask, it calls `show` or `hide` internally already. You can see the internals here: http://github.com/jquery/jquery/blob/master/src/effects.js#L90
Nick Craver
@Nick duhhhhhhhhhh of course you're right as usual :-)
Pointy
+1  A: 

You can reduce your code (without changing markup) down to this:

$("[id^='toggle_'").click(function() {
  $("." + this.id.replace('toggle_','')).toggle(this.checked);
});

Though, if you gave your toggle elements a class instead, like .toggle you can clean up the original selector, like this:

$(".toggle").click(function() {
  $("." + this.id.replace('toggle_','')).toggle(this.checked);
});

You could also give them a class and use the value as the target class, like this:

<input type="checkbox" class="toggle" value="ac_cal" />

Then your jQuery is just this, short and simple:

$(".toggle").click(function() {
  $("." + this.value).toggle(this.checked);
});
Nick Craver
+1 Beat me by 30 seconds :)
fudgey
@fudgey - Thanks :) Just curious, why the interest in `[this.checked ? "show" : "hide"]()` today over `.toggle(this.checked);`?
Nick Craver
This seems the most elegant solution to me. Many thanks for your input. I'm a relative newbie to javascript / jquery, so love to learn there's always a better way.
Neil Burton
@Nick Craver: I copied that part from Pointy because it was cleaner than what I had. Personally I avoid using toggle for personal reasons, mostly because it likes to act up on me - I think I'm cursed LOL.
fudgey
@Neil Burton: "better" is subjective as there are many different ways to do the same thing as you can see from your answers; that being said, if Nick has an answer, chances are it's the best ;)
fudgey
A: 

Try this (demo):

HTML (the id should match the classes in the table):

<input type="checkbox" id="ac_cal" checked="checked" />CAL<br/>
<input type="checkbox" id="ac_quo" checked="checked" />QUO<br/>

Script:

$(document).ready(function () {
    $("input[id^=ac]").click(function()                
    {
        $('tr.' + this.id)[this.checked ? "show" : "hide"]();
    })       
});
fudgey