Use jquery to merge HTML table cells

Source: Internet
Author: User

Use jquery to merge cells in an HTML table:
I. js scripts

/*** Desc: merges adjacent cells of the same text in the specified table (table ID is table_id) specified column (number of columns is table_colnum) * @ table_id table ID: ID of the table to which cells are to be merged. For example, if you specify table id = "data" in HTML, this parameter should be # data * @ table_colnum: the column where the cells to be merged are located. refer to the nth-Child parameter in jquery. if it is a number, it starts from the first column on the left; "even" indicates an even number column; "odd" indicates an odd number series; "3N + 1" indicates that the number of columns is 1, 4, 7 ,...... * @ table_minrow?: Optional. It indicates the column with the smallest number of rows to be merged. If this parameter is omitted, it indicates that the column starts from row 0th (closed interval) * @ table_maxrow?: Optional. It indicates the column with the maximum number of rows to merge columns. If it is omitted, it indicates that the maximum number of rows is the last row of the table (Open interval) */function table_rowspan (table_id, table_colnum) {If (table_colnum = "even") {table_colnum = "2n";} else if (table_colnum = "odd") {table_colnum = "2n + 1 ";} else {table_colnum = "" + table_colnum;} var Cols = []; var all_row_num =$ (table_id + "Tr TD: Nth-Child (1 )"). length; var all_col_num = $ (table_id + "TR: Nth-Child (1 )"). children (). length; If (table_colnum.index Of ("N") =-1) {Cols [0] = table_colnum;} else {var n = 0; var A = table_colnum.substring (0, table_colnum.indexof ("N"); var B = table_colnum.substring (table_colnum.indexof ("N") + 1 ); // alert ("A =" + A + "B =" + (B = true); a =? Parseint (a): 1; B = B? Parseint (B): 0; // alert (B); While (A * n + B <= all_col_num) {Cols [N] = A * n + B; N ++ ;}} var table_minrow = arguments [2]? Arguments [2]: 0; var table_maxrow = arguments [3]? Arguments [3]: all_row_num + 1; var table_firsttd = ""; var table_currenttd = ""; var table_spannum = 0; For (var j = 0; j <cols. length; j ++) {$ (table_id + "Tr TD: Nth-child (" + Cols [J] + ")"). slice (table_minrow, table_maxrow ). each (function (I) {var table_col_obj = $ (this); If (table_col_obj.html ()! = "") {If (I = 0) {table_firsttd = $ (this); table_spannum = 1;} else {table_currenttd = $ (this); If (table_firsttd.text () ==table_currenttd.text () {table_spannum ++; table_currenttd.hide (); // remove (); trim ("rowspan", table_spannum);} else {table_firsttd = $ (this ); table_spannum = 1 ;}}}) ;}}/*** Desc: Merge the specified table (table_id is the table ID) and specify the row (table_rownum is the number of rows) adjacent cells of the same text * @ table_id Table ID: ID of the table to which cells are to be merged. If you specify table id = "data" in HTML, this parameter should be # data * @ table_rownum: the row of the cells to be merged. refer to the nth-Child parameter in jquery. if it is a number, it starts from the first column on the left; "even" indicates an even number of rows; "odd" indicates an odd number of rows; "3N + 1" indicates that the number of rows is 1, 4, 7 ,...... * @ table_mincolnum?: Optional. It indicates the smallest column in the row to be merged. If it is omitted, it indicates that the column starts from column 0th (closed interval) * @ table_maxcolnum?: Optional. It indicates the maximum column in the row to be merged. omitted indicates the maximum number of columns in the table (Open interval) */function table_colspan (table_id, table_rownum) {// If (table_maxcolnum = void 0) {table_maxcolnum = 0;} var table_mincolnum = arguments [2]? Arguments [2]: 0; var table_maxcolnum; var table_firsttd = ""; var table_currenttd = ""; var table_spannum = 0; $ (table_id + "TR: nth-child ("+ table_rownum + ")"). each (function (I) {table_row_obj = $ (this ). children (); table_maxcolnum = arguments [3]? Arguments [3]: table_row_obj.length; table_row_obj.slice (table_mincolnum, table_maxcolnum ). each (function (I) {if (I = 0) {table_firsttd = $ (this); table_spannum = 1;} else if (table_maxcolnum> 0) & (I> table_maxcolnum) {return "";} else {table_currenttd = $ (this); If (table_firsttd.text () = table_currenttd.text () {table_spannum ++; if (table_currenttd.is (": visible") {table_firsttd.width (parseint (partial () + parseint (table_currenttd.width ();} table_currenttd.hide (); // remove (); table_firsttd.attr ("colspan", table_spannum);} else {table_firsttd =$ (this); table_spannum = 1 ;}}});});}

Ii. Page html

<Table id = "shaitu" width = "100%" align = "center"> <tr> <TD width = "10%" align = "center"> NO. </TD> <TD width = "20%" align = "center"> billing type </TD> <TD width = "20%" align = "center"> professional </TD> <TD width = "15%" align = "center"> sub-category </TD> <TD width = "15%" align = "center"> sub-item name </TD> <TD width = "20%" align = "center"> Cost (10 million RMB) </TD> </tr> <TD align = "center"> </TD> <TD align = "center"> </TD> <TD align =" center "> </TD> <TD align =" center "> </TD> </tr> </table>

3. Page call Method

<script type="text/javascript">                $(document).ready(function () {                    table_rowspan("#shaitu", 2);                    table_rowspan("#shaitu", 3);                    table_rowspan("#shaitu", 4);                });</script>

 

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.