JS merge Cells

Source: Internet
Author: User
This function can be used directly to merge the situations where the upper and lower cells have the same value. For example, I have written comments. What you can understand is probably different from what you want, but you can make modifications on this basis. FunctionrowSpan () {// obtain the table object varta... SyntaxHighlighter. all (). This function can be used directly to merge the rows with the same upper and lower cell values. Effect:

 


I have written comments. You can understand the comments, but they may not meet your requirements. But you can modify the comments on this basis.

Function rowSpan (){
// Obtain the table object
Var tab = document. getElementById ("tab ");
If (! Tab ){
Return;
}
// Exclude the title line from the second line
Var startRow = 1;
// Record the content of td in the table cyclically to determine whether the value in td has changed
Var tdTempV = "";
// If the td value is the same, add 1 to the variable; otherwise, add the temporary variable to the set.
Var rowCount = 1;
// Obtain the set www.2cto.com with the same number of rows
Var totalcount = new Array ();

For (var I = 1; I // Obtain the td value first.
Var tdText = tab. rows [I]. cells [0]. innerText;
// If this is the first cycle, directly continue;
If (I = startRow ){
TdTempV = tdText;
Continue;
}
// If the current value is the same as the source value, add 1 to the temporary quantity; otherwise, add it to the set.
If (tdTempV = tdText ){
RowCount ++;
} Else {
Totalcount. push (rowCount );
TdTempV = tdText;
RowCount = 1;
}
 
// Determine whether it is the last cycle. If it is the last one, the current quantity is directly stored in the set.
If (I = tab. rows. length-1 ){
Totalcount. push (rowCount );
}
}
// Temporary variable, judge whether it is the same as a value in the array during recycling
Var tNum = 0;
// Note that this cycle is reversed.
For (var I = tab. rows. length-1; I >= startRow; I --){
// Temporary variables, store td
Var tTd = tab. rows [I]. cells [0];
TNum ++;
// If the tNum value is the same as the last value in the array, it can be determined that the same td has ended [only one of them]
If (tNum = totalcount [totalcount. length-1]) {
// Add the rowSpan attribute to the current td
TTd. setAttribute ("rowSpan", totalcount [totalcount. length-1]);
// Bring up the last element of the array
Totalcount. pop ();
TNum = 0;
} Else {
// Delete the current td
Tab. rows [I]. removeChild (tTd );
}
}
}

 

From zhangyuhong's column
 

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.