 |
|
 |
How to insert radio button in html table in runtime
Sagar
|
|
|
|
 |
|
 |
eeraj,
I use your code and it works great ! Very simple and easy to modidy.
But I have two questions :
1. How do I format so that the additional rows/cells align with the first one. My first for uses | for each cell, and I tried that with your code but it did not work (maybe I did it wrong). So to align the additional row/cell, I just use It works, but I was wondering if there was a better way.
2. How do I validate the additional rows/cells for required entry. My first row is validated with javascript. The additional row/cells have the same name as the first rows but the javascript does not seem to apply to the additional row, just the first one. How can I validate the additional rows/cells for required entry ? |
|
|
|
 |
|
 |
<html> <head> <title>Adding and Removing Rows from a table using DHTML and JavaScript</title> <script language="JavaScript" type="text/javascript"> //add a new row to the table function addRow() { var rowNumber = document.getElementById("tblGrid").getElementsByTagName("TR").length; //add a row to the rows collection and get a reference to the newly added row var newRow = document.all("tblGrid").insertRow(-1); newRow.className = "even"; //add 3 cells (<th>) to the new row and set the innerHTML to contain text boxes var oCell = newRow.insertCell(-1); oCell.innerHTML = "<input type='text' name= 'tc6' id = " + rowNumber + "><input type='button' value='Delete' onclick='removeRow(this);'/>"; for( i=1; i<6; i++) { oCell = newRow.insertCell(-1); oCell.innerHTML = "<input type='text' name='tc" + i +"' id = " + rowNumber + " OnPropertyChange =\"y1total(this,'total" + i + "');\"/>"; } oCell = newRow.insertCell(-1); oCell.innerHTML = "<input type='text' name='tc7' disabled=true id = " + rowNumber + "/>"; } //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.parentElement.parentElement; var cols = oRow.cells; var total; var tempVal; for(i=1;i<cols.length-1;i++) { total=document.getElementById("total" + i ); tempVal = parseInt(total.value) - parseInt(cols[i].childNodes[0].value); if( tempVal == "0" || isNaN(tempVal) || tempVal == null) { total.value = ""; } else { total.value = tempVal; } //total=document.getElementById("TotalSaveingst" + i ); //total.value = parseInt(total.value) - parseInt(cols[i].childNodes[0].value); } //once the row reference is obtained, delete it passing in its rowIndex document.all("tblGrid").deleteRow(oRow.rowIndex); } function y1total( colObj, totalEleId ) { var total=document.getElementById(totalEleId); var x=0; var rowIdx = parseInt(colObj.id); var cols = document.getElementById('tblGrid').rows[rowIdx].cells; var rowTotal = cols[ cols.length - 1 ].childNodes[0]; var textBox= document.getElementsByName(colObj.name);//new Array(document.getElementById('r2t1'),document.getElementById('r3t1'),document.getElementById('r4t1'),document.getElementById('r6t1'),document.getElementById('r7t1'),document.getElementById('r8t1')); for(i=0;i<textBox.length;i++) { if(textBox[i].value != "") { x = x + parseInt(textBox[i].value); } } total.value = x; x = 0; for(i=1;i<cols.length-1;i++) { if(cols[i].childNodes[0].value != "") { x = x + parseInt(cols[i].childNodes[0].value); } } rowTotal.value = x; } </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> <table class="grid" id="tblGrid" style="table-layout:fixed" border="0"> <tr id="mainDiv"> <th width="350px">Details</th> <th width="100px">2008</th> <th width="100px">2009</th> <th width="100px">2010</th> <th width="100px">2011</th> <th width="100px">2012</th> <th width="100px">Total</th> </tr> <tr id="even"> <th name="tc6">Maintenance</th> <th><input type="text" name="tc1" id="1" OnPropertyChange ="y1total(this,'total1');"/></th> <th><input type="text" name="tc2" id="1" OnPropertyChange ="y1total(this,'total2');"/></th> <th><input type="text" name="tc3" id="1" OnPropertyChange ="y1total(this,'total3');"/></th> <th><input type="text" name="tc4" id="1" OnPropertyChange ="y1total(this,'total4');"/></th> <th><input type="text" name="tc5" id="1" OnPropertyChange ="y1total(this,'total5');"/></th> <th><input type="text" name="tc7" id="1"/></th> </tr> <tr id="even"> <th name="tc6">Support</th> <th><input type="text" name="tc1" id="2" OnPropertyChange ="y1total(this,'total1');" /></th> <th><input type="text" name="tc2" id="2" OnPropertyChange ="y1total(this,'total2');"/></th> <th><input type="text" name="tc3" id="2" OnPropertyChange ="y1total(this,'total3');"/></th> <th><input type="text" name="tc4" id="2" OnPropertyChange ="y1total(this,'total4');"/></th> <th><input type="text" name="tc5" id="2" OnPropertyChange ="y1total(this,'total5');"/></th> <th><input type="text" name="tc7" id="2"/></th> </tr> <tr id="even"> <th name="tc6">Savings</th> <th><input type="text" name="tc1" id="3" OnPropertyChange ="y1total(this,'total1');"/></th> <th><input type="text" name="tc2" id="3" OnPropertyChange ="y1total(this,'total2');"/></th> <th><input type="text" name="tc3" id="3" OnPropertyChange ="y1total(this,'total3');"/></th> <th><input type="text" name="tc4" id="3" OnPropertyChange ="y1total(this,'total4');"/></th> <th><input type="text" name="tc5" id="3" OnPropertyChange ="y1total(this,'total5');"/></th> <th><input type="text" name="tc7" id="3"/></th> </tr> <tr id="even"> <th>Others - List</th> </tr> <tr id="even"> <th><input type="text" name="tc6" /></th> <th><input type="text" name="tc1" id="5" OnPropertyChange ="y1total( this,'total1' );"/></th> <th><input type="text" name="tc2" id="5" OnPropertyChange ="y1total(this,'total2');"/></th> <th><input type="text" name="tc3" id="5" OnPropertyChange ="y1total(this,'total3');"/></th> <th><input type="text" name="tc4" id="5" OnPropertyChange ="y1total(this,'total4');"/></th> <th><input type="text" name="tc5" id="5" OnPropertyChange ="y1total(this,'total5');"/></th> <th><input type="text" name="tc7" id="5"/></th> </tr> <tr id="even"> <th><input type="text" name="tc6" /></th> <th><input type="text" name="tc1" id="6" OnPropertyChange ="y1total(this,'total1');" /></th> <th><input type="text" name="tc2" id="6" OnPropertyChange ="y1total(this,'total2');"/></th> <th><input type="text" name="tc3" id="6" OnPropertyChange ="y1total(this,'total3');"/></th> <th><input type="text" name="tc4" id="6" OnPropertyChange ="y1total(this,'total4');"/></th> <th><input type="text" name="tc5" id="6" OnPropertyChange ="y1total(this,'total5');"/></th> <th><input type="text" name="tc7" id="6"/></th> </tr> <tr id="even"> <th><input type="text" name="tc6" /></th> <th><input type="text" name="tc1" id="7" OnPropertyChange ="y1total(this,'total1');"/></th> <th><input type="text" name="tc2" id="7" OnPropertyChange ="y1total(this,'total2');"/></th> <th><input type="text" name="tc3" id="7" OnPropertyChange ="y1total(this,'total3');"/></th> <th><input type="text" name="tc4" id="7" OnPropertyChange ="y1total(this,'total4');"/></th> <th><input type="text" name="tc5" id="7" OnPropertyChange ="y1total(this,'total5');"/></th> <th><input type="text" name="tc7" id="7"/></th> </tr> </table> <input type="button" value="Add Row" onClick="addRow();" /> <table class="grid" id="tblGrid2" style="table-layout:fixed" border="0"> <tr id="mainDiv"> <th width="350px"></th> <th width="100px"></th> <th width="100px"></th> <th width="100px"></th> <th width="100px"></th> <th width="100px"></th> </tr> <tr id="even"> <th>Total Savings - Gross</th> <th><input type="text" name="total1" /></th> <th><input type="text" name="total2" /></th> <th><input type="text" name="total3" /></th> <th><input type="text" name="total4" /></th> <th><input type="text" name="total5" /></th> </tr> <tr id="even"> <th>Total Savings - Net</th> <th><input type="text" name="TotalSaveingst1" /></th> <th><input type="text" name="TotalSaveingst2" /></th> <th><input type="text" name="TotalSaveingst3" /></th> <th><input type="text" name="TotalSaveingst4" /></th> <th><input type="text" name="TotalSaveingst5" /></th> </tr> <tr id="even"> <th>Total Cost Avoidance</th> <th><input type="text" name="TotalCost1" /></th> <th><input type="text" name="TotalCost2" /></th> <th><input type="text" name="TotalCost3" /></th> <th><input type="text" name="TotalCost4" /></th> <th><input type="text" name="TotalCost5" /></th> </tr></table> </body></html>
|
|
|
|
 |
|
 |
Can you uou provide the code for saving the data into the database.
udaikiran@gmail.com
Uday
|
|
|
|
 |
|
 |
Hi All, I had an Add page and Edit page in my application. In add page i am having add row and delete operation as same as edit. In add page i am able to add and delete the selected row. after adding i am going to save these in to DB. But , in edit page i am fetching the records which has been added in the add page. for this time the delete operation is not working properly. It is deleting in correct row Plz help me if anybody knows Thanks
|
|
|
|
 |
|
 |
Hai, It was a good code very useful to me , i hav aquestion anyone know to add the data entered into a DB Table
regards Vinod
|
|
|
|
 |
|
 |
Hey tried your code and found very useful in my work. I have stuck at a point where i have to catch the non-deleted rows values during post back and how do i keep track of the rows and their idex when deleted. Please help.
|
|
|
|
 |
|
 |
Hi, I have tried your code , its cool. but I want to use this code and trying to add data into database. can you please explain me how can i do this?
Prashesh Kachhia Software Developer
|
|
|
|
 |
|
 |
The below article demonstrates how a table can be created dynamically and also deleting particular rows of a table dynamically.
There are 3 input box in the page through which data can be entered. There are 2 buttons namely, Add and Delete.
After entering data in the 3 input boxes, and on clicking on the Add Button, a dynamic table is created based on the data with each row having a checkbox associated with it.
On entering fresh inputs subsequent rows are added to the table.
On selecting the rows which needs to be deleted by selecting the checkboxes and on clicking the delete button, the selected rows get deleted.
The code for this is given below:
<html> <head> <script language="javascript" type="text/javascript">
function CmdAdd_onclick() {
var newTable,startTag,endTag;
//Creating a new table
startTag="<TABLE id='mainTable'><TBODY><TR><TD></TD><TD style=\"WIDTH: 120px\">ID</TD><TD style=\"WIDTH: 120px\">User Name</TD><TD style=\"WIDTH: 120px\">Project</TD></TR>"
endTag="</TBODY></TABLE>"
newTable=startTag;
var trContents;
//Get the row contents
trContents=document.body.getElementsByTagName('TR');
if(trContents.length>1)
{ for(i=1;i<trContents.length;i++) { if(trContents(i).innerHTML)
{
// Add previous rows newTable+="<TR>"; newTable+=trContents(i).innerHTML; newTable+="</TR>";
}
}
}
//Add the Latest row newTable+='<TR><TD><Input type="checkbox" name="chk"/></TD><TD style=\"WIDTH: 120px\" >' + document.getElementById('id').value +'</TD>'; newTable+='<TD style=\"WIDTH: 120px\" >' + document.getElementById('username').value +'</TD>'; newTable+='<TD style=\"WIDTH: 120px\" >' + document.getElementById('project').value +'</TD></TR>'; newTable+=endTag;
//Update the Previous Table With New Table. document.getElementById('tableDiv').innerHTML=newTable; }
function CmdDel_onclick() { var a = document.getElementsByName("chk"); var tbl = document.getElementById('mainTable'); var len=a.length; var i; var val=0; for(i=0;i<a.length;i++) { if(a[i].checked) { val=val+1; if(len==1) { tbl.deleteRow(val); } else { tbl.deleteRow(i+1); i=i-1; val=val-1; } } } } </script>
</head> <body>
<form id="form1" runat="server"> <div> <br>
<label>ID:</label> <input id="id" type="text" /><br>
<label>User Name:</label> <input id="username" type="text" /><br>
<label>Project:</label> <input id="project" type="text" />
<center> <input id="CmdAdd" type="button" value="Add" onclick="return CmdAdd_onclick()" /> <input id="CmdDel" type="button" value="Delete" onclick="return CmdDel_onclick()" /> </center>
</div>
<div id="tableDiv" style="text-align:center" > <table id="mainTable">
</table> </div>
</form> </body> </html>
|
|
|
|
 |
|
 |
Thank you... saved me a lot of trouble. Excellent article!
|
|
|
|
 |
|
 |
Dear Author,
The following code is working properly in firefox. But, it doesn't work with Microsoft's iexplore.  What should I do to work with iexplore. I would like to <b>add new row to inner table</b>.
Best Regards, greeting
<html > <head> <title>Untitled Page</title> </head> <script language="javascript">
function AddRow() { var tb2 = document.getElementById("mytable1"); var rowrow = document.createElement("TR"); var cellcell = document.createElement("TD"); cellcell.innerHTML = "New Row"; rowrow.appendChild(cellcell); tb2.appendChild(rowrow); }
</script> <body> <table id="table1" border=2px> <thead> <tr> <td>Col1</td> <td>Col2</td> <td>Col3</td>
</tr> </thead> <tbody id="tblbd"> <tr> <td>ColAAA1</td> <td>ColBBB2</td> <td>ColCCC3</td> </tr> <tr> <td> <table id="mytable1" border=1> <tr><td>Another Table</td></tr> </table>
</td> </tr> </tbody> </table> <input type=button value="AddRow" onClick="AddRow()" ID="Button1" NAME="Button1"/> </body> </html>
|
|
|
|
 |
|
 |
Your code in IE wasn't working because you need to append the rows to the tbody element, otherwise IE will add it to the table but outside of tbody, so the rows will not be rendered. Though you do not have a tbody specified for mytable1, the DOM does include a tbody. You can check it by doing an alert on innerHTML of mytable1.
I modified your code (see below). It works in IE now, but I am not sure if it works in FireFox - I do not work with Ffox as none of my client uses it. If it still does not work in both browsers, then you may have to detect the browser type and call separate function for ie and ff.
<html> <head>
<script language="javascript"> function addRow() { var tableBodyInner = document.getElementById("tBodyInner");
var rowrow = document.createElement("TR");
var cellcell = document.createElement("TD");
cellcell.innerHTML = "New Row";
rowrow.appendChild(cellcell);
tableBodyInner.appendChild(rowrow);
} </script>
</head>
<body>
<table id="tableouter"> <tbody> <tr> <td> <table id="tableinner" border="1"> <tbody id="tBodyInner"> <tr><td>cell</td></tr> </tbody> </table> </td> </tr> <tbody> </table>
<input type=button value="add row" onclick="addRow();"/> </body> </html>
eeraj http://www.interviewboard.com .NET, SQL, Oracle, ASP.NET interview questions and answers
|
|
|
|
 |
|
 |
Do you know of a way to do the same in ASP.NET?
I have tried: -------------- Dim tRow As New TableRow() Dim tCell As New TableCell() Dim tBox As New TextBox() tBox.EnableViewState = False
tBox.Text = "Test" tCell.Controls.Add(tBox) tRow.Cells.Add(tCell)
Table1.Rows.Add(tRow) -------------- This works fine, but it only adds the row, cell, and text box once. If you execute the code it overwrites the previous entry and does not append to the previously added elements.
Thanks.
|
|
|
|
 |
|
 |
thought i'd post a demo of script I put together. Worx in IE and FireFox. http://www.coastworx.com/dynamic_table_dom.php
|
|
|
|
 |
|
 |
Very good article posted, but how can we send the rows added/delete to server side..how can we save the table data in DB? Can you pls post code for that,I have many controls/fields in each row to be saved.
Thanks, Ali Email:reachali@yahoo.com
A winner is not the one who never fails, but is the one who never quits!!
|
|
|
|
 |
|
 |
Thank you for your comments. I do have the code to do what you are looking for. I will try to post it in the next week.
|
|
|
|
 |
|
 |
Using an array to hold the rows added/deleted, then you can send them to server side.
Obvously, you should edit something in addRow function
example:
<script> var listAdd = []; function addRow() { var newRow = document.all("tblGrid").insertRow(); var oCell = newRow.insertCell(-1); var oText = document.createElement("input"); oText.type = "text"; // by default, the type of input element is text oText.name = "myText"; oCell.appendChild(oText);
// add to list listAdd[listAdd.length] = oText;
} </script>
Ok, now, you can go through listAdd and get any control/value that you want, then submit them to server side.
Hope it helps you.
-- modified at 11:53 Sunday 26th November, 2006
One of five.
|
|
|
|
 |
|
 |
Hi
your code is very simple and do not support FF. why? i think it is better to upgrade your code for support other browsers. world is not belong to microsoft.
|
|
|
|
 |
|
 |
Replace the "parentElement" by "parentNode" and it should work...
|
|
|
|
 |
|
 |
hi i am a newbie to Java Script , i changed the Element by Node , it removes the Rows but not adding New Rows then what is the solution to this , Thanks
|
|
|
|
 |
|
 |
The following works for me in FF and IE. Apparently you need to specify index when inserting rows and cells according to w3c.
function addRow() { var newRow = document.getElementById("tblGrid").insertRow(0); var oCell = newRow.insertCell(0); oCell.innerHTML = "<input type='text' name='t1'>"; oCell = newRow.insertCell(1); oCell.innerHTML = "<input type='text' name='t2'>"; oCell = newRow.insertCell(2); oCell.innerHTML = "<input type='text' name='t3'> <input type='button' value='Delete' onclick='removeRow(this);'/>"; } function removeRow(src) { var oRow = src.parentNode.parentNode; document.getElementById("tblGrid").deleteRow(oRow.rowIndex);
}
|
|
|
|
 |
|
 |
This is what i exactly wanted without being bogged down by style details.;)
not yet
|
|
|
|
 |