Merge adjacent cells with the same text in the table using jquery (1)

Source: Internet
Author: User

The generated data table is roughly as follows:

Region Region ProductCode Product Name Quantity Valid till Validity Period (month) Product batch number Specification Unit Bar Code
Guangdong Shenzhen 00028 Safflower oil              
Guangdong Shenzhen 00028 Safflower oil              
Guangdong Shenzhen 00028 Safflower oil              
Guangdong Guangzhou 00027 White Flower Oil              
Guangdong Guangzhou 00028 Safflower oil              
Guangdong Shenzhen 00028 Safflower oil              
Guangdong Shenzhen 00028 Safflower oil              
Guangdong Shenzhen 00028 Safflower oil              
Guangdong Shenzhen 00028 Safflower oil              

The cells in the first four columns with the same text must be automatically merged. The merge results are as follows:

Region Region Product Code Product Name Quantity Valid till Validity Period (month) Product batch number Specification Unit Bar Code
Guangdong Shenzhen 00028 Safflower oil              
             
             
Guangzhou 00027 White Flower Oil              
00028 Safflower oil              
Shenzhen              
             
             
             

1. Introduce jquery in the HTML head
<Script language = "JavaScript" type = "text/JavaScript" src = "JS/jquery-1.2.js"> </SCRIPT> 

2. Add the function for merging Cells

// Function Description: merges adjacent cells in the same text of the specified table (table ID is _ w_table_id) and the specified column (number of columns is _ w_table_colnum). <br/> // parameter description: _ w_table_id is the ID of the table to be merged. If you specify table id = "data" in HTML, this parameter should be # data <br/> // parameter description: _ w_table_colnum is the column where the cells to be merged are located. It is a number starting from Column 1 on the left. <Br/> function _ w_table_rowspan (_ w_table_id, _ w_table_colnum) {<br/> _ w_table_firsttd = ""; <br/> _ w_table_currenttd = ""; <br/> _ w_table_spannum = 0; <br/> _ w_table_obj = $ (_ w_table_id + "Tr TD: Nth-child (" + _ w_table_colnum + ")"); <br/> _ w_table_obj.each (function (I) {<br/> if (I = 0) {<br/> _ w_table_firsttd = $ (this ); <br/> _ w_table_spannum = 1; <br/>}else {<br/> _ w_table_currenttd = $ (this); <br/> If (_ w_table_firsttd.text () = _ w_table_currenttd.text () {<br/> _ w_table_spannum ++; <br/> _ w_table_currenttd.hide (); // remove (); <br/> _ w_table_firsttd.attr ("rowspan", _ w_table_spannum); <br/>} else {<br/> _ w_table_firsttd = $ (this ); <br/> _ w_table_spannum = 1; <br/>}< br/>}); <br/>}< br/> // function description: merge adjacent cells of the same text in the specified table (table ID is _ w_table_id) specified row (number of rows is _ w_table_rownum) <br/> // parameter description: _ w_table_id: Table ID. If you specify table id = "data" in HTML, this parameter should be # data <br/> // parameter description: _ w_table_rownum is the row of the cells to be merged. For the parameter format, see Nth-Child parameters in jquery. <Br/> // if it is a number, it starts from the first row 1 on the leftmost side. <Br/> // "even" indicates an even number of rows <br/> // "odd" indicates an odd number of rows <br/> // "3N + 1" indicates that the number of rows is 1., 4, 7, 10 ....... <br/> // parameter description: _ w_table_maxcolnum indicates the maximum number of columns corresponding to cells in the specified row. Cells with more columns than this value are not compared and merged. <Br/> // This parameter can be null. If it is null, all cells in the specified row must be compared and merged. <Br/> function _ w_table_colspan (_ w_table_id, _ w_table_rownum, _ w_table_maxcolnum) {<br/> If (_ w_table_maxcolnum = void 0) {_ w_table_maxcolnum = 0 ;} <br/> _ w_table_firsttd = ""; <br/> _ w_table_currenttd = ""; <br/> _ w_table_spannum = 0; <br/> $ (_ w_table_id + "TR: Nth-child (" + _ w_table_rownum + ")"). each (function (I) {<br/> _ w_table_obj = $ (this ). children (); <br/> _ w_table_obj.each (function (I) {<br/> if (I = 0) {<br/> _ w_table_firsttd = $ (this ); <br/> _ w_table_spannum = 1; <br/>} else if (_ w_table_maxcolnum> 0) & (I> _ w_table_maxcolnum )) {<br/> return ""; <br/>} else {<br/> _ w_table_currenttd = $ (this); <br/> If (_ w_table_firsttd.text () ==_ w_table_currenttd.text () {<br/> _ w_table_spannum ++; <br/> _ w_table_currenttd.hide (); // remove (); <br/> _ w_table_firsttd.attr ("colspan", _ w_table_spannum); <br/>} else {<br/> _ w_table_firsttd = $ (this ); <br/> _ w_table_spannum = 1; <br/>}< br/>}); <br/>}

3. Call the Merge function in the HTML head to merge cells.
<SCRIPT type = "text/JavaScript"> <! -- <Br/> $ (document ). ready (function () {<br/> _ w_table_rowspan ("# spdata", 4); <br/> _ w_table_rowspan ("# spdata", 3 ); <br/> _ w_table_rowspan ("# spdata", 2); <br/> _ w_table_rowspan ("# spdata", 1); <br/> }); </body> <br/> </ptml>

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.