The merge logic is as follows:
1. Find the cells to be merged Based on the cell content or cell attributes.
2. Modify the colspan value of the starting cell.
3. Delete unnecessary Cells
The cell merge code is as follows:
/************************************* Table cells merge *************************************** ********
Merge cells with the same data in a specified table. Table: Table object colinx: Index columns of the columns to be merged: Total number of columns of the table
Opts: {columns: Total number of columns, skipnull: whether to ignore the merge mergeattrname of empty cells}
**************************************** **************************************** **************/
Mergetalbevcells = function (table, colinx, opts ){
VaR option = {skipnull: false };
$. Extend (option, opts );
VaR Cols = option. Columns | table. Rows [0]. cells. length;
VaR colarr = [];
VaR rows = table. Rows. length;
VaR n = 1;
For (VAR I = 0; I <rows; I ++ ){
VaR currtxt = '';
If (option. mergeattrname ){
Currtxt = table. Rows [I]. cells [colinx-(Cols-Table. Rows [I]. cells. Length)]. getattribute (option. mergeattrname );
} Else {
Currtxt = table. Rows [I]. cells [colinx-(Cols-Table. Rows [I]. cells. Length)]. innertext;
}
VaR nexttxt;
If (I <rows-1 ){
If (option. mergeattrname ){
Nexttxt = table. Rows [I + 1]. cells [colinx-(Cols-Table. Rows [I + 1]. cells. Length)]. getattribute (option. mergeattrname );
} Else {
Nexttxt = table. Rows [I + 1]. cells [colinx-(Cols-Table. Rows [I + 1]. cells. Length)]. innertext;
}
} Else {
Colarr [colarr. Length] = {rowinx :( (I + 1)-N), rowspan: n };
}
If (currtxt! = Nexttxt ){
Colarr [colarr. Length] = {rowinx :( I-n + 1), rowspan: n };
N = 0;
} Else if (currtxt = "" & nexttxt = ""){
// Ignore null characters
If (option. skipnull ){
Colarr [colarr. Length] = {rowinx: 0, rowspan: 1 };
N = 0;
}
}
N ++;
}
// Merge cells with the same data
For (VAR I = 0; I <colarr. length; I ++ ){
Table. rows [colarr [I]. rowinx]. cells [colinx-(Cols-table. rows [colarr [I]. rowinx]. cells. length)]. rowspan = colarr [I]. rowspan;
For (VAR j = 1; j <colarr [I]. rowspan; j ++ ){
Table. rows [colarr [I]. rowinx * 1 + J]. deletecell (colinx-(Cols-table. rows [colarr [I]. rowinx * 1 + J]. cells. length ));
}
}
}
The merge restoration code is as follows:
/******* Restore and merge *******/
VaR restoremergetable = function (table, colinx, opts ){
VaR option = {skipnull: false };
$. Extend (option, opts );
VaR Cols = option. Columns | table. Rows [0]. cells. length;
VaR colarr = [];
VaR rows = table. Rows. length;
VaR n = 1;
For (VAR I = 0; I <rows; I ++ ){
VaR cell = getcellbycolinx (table. Rows [I], colinx );
If (! Cell) continue;
VaR rowspan = cell. getattribute ("rowspan ");
If (rowspan <2) continue;
For (VAR rI = 1; RI <rowspan; ri ++ ){
Copycell (table. Rows [I + Ri]. insertcell (0), cell, _ this. Option. userdatas );
}
Cell. rowspan = 1;
}
/************** Copy a cell **************/
Function copycell (res_cell, target_cell, attributes ){
Res_cell.innerhtml = target_cell.innerhtml;
If (attributes ){
For (VAR I = 0, Len = attributes. length; I <Len; I ++ ){
Res_cell.setattribute (attributes [I], target_cell.getattribute (attributes [I]);
}
}
Res_cell.setattribute ("columnid", target_cell.getattribute ("columnid "));
}
}