子节点复选框选中与取消选中

Posted zhangzhangjava

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了子节点复选框选中与取消选中相关的知识,希望对你有一定的参考价值。

<%@page import="com.cits.online.buzlogic.bo.SpecifyAgentRelationBO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.cits.online.buzlogic.bo.OperatorBO,
				com.cits.online.common.login.OperatorFactory,
				com.cits.online.common.util.CmnUtFunc,
				java.util.List,
				java.util.Map,
				java.util.Iterator,
				java.util.Set"%>

<%
	OperatorBO operatorBO = (OperatorBO)OperatorFactory.getOperator().getOperatorBO();
	String opener_agent_text_name = CmnUtFunc.nvl(request.getParameter("agent_text_name"));
	String opener_agent_text_id = CmnUtFunc.nvl(request.getParameter("agent_text_id"));
	
	String openerAgentId = CmnUtFunc.nvl(request.getParameter("agentId"));
	
	String[] deptIdArray=null;
	if(CmnUtFunc.isNotNull(openerAgentId))
	   deptIdArray = openerAgentId.split(",");
	
	
	String company_id = CmnUtFunc.nvl(request.getParameter("company_id"));
	Map agentMap = (Map)request.getAttribute("agentMap");
	String operate_type = CmnUtFunc.nvl(request.getParameter("operate_type"));
	boolean showAllAgent = true;
	if(request.getAttribute("showAllAgent") != null)
		showAllAgent = ((Boolean)request.getAttribute("showAllAgent")).booleanValue();
	
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>代理选择new</title>
<script type='text/javascript' src='/citsonlineWeb/js/jquery/jquery.js'></script>
<script type='text/javascript' src='./online/js/jquery-1.9.1.js'></script>
<link href="<%=request.getContextPath()%>/themes/<%=operatorBO.getTheme() %>/css/common.css" rel="stylesheet" type="text/css" />
<link href="<%=request.getContextPath()%>/themes/<%=operatorBO.getTheme() %>/css/displaytag/screen.css" rel="stylesheet" type="text/css"/>
<style type="text/css">
	<!--
    .orange 
		font-family: "宋体";
		font-size: 12px;
		color: #ff7e22;
	 
	-->
	</style>
<script language="JavaScript">
    	
	    
		function clean()
			window.opener.document.getElementById("<%=opener_agent_text_name%>").value = "全部代理";
			window.opener.document.getElementById("<%=opener_agent_text_id%>").value = "";
			window.close();
		
		
		function query()
			var agent_name = document.getElementById("agent_name").value;
			var useForm = document.getElementById("agentForm");
			useForm.action = "./switchdo.do?prefix=/reportCenter&page=/reportCenterCommon.do&actionType=allAgent&operate_type=query&agent_text_name=<%=opener_agent_text_name%>&agent_text_id=<%=opener_agent_text_id%>&company_id=<%=company_id%>";
			useForm.submit();
		
		//展开代理商列表
		function showAgentList(key)
			var flag = false;
			var flagText = event.srcElement.showFlag;
			if(flagText=="+")
				flag = true;
				event.srcElement.showFlag = "-";
				event.srcElement.src="/citsonlineWeb/reportCenter/images/open.jpg";
			else
				flag = false;
				event.srcElement.showFlag = "+";
				event.srcElement.src="/citsonlineWeb/reportCenter/images/close.jpg";
			
			var trArr = document.getElementsByTagName("tr");
			if(trArr!=null&&trArr.length>0)
				for(var i=0;i<trArr.length;i++)
					var obj = trArr[i];
					if(obj.prefix == key)
						if(flag)
							obj.style.display = "block";
						else
							obj.style.display = "none";
						
						
					
				
			
		
		//子节点全选或全不选
		function checkAllchildren(key)
			var obj = event.srcElement;
			var checkboxArr = document.getElementsByName("useAgentID");
			if(checkboxArr!=null&&checkboxArr.length>0)
				for(var i=0;i<checkboxArr.length;i++)
					var checkObj = checkboxArr[i];
					if(checkObj.prefix==key)
						checkObj.checked = obj.checked;
					
				
			
		
		//选择已经选中的代理
		function selected(opener_agent_id,opener_agent_name)
			//获得选中的checkBox
	  		var agentArr = orgnizeCheckedAgentInfo();
	  		if(agentArr==null||agentArr.length<=0)
	  			alert("请选择要查询的代理!");
	  			return;
	  		else
	  			var agentIDs = "";
	  			var agentNames = "";
	  			for(var i=0;i<agentArr.length;i++)
	  				var obj = agentArr[i];
	  				agentIDs+=obj.getAgentID();
	  				agentNames+=obj.getAgentName();
	  			
	  			//去掉最后的,号
	  			agentIDs = agentIDs.substr(0,agentIDs.length-1);
	  			agentNames = agentNames.substr(0,agentNames.length-1);
	  			window.opener.document.getElementById(opener_agent_id).value = agentIDs;	
		  		window.opener.document.getElementById(opener_agent_name).value = agentNames;
		  		
		  		
			try
				window.opener.cleanDeptInfo();
				window.opener.cleanAccountInfo();
			catch(e)
			
		  		
		  		
		  		
		  		
		  		
		  		
		  		
				window.close();
	  		
	    
	    //定义AgentInfo对象
	    function AgentInfoObj(agentID,agentName)
	    	this.agentID = agentID;
	    	this.agentName = agentName;
	    	this.toString = function()
	    		return this.agentID+" "+this.agentName;
	    	
	    	this.getAgentID = function()
	    		return this.agentID+",";
	    	
	    	this.getAgentName = function()
	    		return this.agentName+",";
	    	
	    
	    //组织选中的checkBox
	    function orgnizeCheckedAgentInfo()
	    	var agentArr = new Array();
	    	var index = 0;
	    	var checkboxArr = document.getElementsByName("useAgentID");
		    if(checkboxArr!=null&&checkboxArr.length>0)
				for(var i=0;i<checkboxArr.length;i++)
					var checkObj = checkboxArr[i];
					if(checkObj.checked)
						agentArr[index] = new AgentInfoObj(checkObj.value,checkObj.valueStr);
						index++;
					
				
			
			return agentArr;
	    
	    
	    $(function()
	    	
	    	 
		)
	    
		//子节点单击事件
		function sonClick(obj)
	    	var checked = obj.checked;
	    	var prefix = obj.prefix;
	    	if(checked)
	    		var checkboxArr1 = $("input[prefix='" + prefix + "']"); 
	    		var count1 = checkboxArr1.length;//一个父节点下所有子节点的总数
	    		var count2 = $("input[prefix='" + prefix + "']:checked").length;//一个父节点下处于选中状态的子节点的总数
	    		if(count1 == count2)
	    			$("#a"+prefix).attr("checked",true); 
	    		
	    	else
	    		$("#a"+prefix).removeAttr("checked"); 
	    	
	    
		
    </script>
</head>
<body>
<form name="agentForm" id="agentForm" method="post" >
    <input type="hidden" name="seeAllFlg" value="<%=CmnUtFunc.nvl(request.getParameter("seeAllFlg")) %>">
	<table width="98%" border="0" cellspacing="0" cellpadding="0">
		<tr>
		  <td valign="top">
			
				<table border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td class="top_up_td"></td>
					</tr>
				</table>
				<table id="querytable" width="99%" border="0" cellspacing="0" cellpadding="0" align="center">
				  <tr class="">
					<td width="13%" class="text_right_12p">
						代理:			
					</td>
					<td width="1%"> </td>
					<td width="86%" class="align_left"> 
						<input type="text" id="agent_name" name="agent_name" value="<%=CmnUtFunc.nvl(request.getParameter("agent_name"))%>"   onKeyDown="if (event.keyCode==13) query();">
						<input name="button2" type="button" class="common_btn" value="查 找" onClick="query()">
						<%if(showAllAgent)%>
						<input name="button2" type="button" class="common_btn" value="全 部" onClick="clean()">
						<%%>
						<input name="button2" type="button" class="common_btn" value="选 择" onClick="selected('<%=opener_agent_text_id%>','<%=opener_agent_text_name%>');">
						
					</td>
				  </tr>
			</table>
			
			<%
				if(agentMap!=null&&!agentMap.isEmpty())
			%>
			  <table  cellpadding="0" cellspacing="0" class="its" style="width:100%;">
			<%	
					String key = "";
					String keyValue = "";
					List agentList	= null;
					Set keys = agentMap.keySet();
					Iterator it = keys.iterator();
					key = "xxxxxx";
					agentList = (List)agentMap.get(key);
					if(agentList!=null&&agentList.size()>0)
						keyValue = "公司级代理";
					
			%>
					
				 	 <tr class="odd" >
	                      <td style="text-align: left;" width="30%">
		                      <img src="/citsonlineWeb/reportCenter/images/close.jpg" style="cursor:hand" οnclick="showAgentList('<%=key%>');" showFlag = "+"/>
		        			  <% 
		        			  	  String c = "";
			        			  List<SpecifyAgentRelationBO> updateList = (List<SpecifyAgentRelationBO>)request.getAttribute("agentList");
		                      	  if(updateList != null && updateList.size() > 0)
		                      		    int count = 0;
		                      			for(int i = 0; i < updateList.size(); i++)
		                      				SpecifyAgentRelationBO specifyAgentRelationBO = updateList.get(i);
		                      				for(int j=0;j<agentList.size();j++)
		        					 			Map tempMap = (Map)agentList.get(j);
		        					 			String agentID = (String)tempMap.get("agent_id");
		        					 			if(specifyAgentRelationBO.getSpecify_agent_id().equals(agentID))
		        					 				count++;
		        					 			
		        					 			if(count > 0 && count == agentList.size())
		        					 				break;
		        					 			
		                      				
			                      		
		                      			if(count > 0 && count == agentList.size())
		                      				c = "checked";
		                      			
		                      	  
		        			  %>
		        			  <input type="checkbox" <%=c%> οnclick="checkAllchildren('<%=key%>');" name="parentName<%=key%>" id="a<%=key%>"><%=keyValue%> 
	        			  </td>
                     </tr>
				 		<%
							for(int j=0;j<agentList.size();j++)
					 			Map tempMap = (Map)agentList.get(j);
					 			String agentID = (String)tempMap.get("agent_id");
					 			String agentName = (String)tempMap.get("agentName");
			 			%>
		 			<tr class="even" prefix="<%=key%>" style="display:none">
	                      <td  style="text-align: left;" width="70%">    
	                      <img src="/citsonlineWeb/reportCenter/images/page.gif"/>
	                      <% 
	                      		c = "";
	                      		if(updateList != null && updateList.size() > 0)
	                      			for(int i = 0; i < updateList.size(); i++)
	                      				SpecifyAgentRelationBO specifyAgentRelationBO = updateList.get(i);
		                      			if(specifyAgentRelationBO.getSpecify_agent_id().equals(agentID))
		                      				c = "checked";
		                      				break;
		                      			
		                      		
	                      		
	                      		
	                      %>
		                  <input type="checkbox" οnclick="sonClick(this);" <%=c%>  name="useAgentID" prefix="<%=key%>" value="<%=agentID%>"  valueStr="<%=agentName%>" ><%=agentName%>(子节点)</td>
                    </tr>
		 			<%
							
						
								
								for(;it.hasNext();)
								 	key = (String)it.next();
								 	agentList = (List)agentMap.get(key);
								 	if(key.equals("xxxxxx"))
								 		continue;
								 	else
								 		keyValue = (String)((Map)agentList.get(0)).get("deptName");
								 	
				 	%>
				 	 <tr class="odd" >
	                      <td style="text-align: left;" width="30%">
	                      <img src="/citsonlineWeb/reportCenter/images/close.jpg" style="cursor:hand;" οnclick="showAgentList('<%=key%>');" showFlag = "+"/>
	                      <% 
	        			  	  String c = "";
		        			  List<SpecifyAgentRelationBO> updateList = (List<SpecifyAgentRelationBO>)request.getAttribute("agentList");
	                      	  if(updateList != null && updateList.size() > 0)
	                      		    int count = 0;
	                      			for(int i = 0; i < updateList.size(); i++)
	                      				SpecifyAgentRelationBO specifyAgentRelationBO = updateList.get(i);
	                      				for(int j=0;j<agentList.size();j++)
	        					 			Map tempMap = (Map)agentList.get(j);
	        					 			String agentID = (String)tempMap.get("agent_id");
	        					 			if(specifyAgentRelationBO.getSpecify_agent_id().equals(agentID))
	        					 				count++;
	        					 			
	        					 			if(count > 0 && count == agentList.size())
	        					 				break;
	        					 			
	                      				
		                      		
	                      			if(count > 0 && count == agentList.size())
	                      				c = "checked";
	                      			
	                      	  
	        			  %>
	                      <input type="checkbox" <%=c%> οnclick="checkAllchildren('<%=key%>');" name="parentName<%=key%>" id="a<%=key%>" valueStr="<%=key%>"><%=keyValue%> </td>
                     </tr>
				 	
				 	<%
								 	if(agentList!=null&&agentList.size()>0)
								 		for(int j=0;j<agentList.size();j++)
								 			Map tempMap = (Map)agentList.get(j);
								 			String agentID = (String)tempMap.get("agent_id");
								 			String agentName = (String)tempMap.get("agentName");
		 			%>
		 			<tr class="even" prefix="<%=key%>" style="display:none">
                      	<td  style="text-align: left;" width="70%">    
                      		<img src="/citsonlineWeb/reportCenter/images/page.gif"/>
                      		<% 
	                      		c = "";
	                      		if(updateList != null && updateList.size() > 0)
	                      			for(int i = 0; i < updateList.size(); i++)
	                      				SpecifyAgentRelationBO specifyAgentRelationBO = updateList.get(i);
		                      			if(specifyAgentRelationBO.getSpecify_agent_id().equals(agentID))
		                      				c = "checked";
		                      				break;
		                      			
		                      		
	                      		
	                      		
	                       %>
		                    <input type="checkbox" οnclick="sonClick(this);" <%=c%>  name="useAgentID" prefix="<%=key%>" value="<%=agentID%>" valueStr="<%=agentName%>">
	                      	<%=agentName%>(子节点)
                      	</td>
                  	</tr>
		 			<%
								 		
								 	
								 	
								
					%>
			</table>
			<%
				else if(!"".equals(operate_type))
			%>
			<br>
			 <table width="98%" border="0" cellspacing="0" cellpadding="0" align="center">
               		<tr>
			          <td align="left" class="orange">
			           <img src="/citsonlineWeb/themes/default/images/tanhaotishi.gif" />  没有符合条件的信息
                      </td>
			       </tr>
             </table>
			<%
				 
			%>
				
				
              </td>
            </tr>
          </table>      
         </form>
	</body>
</html>

以上是关于子节点复选框选中与取消选中的主要内容,如果未能解决你的问题,请参考以下文章

ztree点击节点实现选中/取消复选框

TreeView 点击触发父节点子节点的连锁反应选中与取消

如果在该 div 中选中了所有子复选框,则选中/取消选中(全选复选框)

TreeView 检测节点选择,但在选中或取消选中时不检测

如何使用 jQuery 使用按钮选中/取消选中所有复选框?

使用 jquery 选中或取消选中 parent parents 复选框