Using jquery to implement cell merging of HTML tables __html

Source: Internet
Author: User

Cell merge using jquery to implement HTML tables:
One, JS script

/** * desc: The adjacent cell * @table_id table ID of the same text that specifies the column (the table ID is table_id) to specify columns (the number of columns Table_colnum): The ID of the table for which you want to merge cells. If you specify a table id= "data" in HTML, this parameter should be #data * @table_colnum: The column in which you want to merge the cells. Refer to the Nth-child parameters in jquery. If the number is the first from the leftmost column 1; Even "denote even sequence;" Odd "denotes odd series; The number of columns represented by "3n+1" is 1, 4, 7 、...... * @table_minrow? : Optional, representing the column with the smallest number of rows to be merged, omitting the representation starting at line No. 0 (closed range) * @table_maxrow? : Optional, representing the column with the largest number of rows to be merged, omitting to represent the maximum row number as the last line of the table (open interval)/function Table_rowspan (table_id,table_colnum) {if Table_c
	    olnum== "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.indexof ("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 = 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_ma
	            Xrow). 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 ();
	                    Table_firsttd.attr ("RowSpan", table_spannum);
	                        else {TABLE_FIRSTTD = $ (this);
	                    Table_spannum = 1;
        }
	                }
	            }
	        }); }/** * desc: The adjacent cell * @table_id table ID that merges the same text of the specified table (table ID table_id) that specifies the row (the number of rows is Table_rownum): for cells that need to be merged. The ID of the table. If you specify a table id= "data" in HTML, this parameter should be #data * @table_rownum: The row where the cell is to be merged. Refer to the Nth-child parameters in jquery. If the number is the first from the leftmost column 1; Even "denotes even rows;" Odd "denotes odd rows; The number of rows represented by "3n+1" is 1, 4, 7 、...... * @table_mincolnum? : optional, representing the smallest column in the row to be merged, omitting the representation starting from column No. 0 (closed range) * @table_maxcolnum? : Optional, representing the largest column in the row to be merged, omitting the maximum number of columns representing the table (open interval)/function Table_colspan (table_id,table_rownum) {//if (table_maxcol
        num = = 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) {T  
                    ABLE_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 (Table_firsttd.width ()) + Parse
   Int (Table_currenttd.width ()));                    		    } table_currenttd.hide ();  
                        Remove ();
                    Table_firsttd.attr ("ColSpan", table_spannum);  
                        }else{TABLE_FIRSTTD = $ (this);  
                    Table_spannum = 1;  
        }  
                }  
            });  
    }); }


Second, page HTML

<table id= "Shaitu" width= "100%" align= "center" >
<tr>
<td width= "10%" align= "center" >
    Serial number
</td>
<td width= "20%" align= "center" >
    expense category
</td>
<td width= "20%" align = "center" >
    Professional
</td>
<td width= "15%" align= "center" >
    Subclass
</td>
<TD width= "15%" align= "center" >
    sub-item name
</td> <td width= "
20%" align= "center" >
    Cost (million)
</td>
</tr>
<tr>
<td align= "center" >
    </td>
<TD align= "center" >
    </td>
<td align= "center" >
    </td>
<td align= " Center ">
    </td>
<td align=" center ">
   </td>
<td align=" center ">
    </td>
</tr>
</table>


Third, the page calls the 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.