select_menu_checkbox.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <!DOCTYPE html>
  2. <HTML>
  3. <HEAD>
  4. <TITLE> ZTREE DEMO - checkbox select menu</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. <!-- <script type="text/javascript" src="../../../js/jquery.ztree.exedit-3.5.js"></script>-->
  12. <SCRIPT type="text/javascript">
  13. <!--
  14. var setting = {
  15. check: {
  16. enable: true,
  17. chkboxType: {"Y":"", "N":""}
  18. },
  19. view: {
  20. dblClickExpand: false
  21. },
  22. data: {
  23. simpleData: {
  24. enable: true
  25. }
  26. },
  27. callback: {
  28. beforeClick: beforeClick,
  29. onCheck: onCheck
  30. }
  31. };
  32. var zNodes =[
  33. {id:1, pId:0, name:"北京"},
  34. {id:2, pId:0, name:"天津"},
  35. {id:3, pId:0, name:"上海"},
  36. {id:6, pId:0, name:"重庆"},
  37. {id:4, pId:0, name:"河北省", open:true, nocheck:true},
  38. {id:41, pId:4, name:"石家庄"},
  39. {id:42, pId:4, name:"保定"},
  40. {id:43, pId:4, name:"邯郸"},
  41. {id:44, pId:4, name:"承德"},
  42. {id:5, pId:0, name:"广东省", open:true, nocheck:true},
  43. {id:51, pId:5, name:"广州"},
  44. {id:52, pId:5, name:"深圳"},
  45. {id:53, pId:5, name:"东莞"},
  46. {id:54, pId:5, name:"佛山"},
  47. {id:6, pId:0, name:"福建省", open:true, nocheck:true},
  48. {id:61, pId:6, name:"福州"},
  49. {id:62, pId:6, name:"厦门"},
  50. {id:63, pId:6, name:"泉州"},
  51. {id:64, pId:6, name:"三明"}
  52. ];
  53. function beforeClick(treeId, treeNode) {
  54. var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  55. zTree.checkNode(treeNode, !treeNode.checked, null, true);
  56. return false;
  57. }
  58. function onCheck(e, treeId, treeNode) {
  59. var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
  60. nodes = zTree.getCheckedNodes(true),
  61. v = "";
  62. for (var i=0, l=nodes.length; i<l; i++) {
  63. v += nodes[i].name + ",";
  64. }
  65. if (v.length > 0 ) v = v.substring(0, v.length-1);
  66. var cityObj = $("#citySel");
  67. cityObj.attr("value", v);
  68. }
  69. function showMenu() {
  70. var cityObj = $("#citySel");
  71. var cityOffset = $("#citySel").offset();
  72. $("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
  73. $("body").bind("mousedown", onBodyDown);
  74. }
  75. function hideMenu() {
  76. $("#menuContent").fadeOut("fast");
  77. $("body").unbind("mousedown", onBodyDown);
  78. }
  79. function onBodyDown(event) {
  80. if (!(event.target.id == "menuBtn" || event.target.id == "citySel" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
  81. hideMenu();
  82. }
  83. }
  84. $(document).ready(function(){
  85. $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  86. });
  87. //-->
  88. </SCRIPT>
  89. <style type="text/css">
  90. </style>
  91. </HEAD>
  92. <BODY>
  93. <h1>带 checkbox 的多选下拉菜单 -- zTree</h1>
  94. <h6>[ 文件路径: super/select_menu_checkbox.html ]</h6>
  95. <div class="content_wrap">
  96. <div class="zTreeDemoBackground left">
  97. <ul class="list">
  98. <li class="title">&nbsp;&nbsp;<span class="highlight_red">勾选 checkbox 或者 点击节点 进行选择</span></li>
  99. <li class="title">&nbsp;&nbsp;Test: <input id="citySel" type="text" readonly value="" style="width:120px;" onclick="showMenu();" />
  100. &nbsp;<a id="menuBtn" href="#" onclick="showMenu(); return false;">select</a></li>
  101. </ul>
  102. </div>
  103. <div class="right">
  104. <ul class="info">
  105. <li class="title"><h2>实现方法说明</h2>
  106. <ul class="list">
  107. <li>用 checkbox 也可以轻松实现多选的下拉菜单</li>
  108. <li>此 Demo 主要用于 和 其他下拉菜单进行对比</li>
  109. </ul>
  110. </li>
  111. </ul>
  112. </div>
  113. </div>
  114. <div id="menuContent" class="menuContent" style="display:none; position: absolute;">
  115. <ul id="treeDemo" class="ztree" style="margin-top:0; width:180px; height: 300px;"></ul>
  116. </div>
  117. </BODY>
  118. </HTML>