JavaScript dynamically operates on a table instance of of to add and delete rows columns and cells

  • 2020-03-30 00:00:59
  • OfStack


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>js Dynamic operation table </title>
<script language="javascript">
function init(){
_table=document.getElementById("table");
_table.border="1px";
_table.width="800px";
for(var i=1;i<6;i++){
var row=document.createElement("tr");
row.id=i;
for(var j=1;j<6;j++){
var cell=document.createElement("td");
cell.id=i+"/"+j;
cell.appendChild(document.createTextNode(" The first "+cell.id+" column "));
row.appendChild(cell);
}
document.getElementById("newbody").appendChild(row);
}
}
function rebulid(){
var beginRow=document.getElementById("beginRow").value;
var endRow=document.getElementById("endRow").value;
var beginCol=document.getElementById("beginCol").value;
var endCol=document.getElementById("endCol").value;
var tempCol=beginRow+"/"+beginCol;
alert(tempCol);
var td=document.getElementById(tempCol);
for(var x=beginRow;x<=endRow;x++){
for(var i=beginCol;i<=endCol;i++){
if(x==beginRow){
document.getElementById("table").rows[x].deleteCell(i+1);
}
else{
document.getElementById("table").rows[x].deleteCell(i);
}
}
}
td.rowSpan=(endRow-beginRow)+1;
}

function addRow(){
var length=document.getElementById("table").rows.length;

var tr=document.createElement("tr");
tr.id=length+1;
var td=document.createElement("td");
for(i=1;i<4;i++){
td.id=tr.id+"/"+i;
td.appendChild(document.createTextNode(" The first "+td.id+" column "));
tr.appendChild(td);
}
document.getElementById("newbody").appendChild(tr);
}
function addRow_withInsert(){
var row=document.getElementById("table").insertRow(document.getElementById("table").rows.length);
var rowCount=document.getElementById("table").rows.length;
var countCell=document.getElementById("table").rows.item(0).cells.length;
for(var i=0;i<countCell;i++){
var cell=row.insertCell(i);
cell.innerHTML=" new "+(rowCount)+"/"+(i+1)+" column ";
cell.id=(rowCount)+"/"+(i+1);
}
}

function removeRow(){
document.getElementById("newbody").deleteRow(document.getElementById(document.getElementById("table").rows.length).rowIndex);
}

function addCell(){

for(var i=0;i<document.getElementById("table").rows.length;i++){
var cell=document.getElementById("table").rows[i].insertCell(2);
cell.innerHTML=" The first "+(i+1)+"/"+3+" column ";
}
}

function removeCell(){
for(var i=0;i<document.getElementById("table").rows.length;i++){
document.getElementById("table").rows[i].deleteCell(0);
}
}
</script>
</head>
<body onLoad="init();">
<table  id="table" align="center">
<tbody id="newbody"></tbody>
</table>
<div>
<table width="800px" border="1px" align="center">
<tr><td align="center"><input type="button" id="addRow" name="addRow" onClick="addRow();" value=" Add the line "/></td><td align="center"><input type="button" id="delRow" name="delRow" onClick="removeRow();" value=" Delete rows "/></td></tr>
  <tr><td align="center"><input type="button" id="delCell" name="delCell"  onClick="removeCell();" value=" Delete the column "/></td><td align="center"><input type="button" id="addCell" name="addCell" onClick=" addCell();" value=" Add columns "/></td></tr>
 <tr><td align="center" colspan="2"><input type="button" id="addRows" name="addRows"  onClick="addRow_withInsert();" value=" Add the line "/></td></tr>
</table>
</div>
<div>
<table width="800px" border="1px" align="center">
<tr><td> From the first <input type="text" id="beginRow" name="beginRow"  value=""/> Row to <input type="text"  name="endRow"  id="endRow" value=""/> line </td><td rowspan="2"  id="test"><input type="button" name="hebing" id="hebing" value=" merge " onClick="rebulid();"/></td></tr>
  <tr><td> From the first <input type="text" name="beginCol" id="beginCol" value=""/> Column to <input type="text" name="endCol" id="endCol" value=""/> column </td></tr>
</table>
</div>
</body>
</html> 

Related articles: