MERGE table cells using Js

Source: Internet
Author: User

Recently, when working on a project, you need to merge the cells in the GridView to display the same data into a row. A big brother in the company wrote this and pasted it to the bit for a look, if the company's Code cannot be obtained, you can understand it and write it by yourself.

TabObj indicates the table Id, that is, the Id of the GridView. cellIndex indicates the column number to be merged. beignRow indicates the number of rows from which the table is merged. Generally, 1 is used because 0 indicates the table header.

The js Code is as follows:

Function SpanGrid (tabObj, cellindex, beginRow ){
Var colIndex = cellindex;
Var rowBeginIndex = beginRow;
If (tabObj! = Null ){
Var I, j, m;

Var intSpan;
Var strTemp;
M = 0;
For (I = rowBeginIndex; I <tabObj. rows. length; I ++ ){
IntSpan = 1;
M ++;
StrTemp = tabObj. rows [I]. cells [colIndex]. innerText;
For (j = I + 1; j <tabObj. rows. length; j ++ ){
If (strTemp = tabObj. rows [j]. cells [colIndex]. innerText ){
IntSpan ++;
TabObj. rows [I]. cells [colIndex]. rowSpan = intSpan;
TabObj. rows [j]. cells [colIndex]. style. display = "none ";
}
Else {
Break;
}
}

}
I = j-1;
}
}

 

Of course, this Js Code has a small problem, that is, only one column can be merged, and multiple columns cannot be merged at the same time. I hope a friend can improve it so that it can merge multiple columns at the same time.

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.