本来以为这个异步加载会很难控制,因为考虑到ztree节点图标的控制,结果并不是那么困难,ztree自己控制图标,你只要在json中设置isParent:true,它自己会识别父节点并控制图标,以下是核心代码:
<!--前台js代码以及html-->
//参数设置
var setting={
async: {
enable: true,
url:"${pageContext.request.contextPath}/page/department_ascyDepartment.do",
autoParam:["id"]
},
simpleDate: {
enable:true,
idKey:"id",
idPKey:"parentId",
rootPid:null
}
};
//一切预备好后初始化树
$(document).ready(function(){
//创建对象
var demoTree = $.fn.zTree.init($("#treeDemo"), setting);
}); .....
<div class="content_wrap">
<div class="zTreeDemoBackground left">
<ul id="treeDemo" class="ztree" style="margin-left:10px;background-color:white;" >
<img alt="请稍后,正在加载数据……" src="<%=basePath%>images/loading.gif"/>
</ul>
</div>
.....
<!--后台代码 json数据封装-->
public void ascyDepartment(){
JSONArray jsonArr=new JSONArray();
try {
pId=getRequest().getParameter("id");
Map<String,String> map=new HashMap<String, String>();
map.put("pId", pId);
List<Department> departs=departmentService.getChildNodes(map);
for(Department depart:departs){
JSONObject json=new JSONObject();
json.put("id",depart.getId());
json.put("name", depart.getName());
json.put("parentId", depart.getParentId());
if(depart.getHasChild()!=null){
json.put("isParent", true);
}
jsonArr.add(json);
}
HttpServletResponse response=getResponse();
response.setCharacterEncoding("utf-8");
response.getWriter().print(jsonArr.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
本文转自:http://blog.sina.com.cn/s/blog_8a97a8d501011ejk.html