checkbox_fun.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <!DOCTYPE html>
  2. <HTML>
  3. <HEAD>
  4. <TITLE> ZTREE DEMO - beforeCheck / onCheck</TITLE>
  5. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  6. <link rel="stylesheet" href="../../../css/demo.css" type="text/css">
  7. <link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type="text/css">
  8. <script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>
  9. <script type="text/javascript" src="../../../js/jquery.ztree.core-3.5.js"></script>
  10. <script type="text/javascript" src="../../../js/jquery.ztree.excheck-3.5.js"></script>
  11. <!--
  12. <script type="text/javascript" src="../../../js/jquery.ztree.exedit-3.5.js"></script>
  13. -->
  14. <SCRIPT type="text/javascript">
  15. <!--
  16. var setting = {
  17. view: {
  18. selectedMulti: false
  19. },
  20. check: {
  21. enable: true
  22. },
  23. data: {
  24. simpleData: {
  25. enable: true
  26. }
  27. },
  28. callback: {
  29. beforeCheck: beforeCheck,
  30. onCheck: onCheck
  31. }
  32. };
  33. var zNodes =[
  34. { id:1, pId:0, name:"随意勾选 1", open:true},
  35. { id:11, pId:1, name:"随意勾选 1-1"},
  36. { id:12, pId:1, name:"随意勾选 1-2", open:true},
  37. { id:121, pId:12, name:"随意勾选 1-2-1"},
  38. { id:122, pId:12, name:"随意勾选 1-2-2"},
  39. { id:2, pId:0, name:"禁止勾选 2", open:true, doCheck:false},
  40. { id:21, pId:2, name:"禁止勾选 2-1", doCheck:false},
  41. { id:22, pId:2, name:"禁止勾选 2-2", checked:true, open:true, doCheck:false},
  42. { id:221, pId:22, name:"禁止勾选 2-2-1", doCheck:false},
  43. { id:222, pId:22, name:"禁止勾选 2-2-2", checked:true, doCheck:false},
  44. { id:23, pId:2, name:"禁止勾选 2-3", doCheck:false}
  45. ];
  46. var code, log, className = "dark";
  47. function beforeCheck(treeId, treeNode) {
  48. className = (className === "dark" ? "":"dark");
  49. showLog("[ "+getTime()+" beforeCheck ]&nbsp;&nbsp;&nbsp;&nbsp;" + treeNode.name );
  50. return (treeNode.doCheck !== false);
  51. }
  52. function onCheck(e, treeId, treeNode) {
  53. showLog("[ "+getTime()+" onCheck ]&nbsp;&nbsp;&nbsp;&nbsp;" + treeNode.name );
  54. }
  55. function showLog(str) {
  56. if (!log) log = $("#log");
  57. log.append("<li class='"+className+"'>"+str+"</li>");
  58. if(log.children("li").length > 6) {
  59. log.get(0).removeChild(log.children("li")[0]);
  60. }
  61. }
  62. function getTime() {
  63. var now= new Date(),
  64. h=now.getHours(),
  65. m=now.getMinutes(),
  66. s=now.getSeconds(),
  67. ms=now.getMilliseconds();
  68. return (h+":"+m+":"+s+ " " +ms);
  69. }
  70. function checkNode(e) {
  71. var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
  72. type = e.data.type,
  73. nodes = zTree.getSelectedNodes();
  74. if (type.indexOf("All")<0 && nodes.length == 0) {
  75. alert("请先选择一个节点");
  76. }
  77. if (type == "checkAllTrue") {
  78. zTree.checkAllNodes(true);
  79. } else if (type == "checkAllFalse") {
  80. zTree.checkAllNodes(false);
  81. } else {
  82. var callbackFlag = $("#callbackTrigger").attr("checked");
  83. for (var i=0, l=nodes.length; i<l; i++) {
  84. if (type == "checkTrue") {
  85. zTree.checkNode(nodes[i], true, false, callbackFlag);
  86. } else if (type == "checkFalse") {
  87. zTree.checkNode(nodes[i], false, false, callbackFlag);
  88. } else if (type == "toggle") {
  89. zTree.checkNode(nodes[i], null, false, callbackFlag);
  90. }else if (type == "checkTruePS") {
  91. zTree.checkNode(nodes[i], true, true, callbackFlag);
  92. } else if (type == "checkFalsePS") {
  93. zTree.checkNode(nodes[i], false, true, callbackFlag);
  94. } else if (type == "togglePS") {
  95. zTree.checkNode(nodes[i], null, true, callbackFlag);
  96. }
  97. }
  98. }
  99. }
  100. function setAutoTrigger(e) {
  101. var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  102. zTree.setting.check.autoCheckTrigger = $("#autoCallbackTrigger").attr("checked");
  103. $("#autoCheckTriggerValue").html(zTree.setting.check.autoCheckTrigger ? "true" : "false");
  104. }
  105. $(document).ready(function(){
  106. $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  107. $("#checkTrue").bind("click", {type:"checkTrue"}, checkNode);
  108. $("#checkFalse").bind("click", {type:"checkFalse"}, checkNode);
  109. $("#toggle").bind("click", {type:"toggle"}, checkNode);
  110. $("#checkTruePS").bind("click", {type:"checkTruePS"}, checkNode);
  111. $("#checkFalsePS").bind("click", {type:"checkFalsePS"}, checkNode);
  112. $("#togglePS").bind("click", {type:"togglePS"}, checkNode);
  113. $("#checkAllTrue").bind("click", {type:"checkAllTrue"}, checkNode);
  114. $("#checkAllFalse").bind("click", {type:"checkAllFalse"}, checkNode);
  115. $("#autoCallbackTrigger").bind("change", {}, setAutoTrigger);
  116. });
  117. //-->
  118. </SCRIPT>
  119. </HEAD>
  120. <BODY>
  121. <h1>用 zTree 方法 勾选 checkbox</h1>
  122. <h6>[ 文件路径: excheck/checkbox_fun.html ]</h6>
  123. <div class="content_wrap">
  124. <div class="zTreeDemoBackground left">
  125. <ul id="treeDemo" class="ztree"></ul>
  126. </div>
  127. <div class="right">
  128. <ul class="info">
  129. <li class="title"><h2>1、beforeCheck / onCheck 事件回调函数控制</h2>
  130. <ul class="list">
  131. <li>利用 beforeCheck / onCheck 事件回调函数 可以控制是否允许 更改 节点勾选状态,这里简单演示如何监控此事件</li>
  132. <li><p>这里还演示了 checkNode / checkAllNodes 方法触发 beforeCheck / onCheck 事件回调函数的情况,试试看:<br/>
  133. &nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="autoCallbackTrigger" /> setting.check.autoCheckTrigger: <span id="autoCheckTriggerValue">false</span><br/>
  134. &nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="callbackTrigger" checked /> 执行勾选方法是否触发 callback <br/>
  135. &nbsp;&nbsp;&nbsp;&nbsp;单节点--[ <a id="checkTrue" href="#" title="不想勾选我就不勾选你..." onclick="return false;">勾选</a> ]
  136. &nbsp;&nbsp;&nbsp;&nbsp;[ <a id="checkFalse" href="#" title="不想取消勾选我就不取消你..." onclick="return false;">取消勾选</a> ]
  137. &nbsp;&nbsp;&nbsp;&nbsp;[ <a id="toggle" href="#" title="你想怎样?..." onclick="return false;">勾选 切换</a> ]<br/>
  138. &nbsp;&nbsp;&nbsp;&nbsp;单节点 ( 影响父子 )--[ <a id="checkTruePS" href="#" title="不想勾选我就不勾选你..." onclick="return false;">勾选</a> ]
  139. &nbsp;&nbsp;&nbsp;&nbsp;[ <a id="checkFalsePS" href="#" title="不想取消勾选我就不取消你..." onclick="return false;">取消勾选</a> ]
  140. &nbsp;&nbsp;&nbsp;&nbsp;[ <a id="togglePS" href="#" title="你想怎样?..." onclick="return false;">勾选 切换</a> ]<br/>
  141. &nbsp;&nbsp;&nbsp;&nbsp;全部节点--[ <a id="checkAllTrue" href="#" title="不管你有多NB,统统都要听我的!!" onclick="return false;">勾选</a> ]
  142. &nbsp;&nbsp;&nbsp;&nbsp;[ <a id="checkAllFalse" href="#" title="不管你有多NB,统统都要听我的!!" onclick="return false;">取消勾选</a> ]</p>
  143. </li>
  144. <li><p><span class="highlight_red">使用 zTreeObj.checkNode / checkAllNodes 方法,详细请参见 API 文档中的相关内容</span><br/>
  145. beforeCheck / onCheck log:<br/>
  146. <ul id="log" class="log" style="height:130px;"></ul></p>
  147. </li>
  148. </ul>
  149. </li>
  150. <li class="title"><h2>2、setting 配置信息说明</h2>
  151. <ul class="list">
  152. <li>同 "checkbox 勾选操作" 中的说明</li>
  153. </ul>
  154. </li>
  155. <li class="title"><h2>3、treeNode 节点数据说明</h2>
  156. <ul class="list">
  157. <li>同 "checkbox 勾选操作" 中的说明</li>
  158. </ul>
  159. </li>
  160. </ul>
  161. </div>
  162. </div>
  163. </BODY>
  164. </HTML>