After obtaining a cell object, you can set colspan and rowspan to merge cells. I have been using deletecell to delete the merged cells. When merging multiple times, it is difficult to locate cellindex and rowindex, which will lead to confusion in the merged table. After many explorations, the final solution is not to directly Delete the merged cells, but to set style. Display = "NONE.
The code for merging cells in the several flying OA test is as follows. If you are interested, you can complete the code.
<! ----- Code of the dataworks OA experiment: Test on IE7 ---->
<Table border = "1" width = "500" Height = "200" id = "TID">
<Tr>
<TD> 0-0 </TD>
<TD> 0-1 </TD>
<TD> 0-2 </TD>
<TD> 0-3 </TD>
</Tr>
<Tr>
<TD> 1-0 </TD>
<TD> 1-1 </TD>
<TD> 1-2 </TD>
<TD> 1-3 </TD>
</Tr>
<Tr>
<TD> 2-0 </TD>
<TD> 2-1 </TD>
<TD> 2-2 </TD>
<TD> 2-3 </TD>
</Tr>
<Tr>
<TD> 3-0 </TD>
<TD> 3-1 </TD>
<TD> 3-2 </TD>
<TD> 3-3 </TD>
</Tr>
</Table>
Current cell: <input type = "text" value = "0" id = "RID"> row <input type = "text" value = "1" id = "CID"> Column
<Input type = "button" value = "merge down" onclick = "mergedown ()">
<Input type = "button" value = "merge to the right" onclick = "mergeright ()">
<Br>
Content of the current row: <input type = "text" value = "" id = "vid"> <input type = "button" value = "current cell content" onclick = "displayvalue ();">
<Script language = "JavaScript">
Function mergedown (){
VaR otable = Document. getelementbyid ("TID ");
VaR irow = parseint (document. getelementbyid ("RID"). value );
VaR icell = parseint (document. getelementbyid ("CID"). value );
VaR orow = otable. Rows [irow];
VaR ocell = NULL;
// Shufei OA Note: Add judgment to avoid null
If (orow! = NULL ){
Ocell = orow. cells [icell];
}
If (ocell = NULL ){
Return;
}
VaR V = 0;
VaR irow2 = irow;
VaR irowspan = ocell. rowspan;
While (true ){
Irow2 = irow2 + 1;
If (irow2> otable. Rows. Length ){
Break;
}
VaR orow2 = otable. Rows [irow2];
VaR ocell2 = NULL;
If (orow2! = NULL ){
Ocell2 = orow2.cells [icell];
If (ocell2 = NULL ){
Break;
}
// Shufei OA comments: Add judgments to avoid asymmetric merging Methods
If (ocell2.colspan! = Ocell. colspan ){
Alert ("cannot be merged ");
Break;
}
If (ocell2.style. Display = "NONE" & ocell2.style. colspan = "yes "){
Alert ("cannot be merged ");
Break;
}
If (ocell2.style. display! = "NONE "){
Ocell2.style. Display = "NONE ";
// Shufei OA Note: Customize a style to prevent hidden cells from being merged multiple times.
Ocell2.style. rowspan = "yes ";
Irowspan + = ocell2.rowspan;
Ocell. rowspan = irowspan;
Break;
} Else {
// Merge again
}
} Else {
Break;
}
}
}
Function mergeright (){
VaR otable = Document. getelementbyid ("TID ");
VaR irow = parseint (document. getelementbyid ("RID"). value );
VaR icell = parseint (document. getelementbyid ("CID"). value );
VaR orow = otable. Rows [irow];
VaR ocell = NULL;
If (orow! = NULL ){
Ocell = orow. cells [icell];
}
If (ocell = NULL ){
Return;
}
VaR V = 0;
VaR icell2 = icell;
VaR icellspan = ocell. colspan ;//
While (true ){
Icell2 = icell2 + 1;
If (icell2> orow. Length ){
Break;
}
VaR ocell2 = NULL;
Ocell2 = orow. cells [icell2];
If (ocell2 = NULL ){
Break;
}
// If (ocell2.rowspan! = Ocell. rowspan | ocell2.style. Display = "NONE "){
If (ocell2.rowspan! = Ocell. rowspan ){
Alert ("cannot be merged ");
Break;
}
If (ocell2.style. Display = "NONE" & ocell2.style. rowspan = "yes "){
Alert ("cannot be merged ");
Break;
}
If (ocell2.style. display! = "NONE "){
Icellspan + = ocell2.colspan;
Ocell2.style. Display = "NONE ";
Ocell2.style. colspan = "yes"; // custom attributes
Ocell. colspan = icellspan;
Break;
}
}
}
// Content of the current row
Function displayvalue (){
VaR otable = Document. getelementbyid ("TID ");
VaR irow = Document. getelementbyid ("RID"). value;
VaR icell = Document. getelementbyid ("CID"). value;
VaR orow = otable. Rows [irow];
VaR ocell = NULL;
If (orow! = NULL ){
Ocell = orow. cells [icell];
}
If (ocell! = NULL ){
Document. getelementbyid ("vid"). value = ocell. innertext;
}
}
</SCRIPT>