$(document).ready(function(){ var treeObj = $("#treeDemo"); $.fn.zTree.init(treeObj, setting, zNodes); department_select();//页面加载完成执行方法 }); /*ztree实现下拉菜单*/ var setting = { data: { simpleData: { enable: true, idKey: "id", pIdKey: "pId", rootPId: 0 } } }; /*通过查询area确定区域,根据区域id确定下面的部门*/ /*将数据库里面的部门信息查询出来,循环显示出来*/ /*获取区域id和区域名称*/ var arr_areaid = Array(); var arr_areaname = Array(); var url="{:U('area/select_area')}";//ajax传值路径 $.ajax({ type:'post', data:"", url:url, async:false,//设置ajax同步和 异步,如果想在ajax外面用数据,需要将其设置为同步,运行完成之后才能用数据 dataType:"json", success: function (data) { for(var i = 0;i < data.length;i++){ arr_areaid[i] = data[i].areaid; arr_areaname[i] = data[i].areaname; } } }); // console.log("!!!"+arr_areaid);//区域id // console.log("!!!"+arr_areaname);//区域名称 /*获取部门名称以及所属于的区域id*/ var arr_departmentname = Array(); var arr_department_areaid = Array(); var url="{:U('department/select_department')}";//ajax传值路径 $.ajax({ type:'post', data:"", url:url, async:false, dataType:"json", success: function (data) { for(var i = 0;i < data.length;i++){ arr_departmentname[i] = data[i].departmentname; arr_department_areaid[i] = data[i].areaid } } }); // console.log(arr_departmentname); // console.log(arr_department_areaid); var zNodes =[]; for(var i = 0;i < arr_areaid.length;i++){ zNodes.push({ id:arr_areaid[i], pId:0, name:arr_areaname[i], open:true,iconClose:"__PUBLIC__img/user/12.png",iconOpen:"__PUBLIC__img/user/13.png"}); for(var j = 0;j < arr_departmentname.length;j++){ if(arr_areaid[i] == arr_department_areaid[j]){ zNodes.push({ id:(""+i+13+j), pId:arr_areaid[i], name:arr_departmentname[j],icon:"__PUBLIC__img/user/14.png"}) ; } } }