I am trying to dynamically replace a table in the dom, anyone have an
idea on how to do this.
here is some sample suedo code of what I want to do.
[code snipped]
Any ideas on how to do this?
innerHTML has a number of known issues, including a handful dealing with
tables. I don't remember what they are anymore, since I recommend
avoiding innerHTML whenever possible.
You are better off using DOM2 methods to build it.
I've ditched this method in favor of building the table server-side, to
keep my web-app running on non-JS enabled browsers... but here
is a stripped down copy of what I was using.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"
http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="generator" content="HTML Tidy for Linux/x86 (vers 1
September 2005), seewww.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body onload="drawTable();" >
<table>
<tbody id="partData">
</tbody>
</table>
<script type="text/javascript">
var plist = [
{
"idx":"538",
"section":"7",
"line":"1",
"zid":"1",
"zflag":"",
"zmfg":"TOM",
"zpnum":"230260",
"pdesc":"Carburetor A35 cpl."}
,
{
"idx":"539",
"section":"7",
"line":"2",
"zid":"2",
"zflag":"M",
"zmfg":"TOM",
"zpnum":"229827",
"pdesc":"Jet 53"}
,
{
"idx":"540",
"section":"7",
"line":"3",
"zid":"2",
"zflag":"M",
"zmfg":"TOM",
"zpnum":"217940",
"pdesc":"Jet 54"}
];
function drawTable() {
// This is the TABLE BODY id
var tbodyID = 'partData';
if (document.getElementById) {
clearChildNodes(tbodyID);
// assure bug-free redraw in Geck engine by
// letting window show cleared table
// Prolly not needed anymore, but I have one customer still on
// an old version of NS. I'd rather not hear him complain.
if (navigator.product && navigator.product == "Gecko") {
setTimeout("finishDrawTable('" + tbodyID + "')", 0);
} else {
finishDrawTable(tbodyID);
}
} else {
alert("Sorry, dynamic table feature works with IE5 or later for
Windows, and Netscape 6 or later.");
}
}
// Complete the reconstruction of the sorted table
function finishDrawTable(tbodyID) {
var tr, td, txt;
tbody = document.getElementById(tbodyID);
// create holder for accumulated tbody elements and text nodes
var frag = document.createDocumentFragment();
// loop through data source
for (var i = 0; i < plist.length; i++) {
var part = plist
;
tr = document.createElement("tr");
makeCell(part.zpnum, tr);
makeCell(part.pdesc, tr);
frag.appendChild(tr);
}
if (!tbody.appendChild(frag)) {
alert("This browser doesn't support dynamic tables.");
}
}
// Remove existing content of an element
function clearChildNodes(elemID) {
var elem = document.getElementById(elemID);
if (elem == undefined) return;
while (elem.childNodes.length > 0) {
elem.removeChild(elem.firstChild);
}
}
function makeCell(c, tr)
{
var tr, td, txt;
td = document.createElement("td");
txt = document.createTextNode(c);
td.appendChild(txt);
tr.appendChild(td);
return td;
}
</script>
</body>
</html>