beetle
08-30-2002, 05:07 PM
Hey all
Here's the setup. I've got a PHP page that delivers MySql data into an HTML table for a To Do list. Works peachy. Now, I wanted to add a filtering tool to the table, which I've also done, and it works fine. However, I'm dissatisfied with some of the methods I've used becuase they involve a bit of hard coding, and will make it difficult to expand on later down the road. I think you will see what I mean. There's a decent amount of code, but I want to include it all so that you may just copy>paste it and see the page as I do. Also, I thought I was using strictly DOM methods, but this isn't working in NS6 :(
Thanks to those who have the patience for this.<!doctype html public "-//W3C//DTD HTML 3.2 Final//EN" />
<html>
<head>
<title>Dynamic Table Test</title>
<style>
td {
font-size: 12px;
}
h2, h3 {
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #696969;
}
table.dispTable {
font-family: Verdana, sans-serif;
font-size: 10px;
border: 1px solid black;
width: 585px;
padding: 0px;
margin: 5px 0px;
}
table.dispTable th {
color: white;
font-weight: bold;
font-size: 120%;
background-color: #999;
padding: 3px;
text-align: left;
}
table.dispTable tr td {
padding: 3px;
}
table.dispTable tr.odd {
background-color: #FFF;
}
table.dispTable tr.even {
background-color: #EEE;
}
</style>
<script>
function doFilter(t, val, cIndex) {
var cIndex2 = (cIndex == 1) ? 2 : 1;
var sel2 = t.childNodes[1].cells[cIndex2].firstChild;
var val2 = sel2.options[sel2.selectedIndex].value;
for (var i=2; i<t.childNodes.length; i++) {
var tr = t.childNodes[i]
var txtNode1 = tr.cells[cIndex].firstChild.data;
var txtNode2 = tr.cells[cIndex2].firstChild.data;
if ((txtNode1 == val || val == 'all') && (txtNode2 == val2 || val2 == 'all'))
tr.style.display = "";
else
tr.style.display = "none";
}
}
</script>
</head>
<body>
<h3>Jon's To Do List</h3>
<table class="dispTable" cellspacing="0">
<tr id="1">
<th>Title</th>
<th>Priority</th>
<th>Status</th>
<th>Date</th>
</tr>
<tr id="2">
<td style="text-align:right">Filter:</td>
<td>
<select id="fil_priority" onChange="doFilter(this.parentNode.parentNode.parentNode,this.options[this.selectedIndex].value, this.parentNode.cellIndex);">
<option value="all">All</option>
<option value="Low">Low</option>
<option value="Medium">Medium</option>
<option value="High">High</option>
<option value="Critical">Critical</option>
<option value="N/A">N/A</option>
</select>
</td>
<td>
<select id="fil_status" onChange="doFilter(this.parentNode.parentNode.parentNode,this.options[this.selectedIndex].value, this.parentNode.cellIndex);">
<option value="all">All</option>
<option value="Open">Open</option>
<option value="Deferred">Deferred</option>
<option value="Complete">Complete</option>
<option value="Archived">Archived</option>
<option value="Prepaid">Prepaid</option>
</select>
</td>
<td>Date</td>
</tr>
<tr id="3" class="odd">
<td>Test #1</td>
<td>Medium</td>
<td>Open</td>
<td>8/12/02</td>
</tr>
<tr id="4" class="even">
<td>Test #2</td>
<td>High</td>
<td>Complete</td>
<td>8/12/02</td>
</tr>
<tr id="5" class="odd">
<td>Fix Servers</td>
<td>High</td>
<td>Complete</td>
<td>8/13/02</td>
</tr>
<tr id="6" class="even">
<td>Clean House</td>
<td>Critical</td>
<td>Complete</td>
<td>8/16/02</td>
</tr>
<tr id="7" class="odd">
<td>another test</td>
<td>Critical</td>
<td>Open</td>
<td>8/16/02</td>
</tr>
<tr id="8" class="even">
<td>Test Again!!!</td>
<td>Medium</td>
<td>Open</td>
<td>8/16/02</td>
</tr>
</table>
</body>
</html>
Here's the setup. I've got a PHP page that delivers MySql data into an HTML table for a To Do list. Works peachy. Now, I wanted to add a filtering tool to the table, which I've also done, and it works fine. However, I'm dissatisfied with some of the methods I've used becuase they involve a bit of hard coding, and will make it difficult to expand on later down the road. I think you will see what I mean. There's a decent amount of code, but I want to include it all so that you may just copy>paste it and see the page as I do. Also, I thought I was using strictly DOM methods, but this isn't working in NS6 :(
Thanks to those who have the patience for this.<!doctype html public "-//W3C//DTD HTML 3.2 Final//EN" />
<html>
<head>
<title>Dynamic Table Test</title>
<style>
td {
font-size: 12px;
}
h2, h3 {
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #696969;
}
table.dispTable {
font-family: Verdana, sans-serif;
font-size: 10px;
border: 1px solid black;
width: 585px;
padding: 0px;
margin: 5px 0px;
}
table.dispTable th {
color: white;
font-weight: bold;
font-size: 120%;
background-color: #999;
padding: 3px;
text-align: left;
}
table.dispTable tr td {
padding: 3px;
}
table.dispTable tr.odd {
background-color: #FFF;
}
table.dispTable tr.even {
background-color: #EEE;
}
</style>
<script>
function doFilter(t, val, cIndex) {
var cIndex2 = (cIndex == 1) ? 2 : 1;
var sel2 = t.childNodes[1].cells[cIndex2].firstChild;
var val2 = sel2.options[sel2.selectedIndex].value;
for (var i=2; i<t.childNodes.length; i++) {
var tr = t.childNodes[i]
var txtNode1 = tr.cells[cIndex].firstChild.data;
var txtNode2 = tr.cells[cIndex2].firstChild.data;
if ((txtNode1 == val || val == 'all') && (txtNode2 == val2 || val2 == 'all'))
tr.style.display = "";
else
tr.style.display = "none";
}
}
</script>
</head>
<body>
<h3>Jon's To Do List</h3>
<table class="dispTable" cellspacing="0">
<tr id="1">
<th>Title</th>
<th>Priority</th>
<th>Status</th>
<th>Date</th>
</tr>
<tr id="2">
<td style="text-align:right">Filter:</td>
<td>
<select id="fil_priority" onChange="doFilter(this.parentNode.parentNode.parentNode,this.options[this.selectedIndex].value, this.parentNode.cellIndex);">
<option value="all">All</option>
<option value="Low">Low</option>
<option value="Medium">Medium</option>
<option value="High">High</option>
<option value="Critical">Critical</option>
<option value="N/A">N/A</option>
</select>
</td>
<td>
<select id="fil_status" onChange="doFilter(this.parentNode.parentNode.parentNode,this.options[this.selectedIndex].value, this.parentNode.cellIndex);">
<option value="all">All</option>
<option value="Open">Open</option>
<option value="Deferred">Deferred</option>
<option value="Complete">Complete</option>
<option value="Archived">Archived</option>
<option value="Prepaid">Prepaid</option>
</select>
</td>
<td>Date</td>
</tr>
<tr id="3" class="odd">
<td>Test #1</td>
<td>Medium</td>
<td>Open</td>
<td>8/12/02</td>
</tr>
<tr id="4" class="even">
<td>Test #2</td>
<td>High</td>
<td>Complete</td>
<td>8/12/02</td>
</tr>
<tr id="5" class="odd">
<td>Fix Servers</td>
<td>High</td>
<td>Complete</td>
<td>8/13/02</td>
</tr>
<tr id="6" class="even">
<td>Clean House</td>
<td>Critical</td>
<td>Complete</td>
<td>8/16/02</td>
</tr>
<tr id="7" class="odd">
<td>another test</td>
<td>Critical</td>
<td>Open</td>
<td>8/16/02</td>
</tr>
<tr id="8" class="even">
<td>Test Again!!!</td>
<td>Medium</td>
<td>Open</td>
<td>8/16/02</td>
</tr>
</table>
</body>
</html>