This needs some refinement, a lot actually, but it's working:
Now let's see about that other thing (untested):Code:<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Adding and Removing Rows from a table using DHTML and JavaScript </title> <script language="javascript"> //add a Node function addNode(tag, dat, el){ //create new node and get a reference to it var newNode = document.createElement(tag); //add attributes to the node for (var p in dat) if(p == 'text') newNode.appendChild(document.createTextNode(dat[p])); else if(typeof dat[p] != 'object') newNode[p] = dat[p]; else for (var sp in dat[p]) newNode[p][sp] = dat[p][sp]; //return it, or if a parent 'el' is supplied append it to the parent and return the parent if(!el) return newNode; el.appendChild( newNode ); return el; } function totRows(t){ if(typeof t=='number'&&t<4){ var tot=0; for (var v=document.getElementsByTagName('input'), i = v.length-1; i > -1; --i) if(v[i].name=='t'+t) tot += (v[i].value-0); document.getElementById('t'+t+'t').value=tot; if(t<3) totRows(++t); }else totRows(1); } //add a new row to the table function addRow() { //add a row to the rows collection and get a reference to the newly added row var newRow = document.getElementById("tblGrid").insertRow(-1); if(document.getElementById('totals')){ addRow.foot = document.getElementById('totals'); addRow.footP = addRow.foot.parentNode; addRow.foot = addRow.footP.removeChild(addRow.foot); } newRow.className = "even"; //add 4 cells (<td>) to the new row and populate them with elements var oCell = newRow.insertCell(-1); addNode('span', {className:'counter', text:document.getElementById("tblGrid").rows.length-1}, oCell); oCell = newRow.insertCell(-1); addNode('input', {type:'text', name:'t1', onchange:totRows}, oCell); oCell = newRow.insertCell(-1); addNode('input', {type:'text', name:'t2', onchange:totRows}, oCell); oCell = newRow.insertCell(-1); oCell.appendChild(addNode('input', {type:'text', name:'t3', onchange:totRows}, addNode('div', {style:{textAlign:'center'}}))); oCell = newRow.insertCell(-1); addNode('input', {type:'button', value:'Delete', onclick:function(){removeRow(this);}}, oCell); addRow.footP.appendChild(addRow.foot); } //deletes the specified row from the table function removeRow(src) { /* src refers to the input button that was clicked. to get a reference to the containing <tr> element, get the parent of the parent (in this case case <tr>) */ var oRow = src.parentNode.parentNode, oTable = document.getElementById("tblGrid"); //once the row reference is obtained, delete it passing in its rowIndex oTable.deleteRow(oRow.rowIndex); for (var c=oTable.rows.length, s=oTable.getElementsByTagName('span'), i = s.length-1; i > -1; --i) if(s[i].className == 'counter') s[i].firstChild.nodeValue = --c-1; totRows(); if(oTable.rows.length==2){ addRow.foot = document.getElementById('totals'); addRow.footP = addRow.foot.parentNode; addRow.foot = addRow.footP.removeChild(addRow.foot); } } window.onload=totRows; </script> <style> /*---------------------------------*/ /* Table Grid */ /*---------------------------------*/ /* The initial default settings for the Table */ table.grid { width: 100%; height: 20px; border: 1px solid #6688A4; border-collapse: collapse; } /* Style for the title header of the table */ tr#header { border-bottom: 1px solid #6688A4; background-color: #6688A4; color: #FFFFFF; font-family: Arial; font-weight: bold; font-size: 11px; padding-left: 0px; height: 20px; } tr#header th{ padding-left: 12px; color: #FFFFFF; font-family: arial; font-size: 11px; text-align: left; } /* Style for the row containing the MAIN PARAMETERS for the table (row after the Title row) */ tr#mainDiv { background-color: #CCCCCC; color: #000; border-top: 1px solid #FFFFFF; border-left: 1px solid #6688A4; border-right: 1px solid #6688A4; height: 18px; } tr#mainDiv th{ text-align: center; font-family: arial; font-size: 11px; font-weight: normal; border-top: 1px solid #FFFFFF; border-left: 1px solid #FFFFFF; border-right: 1px solid #FFFFFF; } /* Style for the EVEN data rows with the white background */ tr.even { background-color: #FFFFFF; border-left: 1px solid #6688A4; border-right: 1px solid #6688A4; height: 28px; } tr.even th,tr.even td{ padding-left: 5px; color: #333333; font-family: arial; font-size: 11px; font-weight:normal; text-align: left; padding-right: 5px; border: 1px solid #E6E6E6; } tr.even th#yr{ text-align: center; } tr.even th#inc{ color: #008000; } tr.even th#dec{ color: #EF0303; } </style> </head> <body> <p> Demo of a simple table grid that allows adding and deleting rows using D HTML and Javascript </p> <p> Try it out - Click on the Delete button to delete the corresponding row. Click Add Row button to insert a new row. </p> <p>Browser compatility - this sample has been tested to work with IE5.0 and above. </p> <input type="button" value="Add Row" onClick="addRow();"> <hr> <!-- sample table grid with 3 columns and 4 rows that are presented by d efault --> <table class="grid" id="tblGrid" style="table-layout:fixed" border="0"> <tr id="mainDiv"> <th style="width:1em;">#</th> <th style="width:150px;">Field1</th> <th style="width:150px;">Field2</th> <th style="width:250px;">Field3</th> <th style="width:250px;">Action</th> </tr> <tr class="even"> <th><span class="counter">1</span></th> <th><input type="text" name="t1" onchange="totRows();"></th> <th><input type="text" name="t2" onchange="totRows();"></th> <th><div style="text-align:center;"><input type="text" name="t3" onchange="totRows();"></div></th> <th><input type="button" value="Delete" onClick="removeRow(this);"></th> </tr> <tr id="totals" class="even" style="background-color:gray;"> <th style="background-color:#fff;"><span> </span></th> <th><input type="text" id="t1t" name="t1t" value=0 readonly></th> <th><input type="text" id="t2t" name="t2t" value=0 readonly></th> <th><div style="text-align:center;"><input type="text" id="t3t" name="t3t" value=0 readonly></div></th> <th style="background-color:#fff;"><input type="button" value="Totals"></th> </tr> </table> <hr> </body> </html>
If the functions would be used by other nodes, they could be defined separately and then used in addNode like I did the totRows function in the new version of the page above. This is a great savings over innerHTML;Code:addNode('input', {type:'text', size:10, className:'normal', onfocus:function(){this.style.background='#f3f6f9';}, onblur:function(){this.style.background='#fff'}}, oCell);



Reply With Quote


(I can actually figure out how to remove the totals line all by myself 
Bookmarks