首页 > 网站 > WEB开发 > 正文

JQuery databases 动态合并单元格

2024-04-27 15:08:50
字体:
来源:转载
供稿:网友
	function fnMergeGroupColums(mergeGroupColums){		var api = this.api(); 				var rows = api.rows( {page:'current'} ).nodes();		for(var x in mergeGroupColums){			var groups = mergeGroupColums[x];			var by = groups.by;						var last=null;			var tr = null;			var trHolder = null;			var maxSame=1;						var merCols = [];			var colsLast = [];			var cols = groups.cols;			for(var i in cols){				var col = groups.cols[i];				merCols.push(typeof(col)=="number"?col:col.index);				colsLast.push(null);			}			var seq = 0;			var data =  api.column(by, {page:'current'} ).data();			var mergeData =  api.columns(merCols, {page:'current'}).data();						data.each( function ( group, i ) {				console.log(data.length)				tr = $(rows[i]);				            					if(i==0){					last = group;					for(var j in colsLast){						colsLast[j] = mergeData[j][i];					}					trHolder = tr;				}				else if (last!=group) {					seq ++;										for(var j in merCols){						var col = cols[j];						var text = null;						if(col.content=="seq"){							text = seq;						}						else if(col.content instanceof Function){							text = col.content(); 						}						else{							text = colsLast[j];							text = text == null ? "" : text;						}						$("td:eq("+merCols[j]+")",trHolder)						.attr("rowspan",maxSame).text(text)						.CSS("vertical-align","middle");;					}										last = group;					for(var j in colsLast){						colsLast[j] = mergeData[j][i];					}					trHolder = tr;					maxSame = 1;				}else{					maxSame++;					for(var j in merCols){						$("td:eq("+merCols[j]+")",tr).hide();					}				}								if(i==data.length-1){					seq ++;					for(var j in merCols){						var col = cols[j];						var text = null;						if(col.content=="seq"){							text = seq;						}						else if(col.content instanceof Function){							text = col.content(); 						}						else{							text = colsLast[j];							text = text == null ? "" : text;						}												$("td:eq("+merCols[j]+")",trHolder)						.attr("rowspan",maxSame).text(text)						.css("vertical-align","middle");					}				}			});		}	}

调用:

var table = $('#example').initTable({ //合并单元格			mergeGroupColums:[{				by:4,//根据那一列合并				cols:[					0,1,2,3 //合并那几列				]			}],});

效果图:


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表