Vertical merge and merge restoration of JavaScript table cells

Source: Internet
Author: User

 

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 "));
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.