JavaScript implementation dynamic add delete row method instance detail

  • 2020-06-22 23:53:29
  • OfStack

This article illustrated JavaScript's approach to dynamically adding and deleting rows. Share to everybody for everybody reference. The details are as follows:


<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title> increase Table line </title>
  </head>
<script language="javascript">// Example: obj = findObj("image1");
function findObj(theObj, theDoc){ 
var p, i, foundObj;
  if(!theDoc) theDoc = document; if( (p = theObj.indexOf("?")) > 0 && parent.frames.length) {  theDoc = parent.frames[theObj.substring(p+1)].document;  theObj = theObj.substring(0,p); } if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj]; for (i=0; !foundObj && i < theDoc.forms.length; i++)   foundObj = theDoc.forms[i][theObj]; for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)   foundObj = findObj(theObj,theDoc.layers[i].document); if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);  return foundObj;}
// add 1 Participants fill in the rows 
function AddSignRow(){ // Read the last 1 Row Numbers, stored in txtTRLastIndex The text box  
 var txtTRLastIndex = findObj("txtTRLastIndex",document);
 var rowID = parseInt(txtTRLastIndex.value);
 var signFrame = findObj("SignFrame",document);
 // Add the line 
 var newTR = signFrame.insertRow(signFrame.rows.length);
 newTR.id = "SignItem" + rowID;
 // Add columns : The serial number 
 var newNameTD=newTR.insertCell(0);
 // Add column content 
 newNameTD.innerHTML = newTR.rowIndex.toString();
 // Add columns : The name 
 var newNameTD=newTR.insertCell(1);
 // Add column content 
 newNameTD.innerHTML = "<input name='txtName" + rowID + "' id='txtName" + rowID + "' type='text' size='12' />";
 // Add columns : email 
 var newEmailTD=newTR.insertCell(2);
 // Add column content 
 newEmailTD.innerHTML = "<input name='txtEMail" + rowID + "' id='txtEmail" + rowID + "' type='text' size='20' />";
 // Add columns : The phone 
 var newTelTD=newTR.insertCell(3);
 // Add column content 
 newTelTD.innerHTML = "<input name='txtTel" + rowID + "' id='txtTel" + rowID + "' type='text' size='10' />";
 // Add columns : Mobile phone 
 var newMobileTD=newTR.insertCell(4);
 // Add column content 
 newMobileTD.innerHTML = "<input name='txtMobile" + rowID + "' id='txtMobile" + rowID + "' type='text' size='12' />";
 // Add columns : Company name 
 var newCompanyTD=newTR.insertCell(5);
 // Add column content 
 newCompanyTD.innerHTML = "<input name='txtCompany" + rowID + "' id='txtCompany" + rowID + "' type='text' size='20' />";
 
 // Add columns : The delete button 
 var newDeleteTD=newTR.insertCell(6);
 // Add column content 
 newDeleteTD.innerHTML = "<div align='center' style='width:40px'><a href='javascript:;' onclick=\"DeleteSignRow('SignItem" + rowID + "')\"> delete </a></div>";
 // I'm going to push the line number down 1 line 
 txtTRLastIndex.value = (rowID + 1).toString() ;
}
// Deletes the specified row 
function DeleteSignRow(rowid){
 var signFrame = findObj("SignFrame",document);
 var signItem = findObj(rowid,document);
 // Gets the row to be deleted Index
 var rowIndex = signItem.rowIndex;
 // Delete the specified Index The rows of 
 signFrame.deleteRow(rowIndex);
 // Rearrange the Numbers. If there is no number, here 1 Step is omitted 
 for(i=rowIndex;i<signFrame.rows.length;i++){
 signFrame.rows[i].cells[0].innerHTML = i.toString();
 }
}// Empty the list 
function ClearAllSign(){
 if(confirm(' Are you sure you want to empty all the participants? ')){
 var signFrame = findObj("SignFrame",document);
 var rowscount = signFrame.rows.length;
 // Loop delete row , From the last 1 Delete lines forward 
 for(i=rowscount - 1;i > 0; i--){
  signFrame.deleteRow(i);
 }
 // Reset the last line number to 1
 var txtTRLastIndex = findObj("txtTRLastIndex",document);
 txtTRLastIndex.value = "1";
 // Add in advance 1 line 
 AddSignRow();
 }
}
</script>
  <body>
     <div>
 <table width="613" border="0" cellpadding="2" cellspacing="1" id="SignFrame">
       <tr id="trHeader">
        <td width="27" bgcolor="#96E0E2"> The serial number </td>
        <td width="64" bgcolor="#96E0E2"> The user name </td>
        <td width="98" bgcolor="#96E0E2"> email </td>
        <td width="92" bgcolor="#96E0E2"> Fixed telephone </td>
        <td width="86" bgcolor="#96E0E2"> Mobile phone </td>
        <td width="153" bgcolor="#96E0E2"> The name of the company </td>
        <td width="57" align="center" bgcolor="#96E0E2">&nbsp;</td>
       </tr>
    </table>
  </div>
  <div>
    <input type="button" name="Submit" value=" Add participant " onclick="AddSignRow()" />
   <input type="button" name="Submit2" value=" empty " onclick="ClearAllSign()" />
   <input name='txtTRLastIndex' type='hidden' id='txtTRLastIndex' value="1" />
  </div>
  </body>
</html>

JavaScript implements dynamically adding or deleting table rows


<SCRIPT LANGUAGE="JScript"> 
function numberCells() 
{
  var count=0;
  for (i=0; i < document.all.mytable.rows.length; i++)
  {
    for (j=0; j < document.all.mytable.rows(i).cells.length; j++) 
    {
      document.all.mytable.rows(i).cells(j).innerText = count;
      count++;
    }
  }
}
function tb_addnew()
{
  var ls_t=document.all("mytable")
  maxcell=ls_t.rows(0).cells.length;
  mynewrow = ls_t.insertRow();
  for(i=0;i<maxcell;i++)
  {
    mynewcell=mynewrow.insertCell();
    mynewcell.innerText="a"+i;
  }
}
function tb_delete()
{
  var ls_t=document.all("mytable");
  maxcell=ls_t.rows.length;
  if(maxcell > 1)
  {
    ls_t.deleteRow() ;
  }
}
</SCRIPT>
<BODY onload="numberCells()">
<TABLE id=mytable border=1>
<TR><TH> </TH><TH> </TH><TH> </TH><TH> </TH></TR>
<TR><TD> </TD><TD> </TD><TD> </TD><TD> </TD></TR>
<TR><TD> </TD><TD> </TD><TD> </TD><TD> </TD></TR>
</TABLE>
<input type=button value="Ins Row" onclick="tb_addnew()">
<input type=button value="Del Row" onclick="tb_delete()" >

Hopefully, this article has been helpful in your javascript programming.


Related articles: