Home > Web Front-end > JS Tutorial > body text

Code for merging adjacent cells with the same text in a table based on jQuery_jquery

WBOY
Release: 2016-05-16 18:08:39
Original
943 people have browsed it

ONE

已经生成的数据表格大致内容如下:

地区 地区 商品代码 商品名称 数量 有效期至 距效期(月) 产品批号 规格 单位 条形码
广东 深圳 00028 红花油              
广东 深圳 00028 红花油              
广东 深圳 00028 红花油              
广东 广州 00027 白花油              
广东 广州 00028 红花油              
广东 深圳 00028 红花油              
广东 深圳 00028 红花油              
广东 深圳 00028 红花油              
广东 深圳 00028 红花油              

需要将前四列具有相同文本的相邻单元格进行自动合并,合并后如下:

地区 地区 商品代码 商品名称 数量 有效期至 距效期(月) 产品批号 规格 单位 条形码
广东 深圳 00028 红花油              
             
             
广州 00027 白花油              
00028 红花油              
深圳              
             
             
             

1. Introduce jQuery into the head of html

2. Add a function to merge cells

Copy the code The code is as follows:

/ /Function description: Merge adjacent cells with the same text in the specified table (the table id is _w_table_id) and the specified column (the number of columns is _w_table_colnum)
//Parameter description: _w_table_id is the id of the table that needs to merge cells . If table id="data" is specified in HTMl, this parameter should be #data
//Parameter description: _w_table_colnum is the column where the cells need to be merged. It is a number, starting from 1 in the first column on the left.
Function _W_TABLE_ROWSPAN (_W_TABLE_ID, _W_TABLE_COLNUM) {
_W_TABLE_FIRSTTD = "" "";
_w_table_currenttd = "";
_w_table_spannum = 0;
_w_table_obj = $ (_ w_table_id "trd: nth-child ( " _w_table_colnum ")");
_w_table_Obj.each(function(i){
if(i==0){
_w_table_firsttd = $(this);
_w_table_SpanNum = 1;
}else{
_w_table_currenttd = $(this);
if(_w_table_firsttd.text()==_w_table_currenttd.text()){
_w_table_SpanNum;
_w_table_currenttd.hide(); //remove( );
_w_table_firsttd.attr("rowSpan",_w_table_SpanNum);
}else{
_w_table_firsttd = $(this);
_w_table_SpanNum = 1;
}
}
});
}

//Function description: Merge adjacent cells with the same text in the specified table (table id is _w_table_id) and specified row (number of rows is _w_table_rownum)
// Parameter description: _w_table_id is the table id that needs to be merged. If table id="data" is specified in HTMl, this parameter should be #data
//Parameter description: _w_table_rownum is the row where the cells need to be merged. For its parameter form, please refer to the nth-child parameter in jQuery.
// If it is a number, start counting from the first line on the left with 1.
// "even" means even lines
// "odd" means odd lines
// "3n 1" means the number of lines is 1, 4, 7, 10.
//Parameters Note: _w_table_maxcolnum is the maximum number of columns corresponding to the cells in the specified row. Cells with a number greater than this value will not be compared and merged.
// This parameter can be empty. If it is empty, all cells in the specified row will be compared and merged.
function _w_table_colspan(_w_table_id,_w_table_rownum,_w_table_maxcolnum){
if(_w_table_maxcolnum == void 0){_w_table_maxcolnum=0;}
_w_table_firsttd = "";
_w_table_currenttd = "";
_w_table _SpanNum = 0;
$(_w_table_id " tr:nth-child(" _w_table_rownum ")").each(function(i){
_w_table_Obj = $(this).children();
_w_table_Obj.each (function(i){
if(i==0){
_w_table_firsttd = $(this);
_w_table_SpanNum = 1;
}else if((_w_table_maxcolnum>0)&&(i> _w_table_maxcolnum)){
return "";
}else{
_w_table_currenttd = $(this);
if(_w_table_firsttd.text()==_w_table_currenttd.text()){
_w_table_SpanNum ;
_w_table_currenttd.hide(); //remove();
_w_table_firsttd.attr("colSpan",_w_table_SpanNum);
}else{
_w_table_firsttd = $(this);
_w_table_SpanNum = 1;
}
}
});
});
}

3. Call the merge function in the head of html to merge cells
Copy code The code is as follows:


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!