Another dynamic control of the table, with JavaScript dynamic table rows, table columns, and can also dynamically delete these rows, lines, and so on, run the code, click the corresponding function button, you can achieve the corresponding table operation function.
1.jsp
<table id= "Viewtabs" >
<thead>
<tr>
<th> product name </th>
<th> No. < /th>
<th> Quantity </th>
<th> weight </th>
<th> operation </th>
</tr>
</thead>
<tbody>
<tr>
<td><input name= "ProductName" type= "text" ></td>
<td><input name= "ProductNumber" type= "text" ></td>
<td>< Input name= "Quantity" type= "text" ></td>
<td><input name= "Weight" type= "text" ></td>
<td></td>
</tr>
</tbody>
</table>
<button type= " Button "onclick=" addtable (); "style=" margin-left:750px; " > Add Rows </button>
2.js
Add Row function addtable () {var Tab=document.getelementbyid ("Viewtabs");//Get table var colsnum=tab.rows.item (0). Cells.length;
Number of columns in table//table current number of rows Var Num=document.getelementbyid ("Viewtabs"). Rows.length;
var rownum=num;
Tab.insertrow (rownum); for (var i=0;i<4; i++) {Tab.rows[rownum].insertcell (i);//Insert Column if (i==0) {tab.rows[rownum].cells[i].innerhtml= ' <
Input name= "ProductName" type= "text"/> '; }else if (i==1) {tab.rows[rownum].cells[i].innerhtml= ' <input name= ' productnumber ' type= ' text '/> ';} else if (i==2) {tab.rows[rownum].cells[i].innerhtml= ' <input name= ' quantity ' type= ' text '/> ';}
else{tab.rows[rownum].cells[i].innerhtml= ' <input name= ' weight ' type= ' text '/> ';}
Tab.rows[rownum].insertcell (i);
Tab.rows[rownum].cells[i].innerhtml= ' <a href= "#" onclick= "Delrow (This)" > Delete line </a> "; //Delete Row function delrow (obj) {var row=obj.parentnode; var row=obj.parentnode//tr while Row.tagName.toLowerCase ()!= "tr
") {Row=row.parentnode;} Row.parentNode.removeChild (Row); DeleteLine
The above is a small set to share the JavaScript implementation dynamic add Delete table row, I hope to help.