select_menu.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <!DOCTYPE html>
  2. <HTML>
  3. <HEAD>
  4. <TITLE> ZTREE DEMO - 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. view: {
  16. dblClickExpand: false
  17. },
  18. data: {
  19. simpleData: {
  20. enable: true
  21. }
  22. },
  23. callback: {
  24. beforeClick: beforeClick,
  25. onClick: onClick
  26. }
  27. };
  28. var zNodes =[
  29. {id:1, pId:0, name:"北京"},
  30. {id:2, pId:0, name:"天津"},
  31. {id:3, pId:0, name:"上海"},
  32. {id:6, pId:0, name:"重庆"},
  33. {id:4, pId:0, name:"河北省", open:true},
  34. {id:41, pId:4, name:"石家庄"},
  35. {id:42, pId:4, name:"保定"},
  36. {id:43, pId:4, name:"邯郸"},
  37. {id:44, pId:4, name:"承德"},
  38. {id:5, pId:0, name:"广东省", open:true},
  39. {id:51, pId:5, name:"广州"},
  40. {id:52, pId:5, name:"深圳"},
  41. {id:53, pId:5, name:"东莞"},
  42. {id:54, pId:5, name:"佛山"},
  43. {id:6, pId:0, name:"福建省", open:true},
  44. {id:61, pId:6, name:"福州"},
  45. {id:62, pId:6, name:"厦门"},
  46. {id:63, pId:6, name:"泉州"},
  47. {id:64, pId:6, name:"三明"}
  48. ];
  49. function beforeClick(treeId, treeNode) {
  50. var check = (treeNode && !treeNode.isParent);
  51. if (!check) alert("只能选择城市...");
  52. return check;
  53. }
  54. function onClick(e, treeId, treeNode) {
  55. var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
  56. nodes = zTree.getSelectedNodes(),
  57. v = "";
  58. nodes.sort(function compare(a,b){return a.id-b.id;});
  59. for (var i=0, l=nodes.length; i<l; i++) {
  60. v += nodes[i].name + ",";
  61. }
  62. if (v.length > 0 ) v = v.substring(0, v.length-1);
  63. var cityObj = $("#citySel");
  64. cityObj.attr("value", v);
  65. }
  66. function showMenu() {
  67. var cityObj = $("#citySel");
  68. var cityOffset = $("#citySel").offset();
  69. $("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
  70. $("body").bind("mousedown", onBodyDown);
  71. }
  72. function hideMenu() {
  73. $("#menuContent").fadeOut("fast");
  74. $("body").unbind("mousedown", onBodyDown);
  75. }
  76. function onBodyDown(event) {
  77. if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
  78. hideMenu();
  79. }
  80. }
  81. $(document).ready(function(){
  82. $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  83. });
  84. //-->
  85. </SCRIPT>
  86. <style type="text/css">
  87. </style>
  88. </HEAD>
  89. <BODY>
  90. <h1>下拉菜单</h1>
  91. <h6>[ 文件路径: super/select_menu.html ]</h6>
  92. <div class="content_wrap">
  93. <div class="zTreeDemoBackground left">
  94. <ul class="list">
  95. <li class="title">&nbsp;&nbsp;<span class="highlight_red">选择城市时,按下 Ctrl 键可以进行多选</span></li>
  96. <li class="title">&nbsp;&nbsp;城市:<input id="citySel" type="text" readonly value="" style="width:120px;"/>
  97. &nbsp;<a id="menuBtn" href="#" onclick="showMenu(); return false;">选择</a></li>
  98. </ul>
  99. </div>
  100. <div class="right">
  101. <ul class="info">
  102. <li class="title"><h2>实现方法说明</h2>
  103. <ul class="list">
  104. <li>用 zTree 实现这种下拉菜单,应该说是比较容易的,你只需要控制 zTree 所在容器的隐藏/显示,以及位置即可。</li>
  105. <li class="highlight_red">zTree v3.x 实现了多点选中功能,因此对于需要多选的下拉菜单也易如反掌。</li>
  106. <li class="highlight_red">利用 setting 的各项配置参数,完全可以满足大部分的功能需求。</li>
  107. </ul>
  108. </li>
  109. </ul>
  110. </div>
  111. </div>
  112. <div id="menuContent" class="menuContent" style="display:none; position: absolute;">
  113. <ul id="treeDemo" class="ztree" style="margin-top:0; width:160px;"></ul>
  114. </div>
  115. </BODY>
  116. </HTML>