Menu
I have been able to create a dynamic table and populate it.
My question is, what removes the entire table (or specifically just the tbody portion)
so that I can replace it with a newer table?
Do I need to remove individual rows from the table? If so, do I need to remove FIFO or LIFO?
Currently, the new information is appended to the old table and the table becomes twice as long.
I want to basically ‘refresh’ the table rows/cells only with newer information.
[code=php]
<html>
<head>
<title>Dynamic Table Test</title>
<head>
<script type="text/javascript">
function MakeTable() { // use following for DYNAMIC table test creation
var tRows = document.getElementById('BQtable');
for (var r=0; r<5; r++) {
var tr = tRows.insertRow(tRows.rows.length);
var td = tr.insertCell(tr.cells.length);
td.innerHTML = 'Row: '+r;
for (var c=0; c<5; c++) { td = tr.insertCell(tr.cells.length); td.innerHTML = 'Col:'+c; }
}
}
function ClrTable() {
// remove Rows From Table
var tbl = document.getElementById('BQtable');
var lastRow = tbl.rows.length;
while (lastRow > 0) { tbl.deleteRow(lastRow - 1); lastRow = tbl.rows.length; }
}
</script>
</head>
<body>
<button onClick="MakeTable()"> Make Table </button>
<button onClick="ClrTable()"> Clear Table </button>
<!-- Dynamic table definition -->
<div id="BQscore">
<table id="BQtable" border="1"></table>
</div>
</body>
<html>
[/code]
[code=php]
<html>
<head>
<title>Dynamic Table Test</title>
<head>
<script type="text/javascript">
function MakeTable() { // use following for DYNAMIC table test creation
var tBody = document.createElement('tbody');
var tRows = document.getElementById('BQtable');
for (var r=0; r<5; r++) {
var tr = tRows.insertRow(tRows.rows.length);
// var td = tr.insertCell(tr.cells.length);
// td.innerHTML = 'Row: '+r;
for (var c=0; c<5; c++) {
td = tr.insertCell(tr.cells.length);
td.innerHTML = 'RC:'+r+c;
}
}
tBody = appendChild(tr);
}
function ClrTable() {
// remove Rows From Table
var tbl = document.getElementById('BQtable');
var lastRow = tbl.rows.length;
while (lastRow > 0) { tbl.deleteRow(lastRow - 1); lastRow =
tbl.rows.length; }
}
</script>
</head>
<body>
<button onClick="MakeTable()"> Make Table </button>
<button onClick="ClrTable()"> Clear Table </button>
<!-- Dynamic table definition -->
<div id="BQscore">
<!-- Note: next line leaves small footprint with border=1 -->
<table id="BQtable" border="1"></table>
</div>
</body>
</html>
[/code]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Dynamic Tables</title>
<i> </i><script type="text/javascript">
<i> </i> function MakeTable() { // use following for DYNAMIC table test creation
<i> </i> // Create the TBODY element, all TR's appended to this
<i> </i> var tBody = document.createElement('tbody');
<i> </i> // Define vars to hold TR and TD nodes, to not redefine already defined vars
<i> </i> var tr;
<i> </i> var td;
<i> </i> // Set basic data for row and column loops
<i> </i> var r = 0;
<i> </i> var c = 0;
<i> </i> var maxRows = 5;
<i> </i> var maxCols = 5;
<i> </i> // Get a node reference to the TABLE and append the TBODY element to it
<i> </i> document.getElementById('BQTable').appendChild(tBody);
<i> </i> // Loop through maxRows to create TR's
<i> </i> for (r; r<maxRows; r++) {
<i> </i> tr = tBody.insertRow(tBody.rows.length);
<i> </i> // Loop through maxCols to create TD's
<i> </i> for (c = 0; c<maxCols; c++) {
<i> </i> td = tr.insertCell(tr.cells.length);
<i> </i> td.innerHTML = 'RC:'+r+c;
<i> </i> }
<i> </i> }
<i> </i> // Nullify node references
<i> </i> tr = td = tBody = null;
<i> </i> }
<i> </i> function ClearTable() {
<i> </i> var table = document.getElementById("BQTable");
<i> </i> // Remove the TBODY since all rows are inside it
<i> </i> table.removeChild(table.getElementsByTagName("tbody")[0]);
<i> </i> // Nullify the node reference to the TABLE
<i> </i> table = null;
<i> </i> }
<i> </i></script>
</head>
<body>
<input type="button" onclick="MakeTable();" value="Create Table">
<input type="button" onclick="ClearTable();" value="Clear Table">
<table cellpadding="3" cellspacing="0" border="1" id="BQTable"></table>
</body>
</html>
1. Where did you read about the multiple assignment statement that follows?
// Nullify node references
tr = td = tBody = null;
Pretty neat, but I was not aware you could do t his.
Are there any limitations to its use? (Like objects vs. strings vs. elements vs. ???)[/QUOTE]
function myStuff() {
var a = b = c = 42;
}
2. The removal of the 'tbody' contents looks easier than my row removal loop in the ClearTable function.
Why is the 'table=null' in there? I guess, a better question is where was it created to != null?
I thought this was created by the <table> tag you have after the <input> button definitions.[/QUOTE]
0.1.9 — BETA 4.29