ztree的树形菜单

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

<script type="text/javascript">
		// ztree组树配置
		var groupTree_setting = {
			data: {
				simpleData: {
					enable: true,
					idKey: 'groupId',
					pIdKey: 'parentGroupId'
				}
			},
			view: {
				showTitle: false,
				expandSpeed: ""
			},check: {
				enable: true,
				chkStyle: "radio",
				radioType: "all"
			},
			callback: {onClick: clickGroupNode, onCheck: clickGroupNode}
		};
		// ztree菜单树配置
		var setting = {
			data: {
				key:{name: 'menuCnName'},
				simpleData: {
					enable: true,
					idKey: 'menuId',
					pIdKey: 'parentMenuId'
				}
			},
			view: {
				showTitle: false,
				expandSpeed: ""
			},
			callback: {onClick: clickTreeNode}
		};
		$(function(){
			// 加载当前用户所拥有的菜单
			loadUserMenu();
			// 选项卡右键菜单事件
			tabCloseEven();
		});
		// 单击树节点动作
		function clickTreeNode(event, treeId, treeNode) {
			// 处理特殊上下文,是否新打开窗口
			var isRedirect = treeNode.menuAction.indexOf("redirect");
			if(isRedirect == 0) {// 以redirect开头的菜单打开新窗口
				var url = treeNode.menuAction.substring(isRedirect + 9, treeNode.menuAction.length);
				window.open(url, treeNode.menuCnName);
			} else {
				// 点击叶子节点时进行跳转操作
				if(!treeNode.isParent) {
					openTab(treeNode);
				}
			}
		}
		var systemSign;// 系统标识
		var systemIndex;// 首页
		var systemMsg;// 提示信息
		var isShow = false;//提示标志
		var formGroupId;// 上次选择组id
		var groupTreeObj;
		var groupIdList;
		// 加载用户所拥有的系统权限
		function loadUserMenu() {
			// 导航菜单动作		        
			$("#navList").accordion({animate:false});
			// 动态加载菜单数据
	        $.post(
				"/smp/manager-menu/manager-menu!getSystemMenu.action",
			    function(data){
				    if(typeof(data) == 'string'){// 系统超时
						if(data.indexOf('j_spring_cas_security_check') >0 ){
							window.location.reload();
						}
					}
			    	$.each(data, function(i, menu) {
			    		// 拼接左侧菜单
			    	 	var treeid = 'menu_' + menu.systemSign;
			    		var menulist = '<ul><li id=' + treeid + ' class="ztree"></li></ul>';
			    		var divid = 'div' + treeid;
			    		var addDiv = '<div id=' + divid + ' class="expand"></div>'
						// 添加系统菜单列表
						$('#navList').accordion('add', {
				            title: menu.menuName,
				            content: addDiv + menulist,
				            iconCls: 'icon icon-sys'
				        });
				    });
				    if(data == '') {
				    	return;
				    } else if(data.length == 1) {
					    var urlPath = data[0].context;
					    if(window.location.href.indexOf(".corp") > 0) {
							if(urlPath.indexOf(".com.cn") > 0) {
								urlPath = urlPath.replace(".com.cn", ".corp");
							} else if(urlPath.indexOf(".cn") > 0) {
								urlPath = urlPath.replace(".cn", ".corp");
							}
						}
				    	// 慧正系统跳转
				    	if(data[0].systemSign == constants.huizheng) {
				    		window.location.href = urlPath;
				    	}else if(data[0].systemSign == constants.callcenter){
				    		//========= callcenter ==========
				    		window.location.href = urlPath;
				    	}
				    	// 只有一个系统使用自带的onSelect事件
				    	$("#navList").accordion({onSelect:changeSystem});
				    } else {
				    	// 点击左侧系统列表后触发事件(替换自带的onSelect事件,初始只加载一次)
						$("div.panel-header.accordion-header").find("a").click(function(){
							// 菜单导航展开后切换系统
							var systemName = $('div.accordion-header.accordion-header-selected').children(".panel-title").html();
							changeSystem(systemName);
						});
				    }
				    // 默认展开第一个
					var panels = $('#navList').accordion('panels');
					var t = panels[0].panel('options').title;
				    $('#navList').accordion('select', t);
			    }
			);
			// 切换系统
			function changeSystem(systemName) {
				if(null != systemName) {
					// 切换系统名称
					$('#logo').html(systemName);
					$.post(
						"/smp/manager-menu/manager-menu!getSystemContext.action",
						{systemName:systemName},
					     function(data) {
						    if(typeof(data) == 'string'){// 系统超时
								if(data.indexOf('j_spring_cas_security_check') >0 ){
									window.location.reload();
								}
							}
						     // 切换组显示菜单
					     	systemSign = data.systemSign;
					     	systemIndex = data.systemIndex;
					     	systemMsg = data.systemMsg;
					     	if(data.groupList.length > 0) {// 当前员工有组信息
					     		groupTreeObj = $.fn.zTree.init($("#groupTree"), groupTree_setting, data.groupList);
					     		groupIdList = data.userGroup.split(',');
					     		var groupId;
					     		if(groupIdList.length == 1) {
					     			var curNode = groupTreeObj.getNodesByParam("groupId", groupIdList[0], null);
					     			groupId = curNode[0].groupId;
					     			$('#group').val(curNode[0].name);
					     		} else {
					     			var curNode = groupTreeObj.getNodesByParam("groupId", -1, null);
					     			groupId = curNode[0].groupId;
					     			$('#group').val(curNode[0].name);
					     		}
					     		// 除叶子节点,其他不可选
					     		var nodes = groupTreeObj.getNodesByParamFuzzy("name", "", null);
					    		if(nodes != '') {
						    		for (var i=0, l=nodes.length; i < l; i++) {
						    			groupTreeObj.setChkDisabled(nodes[i], true);
									}
									for (var m=0, n=groupIdList.length; m < n; m++) {
										var curNode = groupTreeObj.getNodesByParam("groupId", groupIdList[m], null);
										groupTreeObj.setChkDisabled(curNode[0], false);
									}
									var curNode = groupTreeObj.getNodesByParam("groupId", -1, null);
									groupTreeObj.setChkDisabled(curNode[0], false);
								}
								getMenuByGroupId(groupId);
					     	}
						}
					);
				}
			}
		}
		// 获取组下菜单
		function getMenuByGroupId(groupId) {
			$.post(
				"/smp/manager-menu/manager-menu!getManagerMenu.action",
				{systemSign:systemSign,groupId:groupId},
				function(data) {
					if(typeof(data) == 'string'){// 系统超时
						if(data.indexOf('j_spring_cas_security_check') >0 ){
							window.location.reload();
						}
					} else {
						// 记录上次选择的组id
						formGroupId = groupId;
						// 关闭所有tab页,切换首页
					    $('.tabs-inner span').each(function(i,n){
							var t = $(n).text();
							$('#tabs').tabs('close',t);
						});
						// 没有配置首页不打开tab页
						if(systemIndex.indexOf("#") != (systemIndex.length-1)) {
	    					openTabOrRefresh(parent.creatTabObj("首页", systemIndex));
	    				}
	    				// 首页增加提示信息
	    				if(systemMsg != '' && isShow == false) {
						    // 主框架提示信息
						    $.messager.show({
								title:'提示',msg:systemMsg,
								timeout:0,showType:'slide'
							});
							// 只提示一次
							isShow = true;
						}
						// 加载菜单
						var treeid = 'menu_' + systemSign;
	     				$.fn.zTree.init($("#" + treeid), setting, data);
	     				var html = '<img id="img_'+ treeid +'" src="resourse/images/btn_expand.gif" title="全部展开" onclick="expandAll('+ treeid +')"></img>';
	     				$('#div' + treeid).html(html);
	     			}
				}
			);
		}
		// 切换组
		function changeGroup() {
			var groupOffset = $("#group").offset();
			$("#groupDiv").css({left:groupOffset.left + "px", top:groupOffset.top + $("#group").outerHeight() + "px"}).slideDown(0);
			$("body").bind("mousedown", onBodyDown);
			// 树节点全部展开
			groupTreeObj.expandAll(true);
			var nodes = groupTreeObj.getNodesByParam("groupId", formGroupId, null);
    		if(nodes != '') {
    			// 选中并展开选中级别节点
    			var node = nodes[0];
    			groupTreeObj.checkNode(node, true);
    		}
		}
		function hideMenu() {
			$("#groupDiv").fadeOut(0);
			$("body").unbind("mousedown", onBodyDown);
		}
		function onBodyDown(event) {
			if (!(event.target.id == "group" || $(event.target).parents("#groupDiv").length > 0)) {
				hideMenu();
			}
		}
		// 点击组树
		function clickGroupNode(event, treeId, treeNode) {
			for (var m=0, n=groupIdList.length; m < n; m++) {
   				if(treeNode.groupId == groupIdList[m] || treeNode.groupId == -1) {
   					hideMenu();
					if(formGroupId == treeNode.groupId) {//下拉选择的组没有变化
						return;
					} else {
						formGroupId = treeNode.groupId;
						$('#group').val(treeNode.name);
						getMenuByGroupId(formGroupId);
					}
   				}
			}
		}
		// 全部展开关闭功能
		function expandAll(treeid) {
			var src = $('#img_' + treeid.id).attr('src');
			if(src.indexOf('expand') > 0) {
				src = src.replace('expand', 'collapse');
				$('#img_' + treeid.id).attr('title', "全部关闭");
				$.fn.zTree.getZTreeObj(treeid.id).expandAll(true);
			} else {
				src = src.replace('collapse', 'expand');
				$('#img_' + treeid.id).attr('title', "全部展开");
				$.fn.zTree.getZTreeObj(treeid.id).expandAll(false);
			}
			$('#img_' + treeid.id).attr('src', src);
		}
		// 修改密码
		function modifyPassword() {
			$('#empPwdWin')[0].style.display = "block";
			$('#empPwdWin').window('open');
		}
		// 取消密码重置
		function empCancelReset() {
			$('#empPwdForm')[0].reset();
			$('#empPwdWin').window('close');
		}
		// 密码重置
		function empPwdReset() {
			// 校验表单是否合法
			if($('#empPwdForm').form('validate')) {
				// 提交表单
				$('#empPwdForm').form('submit', {   
					url:"/smp/employee/employee!modifyPassword.action",
					success:function(result){
						if(result == 'success') {
							$('#empPwdForm')[0].reset();
							$('#empPwdWin').window('close');
							$.messager.alert('提示','密码修改成功!');
						} else {
							$.messager.alert('提示','密码修改失败!');
						}
					}   
				});  
			}
		}
		// 退出系统
		function logout() {
			$.messager.confirm('提示', '您确定要退出当前系统?', function(btn){
				if(btn) {
					$.post(//清空当前员工组映射
						"/smp/manager-menu/manager-menu!removeGroupIdMap.action",
						function(data) {
							window.location.href = "/smp/j_spring_security_logout";
						}
					);
				}
			});
		}
	</script>