Reputation: 5002
I have a HTML table (id="tblID")
that is populated from database. The rows have a data attribute status
, which can either be "Y"
or "N"
and only rows that have "Y"
are to be shown on page load. Somewhere in the $(document).ready()
there is this call: which is supposed to show the active rows on page load by passing "Y"
to the showRowsByStatus
:
$("#tblID tr").each(function () {
showRowsByStatus("Y", $(this));
});
There is also checkbox Show Inactive Rows
which when checked will show the inactive rows and when unchecked will show active rows.
$("#chkBox").change(function () {
if (this.checked) {
$("#tblID tr").each(function () {
showRowsByStatus("N", $(this));
});
}
else {
$("#tblID tr").each(function () {
showRowsByStatus("Y", $(this));
});
}
});
This is the definition of showRowsByStatus
method:
function showRowsByStatus(activeStatus, tr) {
if (tr.data("status") == activeStatus && tr.data("status") != undefined) {
tr.show();
}
if (tr.data("status") != activeStatus && tr.data("status") != undefined) {
tr.hide();
}
}
Now the problem I am facing is, on page load active rows are not being displayed at all, but when I toggle the checkbox Show Inactive Rows
, the rows are displayed as expected.
What am I missing in the showRowsByStatus
method?
I also found that style="display:none"
is being added to all rows when the page loads. I have checked every where in the project and I can't find anything that sets this style to these rows. I tried .css("display","none !important")
for hiding and .css("display","block !important")
for showing, but it didn't help. So I am thinking I should fix the showRowsByStatus
method.
Upvotes: 0
Views: 117
Reputation: 5050
This code is working fine and I changed nothing in your jQuery code. May be related to your HTML code around it. You can try it there : http://seo035.com/test/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test Inactive Row</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
</head>
<body>
<script>
$(document).ready( function(){
$("#tblID tr").each(function () {
showRowsByStatus("Y", $(this));
});
$("#chkBox").change(function () {
if (this.checked) {
$("#tblID tr").each(function () {
showRowsByStatus("N", $(this));
});
}
else {
$("#tblID tr").each(function () {
showRowsByStatus("Y", $(this));
});
}
});
})
function showRowsByStatus(activeStatus, tr) {
if (tr.data("status") == activeStatus && tr.data("status") != undefined) {
tr.show();
}
if (tr.data("status") != activeStatus && tr.data("status") != undefined) {
tr.hide();
}
}
</script>
<table id="tblID">
<tr data-status="Y"><td>Row Y</td></tr>
<tr data-status="N"><td>Row N</td></tr>
</table>
<form>
<input id="chkBox" type="checkbox" name="chkBox" value="">
</form>
</body>
</html>
Upvotes: 1