I have a GridView, where the first field is a checkbox. I want to take action client-side (show or hide rows) depending on the state of the checkboxes.
Thanks to my previous StOf posting, I have a RadioButtonList properly firing my Javascript. Now it's a matter of obtaining a reference to the appropriate rows.
I have relied on several questions and answers to build my current Javascript:
$(function () {
$("#RadioButtonList1 input[id^=Radio]").click(function () {
var grd = $("#my_gridview");
var rows = $("#my_gridview tr:gt(0)");
switch (this.value) {
case "All":
{
$("#my_gridview tr").show();
}
case "HideRequested":
{
var rowToShow = rows.find("td:eq(0)").filter(checked != "checked");
rows.show().not(rowToShow).hide();
}
case "ShowRequested":
{
var rowToShow = rows.find("td:eq(0)").filter(checked == "checked");
rows.show().not(rowToShow).hide();
}
}
})
});
But when I run this, I get an error in the JS console: ReferenceError: checked is not defined. Which is odd, because it works in the other questions, and the generated HTML (as confirmed in the console) includes the checked
property in the input
element. The HTML is:
<table cellspacing="0" cellpadding="2" id="gv_grid" style="border-color:Black;border-width:1px;border-style:None;width:100%;border-collapse:collapse;">
<tr class="grGridViewHeader" style="white-space:nowrap;">
<th align="center" scope="col" style="width:100px;white-space:nowrap;">Select Entry</th><th scope="col">Entry Name</th><th align="left" scope="col">Type</th><th align="left" scope="col">Details</th>
</tr><tr valign="top" style="background-color:#BED3FC;border-style:None;">
<td align="center" valign="middle" style="white-space:nowrap;">
<input id="gv_grid_chk_selected_0" type="checkbox" name="gv_grid$ctl02$chk_selected" checked="checked" />
</td><td>Entry 1</td><td style="font-size:Small;">Foo</td><td style="font-size:Small;">Assorted text.</td>
</tr><tr valign="top" style="background-color:White;border-style:None;">
<td align="center" valign="middle" style="white-space:nowrap;">
<input id="gv_grid_chk_selected_1" type="checkbox" name="gv_grid$ctl03$chk_selected" checked="checked" />
</td><td>Entry 2</td><td style="font-size:Small;">Bar</td><td style="font-size:Small;">Assorted text.</td>
</tr><tr valign="top" style="background-color:#BED3FC;border-style:None;">
<td align="center" valign="middle" style="white-space:nowrap;">
<input id="gv_grid_chk_selected_2" type="checkbox" name="gv_grid$ctl04$chk_selected" checked="checked" />
</td><td>Entry 3</td><td style="font-size:Small;">Baz</td><td style="font-size:Small;">Assorted text.<br /></td>
</tr>
</table>
I have also tried:
var checkboxArray = $('#my_gridview td input:checkbox');
Which did produce an array of the checkbox elements, but I still couldn't figure out how to correctly filter for the checked
property nor how to get back to the governing tr
element.
Sources: