"menuData":[{"activity":"0","code":"01","enName":"System Info","id":1680,"link":"0","name":"系统信息","parentid":"00","photo":"images/miniicons/page_tick.gif","remark":"null"},{"activity":"0","code":"02","enName":null,"id":1707,"link":"0","name":"企业信息管理","parentid":"00","photo":"0","remark":"0"},{"activity":"0","code":"03","enName":"0","id":1732,"link":"0","name":"企业总览","parentid":"00","photo":"0","remark":"0"},{"activity":"0","code":"05","enName":"0","id":1763,"link":"0","name":"能源统计与分析","parentid":"00","photo":"0","remark":"0"},{"activity":"0","code":"07","enName":"0","id":1782,"link":"0","name":"节能目标管理","parentid":"00","photo":"0","remark":"0"},{"activity":"0","code":"08","enName":"0","id":3771,"link":"0","name":"设备管理与分析","parentid":"00","photo":"0","remark":"0"},{"activity":"0","code":"10","enName":"0","id":1804,"link":"0","name":"系统建模","parentid":"00","photo":"0","remark":"0"},{"activity":"0","code":"11","enName":"0","id":1811,"link":"0","name":"文件信息发布","parentid":"00","photo":"0","remark":"0"},{"activity":"0","code":"12","enName":"0","id":1830,"link":"0","name":"能源GIS监控","parentid":"00","photo":"0","remark":"0"},{"activity":"0","code":"13","enName":"0","id":3826,"link":"0","name":"能耗报警","parentid":"00","photo":"0","remark":"0"},{"activity":"0","code":"14","enName":"0","id":3829,"link":"0","name":"能源评价预测","parentid":"00","photo":"0","remark":"0"},{"activity":"0","code":"15","enName":"0","id":3867,"link":"0","name":"能耗报告","parentid":"00","photo":"0","remark":"0"},{"activity":"0","code":"17","enName":"0","id":3935,"link":"0","name":"页面","parentid":"00","photo":"0","remark":"0"},{"activity":"0","code":"18","enName":"0","id":4059,"link":"0","name":"高级分析","parentid":"00","photo":"0","remark":"0"},{"activity":"0","code":"16","enName":"0","id":4047,"link":"0","name":"环境管理","parentid":"00","photo":"0","remark":"0"},{"activity":"0","code":"23","enName":"0","id":4016,"link":"0","name":"桥吊分析","parentid":"00","photo":"0","remark":"0"},{"activity":"0","code":"24","enName":"0","id":4017,"link":"0","name":"龙门吊分析","parentid":"00","photo":"0","remark":"0"},{"activity":"0","code":"25","enName":"0","id":4018,"link":"0","name":"油机分析","parentid":"00","photo":"0","remark":"0"}],"result":null,"text":null}
$(function(){ var defaultData = [ { text: '系统信息', href: '#parent1', tags: ['4'], nodes: [ { text: '数据字典', href: '#child1', tags: ['2'] }, { text: '菜单管理', href: '#child2', tags: ['0'] }, { text: '角色管理', href: '#child2', tags: ['0'] }, { text: '用户管理', href: '#child2', tags: ['0'] }, { text: '权限管理', href: '#child2', tags: ['0'] }, { text: '文件管理', href: '#child2', tags: ['0'] }, { text: '系统邮箱', href: '#child2', tags: ['0'] } ] } // { // text: '菜单管理', // href: '#parent2', // tags: ['0'] // }, // { // text: '角色管理', // href: '#parent3', // tags: ['0'] // }, // { // text: '用户管理', // href: '#parent4', // tags: ['0'] // }, // { // text: '权限管理', // href: '#parent5' , // tags: ['0'] // } ]; // $('#treeview').treeview({ // data: defaultData // }); initOneLevel(); //initTwoLevel(); initEvent(); }) Array.prototype.unique4 = function() { var arr = []; var hash = {}; for (var i = 0; i < this.length; i++) { var item = this[i]; var key = typeof(item) + item if (hash[key] !== 1) { arr.push(item); hash[key] = 1; } } return arr; } /** * 初始化一级菜单 */ function initOneLevel(){ $.post("queryMenuData.action", {}, function (data, textStatus){ if(textStatus=="success"){ if(data.result=="noLogin"){window.location.href="login.jsp";} var menudata=data.menuData; // 把返回的数据添加到页面上 var dbName; buildOneNavbar(menudata); }else{ alert("获取数据失败:"+textStatus); return false; } },"json" ); } function buildOneNavbar(data){ var str='<ul class="nav navbar-nav">'; $("#h-navbar").html("");//清空 str+='<li class="cur" data-code="0"> '+ '<a href="#" >主页</a> '+ '<div style="display:block;"></div> '+ '<em class="caret" style="display:block; top: 3px;left: -7px;transform: rotate(-45deg);"></em> '+ '<em class="caret" style="display:block;top: 3px;right: -5px;transform: rotate(45deg);"></em> '+ '</li>'; for(var i=0;i<data.length;i++){ var _name=data[i].name; var _code=data[i].code; str+='<li data-code="'+_code+'"> '+ '<a href="#">'+_name+'</a> '+ '<div></div> '+ '<em class="caret" style="top: 3px;left: -7px;transform: rotate(-45deg);"></em> '+ '<em class="caret" style="top: 3px;right: -5px;transform: rotate(45deg);"></em> '+ '</li>'; } $("#h-navbar").html(str+'</ul>'); } /** * 初始化二级菜单 */ function initTwoLevel(code){ var paramText=""; if(code==0){ //展示主页,左侧的菜单隐藏 $(".content .slider").hide(); }else{ $(".content .slider").show(); $.post("slMenu_queryMenuDataMs.action", {treeCode:code}, function (data, textStatus){ if(textStatus=="success"){ console.log(data); var menuList=data.menuList; //组件tree的数据格式 initData(menuList); }else{ alert("获取数据失败:"+textStatus); return false; } },"json" ); } } function initData(data){ //第一步:构建两个对象 子对象,与父子关系的对象 console.log(data,'data') for(var i=0,childObj={},relatArr=[],relatArr1=[],relatObj={},nodes={nodes:[]};i<data.length;i++){ var _rowData=data[i]; childObj[_rowData.treeCode]=_rowData; relatObj[_rowData.treePCode]=relatData(_rowData.treePCode); relatArr1.push(_rowData.treePCode); if(_rowData.treePCode==='00'){ var _pnode=_rowData.treeCode; if(nodes[_rowData.treeCode]===undefined){ nodes[_rowData.treeCode]={nodes:[]}; } nodes.nodes=nodes[_rowData.treeCode].nodes; }else{ if(nodes[_rowData.treeCode]===undefined){ nodes[_rowData.treeCode]={text:_rowData.name,nodes:[],obj:_rowData}; }else{ nodes[_rowData.treeCode].text=_rowData.name; nodes[_rowData.treeCode].obj=_rowData; } if(nodes[_rowData.treePCode]===undefined){ nodes[_rowData.treePCode]={text:'',nodes:[],obj:{}}; } nodes[_rowData.treePCode].nodes.push(nodes[_rowData.treeCode]); } } console.log(nodes); //去除nodes为空的键 for(var k in nodes){ if(k=='nodes'){continue;} if(nodes[k].nodes.length==0){ delete nodes[k].nodes; } } $('#treeview').treeview({ data: nodes.nodes }); relatArr=relatArr1.unique4();//数组进行去重 console.log(childObj,'childObj'); console.log(relatObj,'relatObj'); //第二步:遍历 var treeArr=[];//最终构建树的数据 for(var j=0;j<relatArr.length;j++){ var relat1=relatArr[j]; //如果relatArr[j]在relatObj里面存在,那么遍历存在键值的数组 if(relatObj[relat1]!==undefined){ var relat2=relatObj[relat1]; for(var k=0;k<relat2.length;k++){ treeArr[j]={ text:childObj[relat2[k]].name } //如果__relat[k]在父子关系的对象中有 if(relatObj[relat2[k]]!==undefined){ var relat3=relatObj[relat2[k]]; treeArr[j].nodes=[] for(var m=0;m<relat3.length;m++){ treeArr[j].nodes[m]={ text:childObj[relat3[m]].name } } } } } } console.log(treeArr); function relatData(pcode){ var arr=[]; for(var m=0;m<data.length;m++){ if(data[m].treePCode==pcode){ arr.push(data[m].treeCode); } } return arr; } } function initEvent(){ //一级菜单的点击事件 $("#h-navbar").on("click","li>a",function(){ //获取到当前点击的文字 initTwoLevel($(this).parent().attr("data-code")); //设置激活 $(this).parent().attr("class","cur").siblings(".cur").removeAttr("class").children(":not(:first)").css("display","none"); $(this).parent().children(":not(:first)").css("display","block"); }) }