Automatically merged cells with duplicate values in js

Source: Internet
Author: User

 

Function uniteTable (tableId, colLength ){

// ColLength -- start with column 1 of the cells to be merged

Var tb = document. getElementById (tableId );

Tb. style. display = '';

Var I = 0;

Var j = 0;

Var rowCount = tb. rows. length; // number of rows

Var colCount = tb. rows [0]. cells. length; // Number of Columns

Var obj1 = null;

Var obj2 = null;

// Name each cell

For (I = 0; I <rowCount; I ++ ){

For (j = 0; j <colCount; j ++ ){

Tb. rows [I]. cells [j]. id = "tb _" + I. toString () + "_" + j. toString ();

}

}

// Merge rows

For (I = 0; I <colCount; I ++ ){

If (I = colLength) break;

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 ());

}

}

}

// Merge Columns

For (I = 0; I <rowCount; I ++ ){

ColCount = tb. rows [I]. cells. length;

Obj1 = document. getElementById (tb. rows [I]. cells [0]. id );

For (j = 1; j <colCount; j ++ ){

If (j> = colLength) break;

If (obj1.colSpan> = colLength) break;

 

Obj2 = document. getElementById (tb. rows [I]. cells [j]. id );

If (obj1.innerText = obj2.innerText ){

Obj1.colSpan ++;

Obj2.parentNode. removeChild (obj2 );

J = j-1;

}

Else {

Obj1 = obj2;

J = j + obj1.rowSpan;

}

}

}

}

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.