分享

Ztree树的复选框和获取选择的节点实例和代码

 一本正经地胡闹 2019-05-23
关于ztree中添加复选框的问题,自己研究查资料和api 获取得到 被选择的树节点的id。
如图只获取  公司办公室和策略组的id
[html] view plain copy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html>  
  3. <head>  
  4. <title>  
  5. 选择收件人  
  6. </title>  
  7.     <link rel="stylesheet" href="<%=request.getContextPath()%>/css/zTreeStyle/demo.css" type="text/css">  
  8.     <link rel="stylesheet" href="<%=request.getContextPath()%>/css/zTreeStyle/zTreeStyle.css" type="text/css">  
  9.     <script charset="utf-8" src="<%=request.getContextPath()%>/js/jquery-1.7.1.min.js"></script>  
  10.     <script charset="utf-8" src="<%=request.getContextPath()%>/js/jquery.ztree.all-3.5.min.js"></script>  
  11.     <SCRIPT type="text/javascript">  
  12.     $(function(){  
  13.         //查询所有的菜单信息  
  14.         $.ajax({  
  15.             type:'post',  
  16.             url:'getorganization.jsp',  
  17.             success:function(result){  
  18.                   
  19.   
  20.                 var setting = {  
  21.                     data:{  
  22.                         simpleData:{  
  23.                             enable:true  
  24.                         }  
  25.                     },  
  26.                     check: {    
  27.                         enable: true  
  28.                     }  
  29.   
  30.                 };  
  31.                 eval("var ztreenode="+result);  
  32.                 $.fn.zTree.init($("#treeDemo"),setting,ztreenode);  
  33.                 SelectNode();  
  34.             }  
  35.         });  
  36.     });  
  37.      function SelectNode() {  
  38.             var treeObj = $.fn.zTree.getZTreeObj("treeDemo");  
  39.                
  40.             var treenode = treeObj.getNodeByParam("id", <%=code%>, null);  
  41.             treeObj.expandNode(treenode, true, true, true);  
  42.             treeObj.selectNode(treenode);  
  43.         }  
  44.     function  fun_getCheckValue(){  
  45.        var zTree = $.fn.zTree.getZTreeObj("treeDemo");  
  46.           
  47.                 var nodes=zTree.getChangeCheckedNodes(true);  
  48.   
  49.                 var result='';  
  50.                 if(nodes.length==0){  
  51.                     alert("请选择部门!!");  
  52.                     return false;  
  53.                 }  
  54.                  for (var i = 0; i < nodes.length; i++) {  
  55.                     var halfCheck = nodes[i].getCheckStatus();  
  56.                      if (!halfCheck.half){  
  57.                         result += nodes[i].id +',';  
  58.                      }  
  59.                   
  60.                 }  
  61.                 result=result.substring(0,result.lastIndexOf(","));  
  62.         window.opener.showChiledValue_(result);//传到父页面  
  63.           window.close();  
  64.       
  65.     }  
  66. </SCRIPT>  
  67. </head>  
  68. <style type="text/css">  
  69.   
  70. </style>  
  71. <body style='background: none repeat scroll 0 0 #ddedf1;'>    
  72. <div style="float:">  
  73. <table >  
  74. <tr><td>  
  75. <div style="float: left; width: 280px;">  
  76. <table width="100%" style="padding-left: 10px;">  
  77.   
  78.     <tr>  
  79.     <td align="left" >组织部门:</td>  
  80.     </tr>  
  81.     <tr>  
  82.         <td><div  class="zTreeDemoBackground left" style="border-width: 0px 1px 1px; border-style: solid; border-color: rgb(220, 217, 212); border-radius: 0px; padding: 0px; margin-top: 0px; width: 100%;background: none repeat scroll 0 0 #ffffff;overflow: auto;height:250px">  
  83.               
  84.                 <div id="treeDemo" class="ztree"></div>  
  85.           
  86.         </div>  
  87.         </td>  
  88.     </tr>  
  89.     </table>  
  90.   
  91. </div>  
  92. </div>  
  93. </td></tr>  
  94.         <tr>  
  95.                 <td align="center" ><input type="button"  
  96.                     class="btn btn_main" value="确定" onClick="fun_getCheckValue();">  
  97.   
  98.                 </td>  
  99.             </tr>  
  100. </table>  
  101.     </div>  
  102.   
  103.   
  104. </body>  
  105. </html>  

如果全部获取的话。并且给节点绑定click事件的话。代码如下
[html] view plain copy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html>  
  3. <head>  
  4. <title>  
  5. 选择收件人  
  6. </title>  
  7.     <link rel="stylesheet" href="<%=request.getContextPath()%>/css/zTreeStyle/demo.css" type="text/css">  
  8.     <link rel="stylesheet" href="<%=request.getContextPath()%>/css/zTreeStyle/zTreeStyle.css" type="text/css">  
  9.     <script charset="utf-8" src="<%=request.getContextPath()%>/js/jquery-1.7.1.min.js"></script>  
  10.     <script charset="utf-8" src="<%=request.getContextPath()%>/js/jquery.ztree.all-3.5.min.js"></script>  
  11.     <SCRIPT type="text/javascript">  
  12.     $(function(){  
  13.         //查询所有的菜单信息  
  14.         $.ajax({  
  15.             type:'post',  
  16.             url:'getorganization.jsp',  
  17.             success:function(result){  
  18.                   
  19.   
  20.                 var setting = {  
  21.                     data:{  
  22.                         simpleData:{  
  23.                             enable:true  
  24.                         }  
  25.                     },  
  26.                     check: {    
  27.                         enable: true  
  28.                     },    
  29.                     callback: {  
  30.                         onClick: zTreeOnClick  
  31.                     }  
  32.   
  33.                 };  
  34.                 console.info(result);  
  35.                 eval("var ztreenode="+result);  
  36.                 $.fn.zTree.init($("#treeDemo"),setting,ztreenode);  
  37.                 SelectNode();  
  38.             }  
  39.         });  
  40.     });  
  41.      function SelectNode() {  
  42.             var treeObj = $.fn.zTree.getZTreeObj("treeDemo");  
  43.                
  44.             var treenode = treeObj.getNodeByParam("id", <%=code%>, null);  
  45.             treeObj.expandNode(treenode, true, true, true);  
  46.             treeObj.selectNode(treenode);  
  47.         }  
  48.     function getAllChildrenNodes(treeNode,result){  
  49.   
  50.       if (treeNode.isParent) {  
  51.   
  52.         var childrenNodes = treeNode.children;  
  53.   
  54.         if (childrenNodes) {  
  55.   
  56.             for (var i = 0; i < childrenNodes.length; i++) {  
  57.   
  58.                 result += ','+childrenNodes[i].id ;  
  59.   
  60.                 result = getAllChildrenNodes(childrenNodes[i], result);  
  61.   
  62.             }  
  63.   
  64.         }  
  65.   
  66.     }  
  67.   
  68.     return result;  
  69.   
  70. }  
  71.     function zTreeOnClick(event, treeId, treeNode) {  
  72.        // alert(treeNode.id + ", " + treeNode.name);  
  73.        var zTree = $.fn.zTree.getZTreeObj("treeDemo");  
  74.        var str =treeNode.id;  
  75.   
  76.      str = getAllChildrenNodes(treeNode,str);  
  77.   
  78.      alert(str); //所有叶子节点ID  
  79.       /* var nodes=zTree.getNodes();   
  80.        var nodes_array = zTree.transformToArray (nodes);  
  81.         alert(nodes_array.length);  
  82.        for(var i=0;i<nodes_array.length;i++){  
  83.            alert(nodes_array[i].name);   
  84.           }   
  85.           */  
  86.         $.ajax({  
  87.             url:'getYuangong.jsp?deptcode='+treeNode.id+"&mm="+<%=Math.random()%>,  
  88.             success:function(result){  
  89.                 $("#employee").html(result);  
  90.                 }  
  91.         });  
  92.     };  
  93.       
  94.     $(function(){  
  95.            $.ajax({  
  96.                 url:'getUserTongshi.jsp?mm='+<%=Math.random()%>,  
  97.                 success:function(result){  
  98.                     $("#employee").html(result);  
  99.                     }  
  100.             });  
  101.           
  102.           
  103.         });  
  104.     function rightMove(value){  
  105.             if(value!=""){  
  106.                $("#choseemployee").append('<option  value="'+value+'">'+$('#employee option:selected').text()+'</option>');  
  107.                $('#employee option:selected').remove();  
  108.             }  
  109.              
  110.     }  
  111.     function leftMove(value){  
  112.           
  113.         if(value!=""){  
  114.            $("#employee").append('<option  value="'+value+'">'+$('#choseemployee option:selected').text()+'</option>');  
  115.            $('#choseemployee option:selected').remove();  
  116.         }  
  117.     }  
  118.   
  119.       
  120. </SCRIPT>  
  121. </head>  
  122. <style type="text/css">  
  123.   
  124. </style>  
  125. <body style='background: none repeat scroll 0 0 #ddedf1;'>    
  126. <div style="float:">  
  127. <table >  
  128. <tr><td>  
  129. <div style="float: left; width: 280px;">  
  130. <table width="100%" style="padding-left: 10px;">  
  131.   
  132.     <tr>  
  133.     <td align="left" >组织部门:</td>  
  134.     </tr>  
  135.     <tr>  
  136.         <td><div  class="zTreeDemoBackground left" style="border-width: 0px 1px 1px; border-style: solid; border-color: rgb(220, 217, 212); border-radius: 0px; padding: 0px; margin-top: 0px; width: 100%;background: none repeat scroll 0 0 #ffffff;overflow: auto;height:250px">  
  137.               
  138.                 <div id="treeDemo" class="ztree"></div>  
  139.           
  140.         </div>  
  141.         </td>  
  142.     </tr>  
  143.     </table>  
  144.   
  145. </div>  
  146. <div style='float:right;width: 250px;'>  
  147. <table width="100%" >  
  148.   
  149.     <tr>  
  150.     <td  align="left">已选收件人:</td>  
  151.     </tr>  
  152.     <tr>  
  153.         <td>  
  154.         <div id="aa"style="border-width: 0px 1px 1px; border-style: solid; border-color: rgb(220, 217, 212); border-radius: 0px; padding: 0px; margin-top: 0px; width: 100%;background: none repeat scroll 0 0 #ffffff;overflow: auto;height:150px">  
  155.             <select ondblclick="leftMove(this.value);" name="choseemployee" id="choseemployee"   multiple="multiple"style="width: 240px; height: 150px;">  
  156.               
  157.             </select>  
  158.             </div>  
  159.         </td>  
  160.     </tr>  
  161.     </table>  
  162.   
  163. </div>  
  164. <div style='float:right;width: 200px;padding-right: 30px;'>  
  165. <table width="100%">  
  166.   
  167.     <tr>  
  168.     <td align="left">部门员工:</td>  
  169.     </tr>  
  170.     <tr>  
  171.         <td>  
  172.         <div id="content"style="border-width: 0px 1px 1px; border-style: solid; border-color: rgb(220, 217, 212); border-radius: 0px; padding: 0px; margin-top: 0px; width: 100%;background: none repeat scroll 0 0 #ffffff;overflow: auto;height:150px">  
  173.                 <select ondblclick="rightMove(this.value);" name="employee" id="employee"    multiple="multiple"style="width: 200px; height: 150px;">  
  174.               
  175.                 </select>  
  176.             </div>  
  177.         </td>  
  178.     </tr>  
  179.     </table>  
  180.   
  181. </div>  
  182. </td></tr>  
  183.         <tr>  
  184.                 <td align="center" ><input type="button"  
  185.                     class="btn btn_main" value="确定" onClick="fun_getCheckValue();">  
  186.   
  187.                 </td>  
  188.             </tr>  
  189. </table>  
  190.     </div>  
  191.     <script type="text/javascript">  
  192.     function fun_getCheckValue(){  
  193.         var staffnos=[];  
  194.         var staffnames=[];  
  195.          for(i=0;i<$("#choseemployee option").length;i++){  
  196.              staffnos.push($("#choseemployee option")[i].value);  
  197.              staffnames.push($("#choseemployee option")[i].text);  
  198.          }  
  199.          window.opener.showChiledValue_(staffnos,staffnames);//传到父页面  
  200.           window.close();  
  201.     }  
  202.   
  203.     </script>  
  204.   
  205. </body>  
  206. </html>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多