使用jQuery实现自定义添加table的行和列

需求:根据用户自己填写的两个参数来添加需要合并的行的总行数和每个合并行里面包含多少行。

  1. 图中input框中的2代表第一列有这样的黄色(或者别的颜色,一共设置了4种颜色是一个循环)
  2. 3代表每种颜色下的行数

在这里插入图片描述jsp页面代码

<div class="main-container" id="main-container">
		<!-- /section:basics/sidebar -->
		<div class="main-content">
			<div class="main-content-inner">
				<div class="page-content">
					<div class="row ">
						<div class="col-xs-24">
						<!-- 检索  -->
						<form action="cmmshadowdetail/list.do" method="post" name="Form" id="Form">
						<!-- <input type="text" id="encoderContent"  value="" style="width:300px;"> -->
						<input type="hidden" name="encoderContent" id="encoderContent" value="${pd.ENCODERCONTENT}"/>
						
						<table style="margin-top:5px;">
							<tr>
								<td>
									<div class="nav-search">
										<span class="input-icon">
											<input type="text" placeholder="请先输入Glass_ID" class="nav-search-input" id="keywords" autocomplete="off" name="keywords" value="" placeholder="这里输入关键词"/>
											<i class="ace-icon fa fa-search nav-search-icon"></i>
										</span>
									</div>
								</td>
								<td style="vertical-align:top;padding-left:2px"><a class="btn btn-light btn-xs" onclick="tosearch();"  title="检索"><i id="nav-search-icon" class="ace-icon fa fa-search bigger-110 nav-search-icon blue"></i></a></td>
								<td>
								 	<input type="number" placeholder="样品个数" class="table_row" id="table_row" autocomplete="off" name="table_row" value="" placeholder=""/>
									<input type="number" placeholder="每个样品对应的点位数" class="table_row1" id="table_row1" autocomplete="off" name="table_row1" value="" placeholder=""/>
								</td>
							 	<td style="vertical-align:top;padding-left:2px"><a class="btn btn-xs btn-success" onclick="createTable();">添加单元格</a></td>
								<td style="vertical-align:top;padding-left:2px;">
									<%-- <c:if test="${QX.add == 1 }"> --%>
									<a class="btn btn-xs btn-success" onclick="save();">保存</a>
									<%-- </c:if> --%>
								</td>	
							</tr>
						</table>
						<!-- 检索  -->
						<!-- 信息标签 -->
						<table  class="table  table-bordered table-hover" style="margin-top:10px;">
							<c:forEach items="${varList}" var="var" varStatus="vs">
							<tr>
								<td>蒸镀日期</td>
								<td><input name="EVAPORATION_DATE" readonly="readonly" id="EVAPORATION_DATE" value="${var.EVAPORATION_DATE}" type="text"   placeholder=""/></td>
								<td>蒸镀时间</td>	
								<td><input name="EVAPORATION_TIME" readonly="readonly" id="EVAPORATION_TIME" value="${var.EVAPORATION_TIME}" type="text"   placeholder="" /></td>
								<td>chamber</td>
								<td><input name="CHAMBER" readonly="readonly" id="CHAMBER" value="${var.CHAMBER}" type="text"placeholder="" /></td>
								<td>Mask_ID</td>	
								<td><input name="MASK_ID" readonly="readonly" id="MASK_ID" value="${var.MASK_ID}" type="text"placeholder="" /></td>
							</tr>
							<tr>
								<td style="color:red"><B>Glass_ID</B></td>
								<td><input name="GLASS_ID" readonly="readonly" id="GLASS_ID" value="${var.GLASS_ID}" type="text"placeholder=""  /></td>
								<td>取出时间</td>	
								<td><input name="REMOVAL_TIME" readonly="readonly" id="REMOVAL_TIME" value="${var.REMOVAL_TIME}" type="text"placeholder="" /></td>
								<td>目标膜厚</td>
								<td><input name="TARGET_FILM_THICKNESS" readonly="readonly" id="TARGET_FILM_THICKNESS" value="${var.TARGET_FILM_THICKNESS}" type="text"placeholder="" /></td>
								<td>操作人</td>	
								<td><input name="OPERATOR" readonly="readonly" id="OPERATOR" value="${var.OPERATOR}" type="text"placeholder="" /></td>
							</tr>
							<tr>
								<td>量测日期</td>
								<td><input name="MEASURE_DATE" readonly="readonly" id="MEASURE_DATE" value="${var.MEASURE_DATE}" type="text"placeholder="" /></td>
								<td>量测人员</td>
								<td><input name="MEASURE_PERSON" readonly="readonly" id="MEASURE_PERSON" value="${var.MEASURE_PERSON}" type="text"placeholder="" /></td>
								<td>备注</td>
								<td><input name="REMARK" readonly="readonly" id="REMARK" value="${var.REMARK}" type="text"placeholder="" /></td>	
							</tr>
							</c:forEach>
						</table>
						<!-- 信息标签 -->
						<table id="AddStyle" class="table table-striped table-bordered table-hover" style="margin-top:5px;">	
							<!-- table-striped 隔行换色   simple-table -->
							<tbody>
							   <tr>
									<td class="center"colspan="13"><b>Measurement量测参数记录</b></td>
							   </tr>
							   <tr>
									<td class="center">Range</td>
									<td class="center"style="width:95px"><input name="range" id="range" value="${pd.RANGE}" type="text"   placeholder=""style="width:90%;"/></td>
									<td class="center">Length</td>
									<td class="center"colspan="2"><input name="length" id="length" value="${pd.LENGTH}" type="text" placeholder="" style="width:90%;"/></td>
									<td class="center">Duration</td>
									<td class="center"colspan="2"><input name="duration" id="duration" value="${pd.DURATION}" type="text"  placeholder="" style="width:90%;"/></td>
									<td class="center">Stylus Force</td>
									<td class="center"colspan="3"><input name="stylus_force" id="stylus_force" value="${pd.STYLUS_FORCE}" type="text"placeholder="" style="width:90%;"/></td>
								</tr>
							   <tr>
									<td class="center">日期</td>
									<td class="center">时间</td>
									<td class="center"colspan="5">NOOZLE</td>
									<td class="center"colspan="6">SCAN</td>
								</tr>
								<tr>
									<td class="center"colspan="2">参考值</td>
									<td class="center"><input name="film_thickness1_standard" id="film_thickness1_standard" value="" type="number"placeholder="" style="width:90%;"/></td>
									<td class="center"></td>
									<td class="center"><input name="A1_standard" id="A1_standard" value="" type="number"placeholder="" style="width:90%;"/></td>
									<td class="center"><input name="B1_standard" id="B1_standard" value="" type="number"placeholder="" style="width:90%;"/></td>
									<td class="center"><input name="C1_standard" id="C1_standard" value="" type="number"placeholder="" style="width:90%;"/></td>
									<td class="center"></td>
									<td class="center"><input name="film_thickness2_standard" id="film_thickness2_standard" value="" type="number"placeholder="" style="width:90%;"/></td>	
									<td class="center"></td>
									<td class="center"><input name="A2_standard" id="A2_standard" value="" type="number"placeholder="" style="width:90%;"/></td>
									<td class="center"><input name="B2_standard" id="B2_standard" value="" type="number"placeholder="" style="width:90%;"/></td>
									<td class="center"><input name="C2_standard" id="C2_standard" value="" type="number"placeholder="" style="width:90%;"/></td>
								</tr>
								<tr>
									<td class="center"colspan="2">误差百分比( <B>%</B>)</td>
									<td class="center"><input οninput="if(value>100)value=100;if(value.length>3)value=value.slice(0,4);if(value<0)value=0"name="film_thickness1_percent" id="film_thickness1_percent" value="" type="number"placeholder="" style="width:90%;"/></td>
									<td class="center"></td>
									<td class="center"><input οninput="if(value>100)value=100;if(value.length>3)value=value.slice(0,4);if(value<0)value=0" name="A1_percent" id="A1_percent" value="" type="number"placeholder="" style="width:90%;"/></td>
									<td class="center"><input οninput="if(value>100)value=100;if(value.length>3)value=value.slice(0,4);if(value<0)value=0" name="B1_percent" id="B1_percent" value="" type="number"placeholder="" style="width:90%;"/></td>
									<td class="center"><input οninput="if(value>100)value=100;if(value.length>3)value=value.slice(0,4);if(value<0)value=0"name="C1_percent" id="C1_percent" value="" type="number"placeholder="" style="width:90%;"/></td>
									<td class="center"></td>
									<td class="center"><input οninput="if(value>100)value=100;if(value.length>3)value=value.slice(0,4);if(value<0)value=0"name="film_thickness2_percent" id="film_thickness2_percent" value="" type="number"placeholder="" style="width:90%;"/></td>	
									<td class="center"></td>
									<td class="center"><input οninput="if(value>100)value=100;if(value.length>3)value=value.slice(0,4);if(value<0)value=0"name="A2_percent" id="A2_percent" value="" type="number"placeholder="" style="width:90%;"/></td>
									<td class="center"><input οninput="if(value>100)value=100;if(value.length>3)value=value.slice(0,4);if(value<0)value=0"name="B2_percent" id="B2_percent" value="" type="number"placeholder="" style="width:90%;"/></td>
									<td class="center"><input οninput="if(value>100)value=100;if(value.length>3)value=value.slice(0,4);if(value<0)value=0"name="C2_percent" id="C2_percent" value="" type="number"placeholder="" style="width:90%;"/></td>
								</tr>
							 <tr>
									<td class="center" >样品编号</td>
									<td class="center">点位</td>
									<td class="center">膜厚</td>
									<td class="center">95%膜厚</td>
									<td class="center">A</td>
									<td class="center">B</td>
									<td class="center">C</td>
									<td class="center">点位</td>
									<td class="center">膜厚</td>
									<td class="center">95%膜厚</td>
									<td class="center">A</td>
									<td class="center">B</td>
									<td class="center">C</td>
								</tr>
							</tbody>
						</table>
						<div class="page-header position-relative">
						<%-- <table style="width:100%;">
							<tr>
								<td style="vertical-align:top;">
									<c:if test="${QX.add == 1 }">
									<a class="btn btn-mini btn-success" onclick="add();">新增</a>
									</c:if>
									<c:if test="${QX.add == 1 }">
									<a class="btn btn-mini btn-info" onclick="makeAll('确定要删除选中的数据吗?');">批量打印</a>
									</c:if>
									<c:if test="${QX.del == 1 }">
									<a class="btn btn-mini btn-danger" onclick="makeAll('确定要删除选中的数据吗?');" title="批量删除" ><i class='ace-icon fa fa-trash-o bigger-120'></i></a>
									</c:if>
								</td>
								<td style="vertical-align:top;"><div class="pagination" style="float: right;padding-top: 0px;margin-top: 0px;">${page.pageStr}</div></td>
							</tr>
						</table> --%>
						</div>
						<!--  <td align="right"><button  onClick="window.print()">打印</button>&nbsp;</td>-->
						</form>
						</div>
						<!-- /.col -->
					</div>
					<!-- /.row -->
				</div>
				<!-- /.page-content -->
			</div>
		</div>
		<!-- /.main-content -->

		<!-- 返回顶部 -->
		<a href="#" id="btn-scroll-up" class="btn-scroll-up btn btn-sm btn-inverse">
			<i class="ace-icon fa fa-angle-double-up icon-only bigger-110"></i>
		</a>

	</div>

js代码

 function createTable(){ 
	    	//先扫码获取信息标签里面信息,用GLASS_ID来判断
	    	  if($("#GLASS_ID").val()==undefined  ||$("#GLASS_ID").val()==""||$("#GLASS_ID").val()==null){
  			//if($("#GLASS_ID").val().length>0){		    	
	    		 /*  alert("请先扫码获取信息标签!!!");
	    		  return; */
	    	  }
	    	  var tr=$("#table_row").val();
	    	  var td=$("#table_row1").val();
	    		  //$("#AddStyle tbody").append('<tr></tr>').addClass('tr1');
	    		  //给添加的每个tr加上id,id是递增的
	    		//  $("#AddStyle tbody").append('<tr></tr>');
	    		  //tr的总数量
	    		  var len=$("#AddStyle > tbody > tr").length;
	    		  var num=0;
	    		  //防止第二次添加时出错
 			       var twiceDo=0;
	  			   if (len>6) {
	 				 	twiceDo=len-6; 
	 			    }
	    		  for (var j = 1; j <= td*tr; j++) {
	    			  num++;
	    			  var num1=Number(num)+Number(td);
	    			//   len=len+j;
	    			  //$("#AddStyle tbody").append('<tr id='+"clone"+len+'>'
	  				  $("#AddStyle tbody").append('<tr id="clone">'
	    					    //+'<td class="center bg-warning"style="visible:none;"><input class="span10 date-picker" placeholder="" style="width:98%;" id='+"sample_no"+j+'></input></td>'
	  							//+'<td class="center"colspan="1"value='+num+' id='+"point_position"+j+'>'+num+'</td>'
	  							+'<td class="center" id='+"point_position"+(j+twiceDo)+' >'+num+'</td>'
	  							+'<td class="center"><input type="number"style="width:98%" onmousewheel="return false;"οnchange="rangeCheck(this)" autocomplete="off"placeholder="" id='+"film_thickness1_"+j+'></input></td>'
	  							+'<td class="center"><input type="number"style="width:98%" onmousewheel="return false;" readonly autocomplete="off" placeholder=""id='+"film_thickness95_1_"+j+'></input></td>'
	  							+'<td class="center"><input type="number"style="width:98%" onmousewheel="return false;"οnblur="rangeCheck(this)"autocomplete="off" placeholder=""id='+"A1_"+j+ '></input></td>'
	  							+'<td class="center"><input type="number"style="width:95%" onmousewheel="return false;"οnblur="rangeCheck(this)"autocomplete="off" placeholder=""id='+"B1_"+j+ '></input></td>'
	  							+'<td class="center"><input type="number"style="width:95%" onmousewheel="return false;"οnblur="rangeCheck(this)"autocomplete="off" placeholder=""id='+"C1_"+j+ '></input></td>'
	  							+'<td class="center" id='+"point_position1_"+j+' >'+num1+'</td>'
	  							+'<td class="center"><input type="number"style="width:95%" onmousewheel="return false;"οnblur="rangeCheck(this)"autocomplete="off" placeholder=""id='+"film_thickness2_"+j+ '></input></td>'
	  							+'<td class="center"><input type="number"style="width:95%" onmousewheel="return false;" readonly autocomplete="off" placeholder=""id='+"film_thickness95_2_"+j+ '></input></td>'
	  							+'<td class="center"><input type="number"style="width:95%" onmousewheel="return false;"οnblur="rangeCheck(this)"autocomplete="off" placeholder=""id='+"A2_"+j+ '></input></td>'
	  							+'<td class="center"><input type="number"style="width:95%" onmousewheel="return false;"οnblur="rangeCheck(this)"autocomplete="off" placeholder=""id='+"B2_"+j+ '></input></td>'
	  							+'<td class="center"><input type="number"style="width:95%" onmousewheel="return false;"οnblur="rangeCheck(this)"autocomplete="off" placeholder=""id='+"C2_"+j+ '></input></td>'
	  							+'<td class="center"style="display:none"><input type="text" autocomplete="off" placeholder=""id='+"hidden_sample"+j+ '></input></td>'
	  							+'</tr>'
	  							)
	    			    if(j%td==0) {
						 num=0;						
						}		
	    		  }
  			      
  			    	//第一次操作时直接根据用户填的tr来合并就可以了
  			    	//第二次操作的时候要合并的列的序号要加上已经存在的tr,这样才能保证第二次循环合并的时候不会影响第一次的单元格
	    		   for (var i = 0; i < tr; i++) {
	    			   //需要加上rowspan的行数
	    			    var tr_eq=6+td*i+twiceDo;
	    			    //if($("#AddStyle tbody").children("tr:eq("+tr_eq+")").attr("id")!="clone"){}
	    			    	//添加合并的 第一列																						
							$("#AddStyle tbody").children("tr:eq("+tr_eq+")").children("td:eq(0)").before('<td class="center aaa" value="" id='+"sample_no"+(i+1)+ ' rowspan='+ td +'><input type="number" onmousewheel="return false;"placeholder=""style="width:98%"></input></td>')
							/* $("#AddStyle tbody").children("tr:eq("+tr_eq+")").children("td:eq(0)").attr("rowspan",td);
							$("#AddStyle tbody").children("tr:eq("+tr_eq+")").children("td:eq(0)").removeAttr("display"); */
							//换颜色
							switch (i%4) {
							case 0:
								$("#AddStyle tbody").children("tr:eq("+tr_eq+")").children("td:eq(0)").addClass("bg-warning");
								break;
							case 1:
								$("#AddStyle tbody").children("tr:eq("+tr_eq+")").children("td:eq(0)").addClass("bg-danger");
								break;
							case 2:
								$("#AddStyle tbody").children("tr:eq("+tr_eq+")").children("td:eq(0)").addClass("bg-info");
								break;
							case 3:
								$("#AddStyle tbody").children("tr:eq("+tr_eq+")").children("td:eq(0)").addClass("bg-success");
								break;						
  				}				
			}
	    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值