js 树菜单 ztree

官网 api


js
  1. /**
  2. <div id="menuContent" class="menuContent" style=" position: absolute;">
  3. <ul id="orgTree" class="ztree" style="margin-top:0; width:160px;"></ul>
  4. </div>
  5. */
  6. //var allowClick = true; //标记节点是否能被点击
  7. var sdorg={};
  8. /**
  9. * 初始化组织机构树
  10. * renderId 渲染位置
  11. * nodeClick 点击时触发的事件
  12. * beforeNodeClick 点击前触发的事件
  13. * ischkbox 是否显示多选框
  14. */
  15. sdorg.showcurrorg =function (renderId,nodeClick,beforNodeClick,ischkbox) {
  16. var orgNodes;
  17. var setting = {
  18. data:{
  19. simpleData:{
  20. enable:true,
  21. idKey:"id",
  22. pIdKey:"parentId",
  23. root:0
  24. },
  25. key:{
  26. name:"orgName"
  27. }
  28. },
  29. view:{
  30. showIcon:true
  31. },
  32. check:{
  33. enable:ischkbox
  34. ,chkStyle:'checkbox'
  35. ,chkboxType:{ "Y": "", "N": "" }
  36. //,autoCheckTrigger: false
  37. },
  38. async:{
  39. enable:true,
  40. url:baseURL+"/org/org!listOrgNode.action",
  41. autoParam:["id"],
  42. dataFilter:ajaxDataFilter
  43. },
  44. callback:{
  45. // onExpand:expandParentOrg,
  46. onClick:nodeClick||_nodeClick,
  47. beforeClick:beforNodeClick||_beforNodeClick,
  48. }
  49. };
  50. var url =baseURL+'/org/org!listOrgRoot.action';
  51. $.ajax({
  52. url:url,
  53. type:'post',
  54. dataType:'json',
  55. success:function (result) {
  56. if ('0' == result.code) {
  57. $.fn.zTree.init($('#'+renderId), setting, null);
  58. var orgTree = $.fn.zTree.getZTreeObj(renderId);
  59. orgTree.addNodes(null, result.data.orgList );
  60. // 展开机构树的根节点
  61. var rootNodes = orgTree.getNodes();
  62. if (rootNodes.length > 0)
  63. orgTree.expandNode(rootNodes[0], true, false, true);
  64. //orgTree.expandAll(true);
  65. } else {
  66. alert(result.desc);
  67. }
  68. }
  69. });
  70. }
  71. sdorg.showcurrorg1 =function (renderId,nodeClick,nodeCheck,ischkbox,beforeShow,onExpand,orgId,orgName) {
  72. var orgNodes;
  73. var setting = {
  74. data:{
  75. simpleData:{
  76. enable:true,
  77. idKey:"organizationId",
  78. pIdKey:"parentId",
  79. root:0
  80. },
  81. key:{
  82. name:"organizationName"
  83. }
  84. },
  85. view:{
  86. showIcon:true
  87. },
  88. check:{
  89. enable:ischkbox
  90. ,chkStyle:'checkbox'
  91. ,chkboxType: { "Y": "s", "N": "s" }
  92. ,autoCheckTrigger: true
  93. },
  94. async:{
  95. enable:true,
  96. url:baseURL+"/org/org!listOrgNode.action",
  97. autoParam:["organizationId"],
  98. dataFilter:ajaxDataFilter
  99. },
  100. callback:{
  101. // onExpand:expandParentOrg,
  102. onClick:nodeClick||_nodeClick,
  103. onCheck:nodeCheck,
  104. onExpand:onExpand
  105. }
  106. };
  107. //var url = cur_user_id=="admin"?baseURL+'/orgext/orgext!listOrgRoot.action':baseURL+'/orgext/orgext!listOrgUnderCurrentUserOrg.action';
  108. var url = orgId?baseURL+'/org/org!listOrgNode.action':baseURL+'/org/org!listOrgRoot.action';
  109. // alert(orgId); return;
  110. $.ajax({
  111. url:url,
  112. type:'post',
  113. dataType:'json',
  114. data:{organizationId:orgId},
  115. success:function (result) {
  116. if ('0' == result.code) {
  117. $.fn.zTree.init($('#'+renderId), setting, null);
  118. var orgTree = $.fn.zTree.getZTreeObj(renderId);
  119. if(orgId){
  120. var newNode = {organizationId:orgId,organizationName:orgName};
  121. orgTree.addNodes(null, newNode);
  122. orgTree.addNodes(orgTree.getNodeByParam("organizationId",orgId,null), result.data.orgList );
  123. }else{
  124. orgTree.addNodes(null, result.data.orgList );
  125. }
  126. // 展开机构树的根节点
  127. var rootNodes = orgTree.getNodes();
  128. if (rootNodes.length > 0)
  129. orgTree.expandNode(rootNodes[0], true, false, true);
  130. //orgTree.expandAll(true);
  131. beforeShow&&beforeShow.call();
  132. } else {
  133. alert(result.desc);
  134. }
  135. }
  136. });
  137. }
  138. sdorg.showchildorg= function (renderId,orgId,nodeClick,beforNodeClick,ischkbox,iscanexpand) {
  139. var orgNodes;
  140. var setting = {
  141. data:{
  142. simpleData:{
  143. enable:true,
  144. idKey:"organizationId",
  145. pIdKey:"parentId",
  146. root:0
  147. },
  148. key:{
  149. name:"organizationName"
  150. }
  151. },
  152. view:{
  153. showIcon:true
  154. },
  155. check:{
  156. enable:ischkbox
  157. ,chkStyle:'checkbox'
  158. ,chkboxType:{ "Y": "", "N": "" }
  159. //,autoCheckTrigger: false
  160. },
  161. async:{
  162. enable:iscanexpand||false,
  163. url:baseURL+"/orgext/orgext!listOrgNode.action",
  164. autoParam:["organizationId"],
  165. dataFilter:ajaxDataFilter
  166. },
  167. callback:{
  168. // onExpand:expandParentOrg,
  169. onClick:nodeClick||_nodeClick,
  170. beforeClick:beforNodeClick||_beforNodeClick,
  171. }
  172. };
  173. var url = orgId?baseURL+'/orgext/orgext!listOrgNode.action':baseURL+'/orgext/orgext!listOrgRoot.action';
  174. $.ajax({
  175. url:url,
  176. type:'post',
  177. data:{organizationId:orgId},
  178. dataType:'json',
  179. success:function (result) {
  180. if ('0' == result.code) {
  181. orgNodes =ajaxDataFilter(null, null, result); //result.data.orgList;
  182. if (orgNodes.length > 0) {
  183. $.fn.zTree.init($('#'+renderId), setting, orgNodes);
  184. // 展开机构树的根节点
  185. var orgTree = $.fn.zTree.getZTreeObj(renderId);
  186. var rootNodes = orgTree.getNodes();
  187. if ((!orgId||iscanexpand)&&rootNodes.length > 0)
  188. orgTree.expandNode(rootNodes[0], true, false, true);
  189. //orgTree.expandAll(true);
  190. }
  191. } else {
  192. alert(result.desc);
  193. }
  194. }
  195. });
  196. }
  197. //
  198. /**
  199. * 异步加载节点数据后的处理函数
  200. * @param treeId
  201. * @param parent
  202. * @param result
  203. */
  204. function ajaxDataFilter(treeId, parent, result) {
  205. //if ('0' == result.code) {
  206. return result.data.orgList;
  207. //}
  208. }
  209. function _nodeClick(event, treeId, treeNode) {
  210. //console.info(treeNode);
  211. //alert(treeNode.organizationId);
  212. //alert(treeNode.organizationName);
  213. }
  214. /**
  215. * 控制节点是否能被选中
  216. * @param treeId
  217. * @param treeNode
  218. * @param clickFlag
  219. */
  220. function _beforNodeClick(treeId, treeNode, clickFlag) {
  221. }
  222. //当前对应input框 显示的input 和要隐藏id的input
  223. var $_currdom2,$_hiddendom2;
  224. //显示下拉树
  225. function showMenu2(inputid,hiddenid) {
  226. $_currdom2 = $("#"+inputid);
  227. $_hiddendom2 = $("#"+hiddenid);
  228. var cityOffset = $_currdom2.offset();
  229. $("#Content").css({left:cityOffset.left, top:cityOffset.top-50}).slideDown("fast");
  230. $("body").bind("mousedown", onBodyDown2);
  231. }
  232. //隐藏下拉树
  233. function hideMenu2() {
  234. $("#Content").fadeOut("fast");
  235. $("body").unbind("mousedown", onBodyDown2);
  236. }
  237. //点击页面其他位置时,下拉树退出
  238. function onBodyDown2(event) {
  239. if (!(event.target.id == "Btn" || event.target.id == "Content" || $(event.target).parents("#Content").length>0)) {
  240. hideMenu2();
  241. }
  242. }
  243. //节点点击事件
  244. function onClick2(e, treeId, treeNode) {
  245. $_currdom2.val(treeNode.organizationName);
  246. $_currdom2.focus();
  247. $_hiddendom2.val(treeNode.organizationId);
  248. $("#Content").fadeOut("fast");
  249. }
  250. function onCheck(e,treeId,treeNode){
  251. var treeObj = $.fn.zTree.getZTreeObj("org");
  252. var $ckNode = treeObj.getCheckedNodes();
  253. var orgName = "";
  254. var orgId = "";
  255. $($ckNode).each(function(){
  256. orgName+=this.organizationName+",";
  257. orgId+=this.organizationId+",";
  258. })
  259. $_currdom2.val(orgName);
  260. $_hiddendom2.val(orgId);
  261. }

html
  1. <!-- 组织架构树 -->
  2. <div id="Content" class="menuContent" style="position:absolute;display:none;z-index:10000;">
  3. <ul id="org" class="ztree" style="margin-top:0; width:350px; background:white; border:2px dotted #cccccc;"></ul>
  4. </div>

posted on 2017-03-22 14:38  signheart  阅读(475)  评论(0编辑  收藏  举报

导航