javascript merges table cell instance code

  • 2020-11-18 06:05:33
  • OfStack

This article introduces a piece of code from the network example, can merge cells, and share with you below 1, hope to give the friends in need more or less 1 definite help.
Code examples are as follows:


<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<title> Table cell merge code </title>
<script type="text/javascript"> 
function autoRowSpan(tb,row,col){ 
 var lastValue=""; 
 var value=""; 
 var pos=1; 
 for(var i=row;i<tb.rows.length;i++){ 
 value=tb.rows[i].cells[col].innerText; 
 if(lastValue==value){ 
  tb.rows[i].deleteCell(col); 
  tb.rows[i-pos].cells[col].rowSpan=tb.rows[i-pos].cells[col].rowSpan+1; 
  pos++; 
 }
 else{ 
  lastValue=value; 
  pos=1; 
 } 
 } 
} 
window.onload=function(){
 var tb=document.getElementById("tb");
 autoRowSpan(tb,0,0)
}
</script>
</head>
<body>
<table id="tb" border="1">
 <thead>
 <tr >
  <td> countries </td>
  <td> region </td>
 </tr>
 </thead>
 <tr>
 <td> China </td>
 <td> henan </td>
 </tr>
 <tr>
 <td> China </td>
 <td>4 sichuan </td>
 </tr>
 <tr>
 <td> China </td>
 <td> Beijing </td>
 </tr>
 <tr>
 <td> The United States </td>
 <td> New York, </td>
 </tr>
 <tr>
 <td> The United States </td>
 <td> Los Angeles, </td>
 </tr>
 <tr>
 <td> Britain </td>
 <td> London </td>
 </tr>
</table>
</body>
</html>

I would like to share with you a paragraph:


<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
<title> Combined form </title> 
<script> 
/////////////////////////////////////////////// 
// Function: Merge tables  
// Parameters: tb - Tables that need to be merged ID 
// Parameters: colLength -- The first columns need to be merged, for example,  
// If you want to merge the first two columns, the subsequent data columns ignore the merge, colLength Should be 2 
// The default is to merge all columns  
//data:2011.11.06 
/////////////////////////////////////////////// 
function uniteTable(tb,colLength){ 
// Check that the form is organized  
if(!checkTable(tb)) return; 
var i=0; 
var j=0; 
var rowCount=tb.rows.length; // The number of rows  
var colCount=tb.rows[0].cells.length; // The number of columns  
var obj1=null; 
var obj2=null; 
// Give each cell a name  
for(i=0;i<rowCount;i++){ 
for(j=0;j<colCount;j++){ 
tb.rows[i].cells[j].id="tb__" + i.toString() + "_" + j.toString(); 
} 
} 
// Check the merge column by column  
for(i=0;i<colCount;i++){ 
if(i==colLength) return; 
obj1=document.getElementById("tb__0_"+i.toString()) 
for(j=1;j<rowCount;j++){ 
obj2=document.getElementById("tb__"+j.toString()+"_"+i.toString()); 
if(obj1.innerText==obj2.innerText){ 
obj1.rowSpan++; 
obj2.parentNode.removeChild(obj2); 
}else{ 
obj1=document.getElementById("tb__"+j.toString()+"_"+i.toString()); 
} 
} 
} 
} 

///////////////////////////////////////// 
// Function: Check if the form is organized  
// Parameters: tb -- Forms to check ID 
//data: 2011.11.06
///////////////////////////////////////// 
function checkTable(tb){ 
if(tb.rows.length==0) return false; 
if(tb.rows[0].cells.length==0) return false; 
for(var i=0;i<tb.rows.length;i++){ 
if(tb.rows[0].cells.length!=tb.rows[i].cells.length) return false; 
} 
return true; 
} 
</script> 
</head> 
<body> 
<table width="400" border="1" id="table1"> 
<tr> 
<td>a</td> 
<td>for</td> 
<td>100</td> 
<td>200</td>
<td>1</td> 
</tr> 
<tr> 
<td>a</td> 
<td>for</td> 
<td>100</td> 
<td>300</td> 
<td>2</td> 
</tr> 
<tr> 
<td>a</td> 
<td>if</td> 
<td>100</td> 
<td>200</td> 
<td>3</td> 
</tr> 
<tr> 
<td>a</td> 
<td>if</td> 
<td>300</td> 
<td>230</td> 
<td>4</td> 
</tr> 
<tr> 
<td>a</td> 
<td>if</td> 
<td>320</td> 
<td>230</td> 
<td>5</td> 
</tr> 
</table> 
<br><input type="button" value=" Combined form " onClick="uniteTable(table1,4)"> 
</body> 
</html>

I hope this article has been helpful in learning javascript programming.


Related articles: