處理mysql返回ztree資料格式問題
阿新 • • 發佈:2019-01-22
<TreeListUtil.java> package com.*.utils; import com.jetair.domin.Emp; import org.apache.commons.lang.ObjectUtils; import java.util.ArrayList; import java.util.List; public class TreeListUtil { private static List<Emp> newList; //treeList public TreeListUtil(){ newList = new ArrayList<Emp>(); } /** * 向新list中裝入根節點並遞迴子節點 */ public List<Emp> setRootNode(List treeList){ List<Emp> rootNode = getRootNode(treeList); //獲得根節點 for (int i = 0 ; i < rootNode.size() ; i++) { newList.add(rootNode.get(i)); //存入所有根節點 sortNode(rootNode.get(i) , treeList); } return newList; } /** * 獲得根節點 */ private List<Emp> getRootNode(List treeList){ List<Emp> rootList = new ArrayList<Emp>(); //構造返回list for (int i = 0 ; i < treeList.size() ; i++) { //迴圈讀取pid等於0的根節點 if((Integer)((Emp)treeList.get(i)).getMgr() == 0){ rootList.add((Emp)treeList.get(i)); //放入list中 } } return rootList; //返回list } /** * 遞迴子節點 * @param rootNode 各個根節點 * @param treeList 存放所有節點的list * @return childList */ private List<Emp> sortNode(Emp rootNode , List treeList){ List<Emp> rightList = new ArrayList<Emp>(); List<Emp> childList = getChild(rootNode,treeList); //獲得根節點的子節點 if(childList!=null){ for (int i = 0 ; i < childList.size() ; i++){ newList.add(childList.get(i)); sortNode(childList.get(i) , treeList); //把查出來的子節點當做父節點遞迴查詢 } } return childList; } /** * 獲得根節點的子節點 * @param treeNode 各個根節點 * @param treeList 存放所有節點的list * @return childList */ private List<Emp> getChild(Emp treeNode , List treeList){ List<Emp> childList = new ArrayList<Emp>(); //構建返回list for (int i = 0 ; i < treeList.size() ; i++) { if(treeNode.getEmpno() == ((Emp)treeList.get(i)).getMgr()){ //查詢儲存根節點的子節點 childList.add((Emp)treeList.get(i)); } } return childList; //返回list } }
<Emp.java> package com.*.domin; import java.util.Date; import java.util.List; public class Emp { private int empno; private String ename; private String job; private int mgr; private Date hiredate; private int sal; private int comm; private int deptno; private String level; public String getLevel() { return level; } public void setLevel(String level) { this.level = level; } public int getEmpno() { return empno; } public void setEmpno(int empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public int getMgr() { return mgr; } public void setMgr(int mgr) { this.mgr = mgr; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public int getSal() { return sal; } public void setSal(int sal) { this.sal = sal; } public int getComm() { return comm; } public void setComm(int comm) { this.comm = comm; } public int getDeptno() { return deptno; } public void setDeptno(int deptno) { this.deptno = deptno; } }
<EmpMapper.xml> <resultMap id="treeListMap" type="com.jetair.domin.Emp"> <id column="EMPNO" property="empno"/> <result column="ENAME" property="ename"/> <result column="JOB" property="job"/> <result column="MGR" property="mgr"/> <result column="HIREDATE" property="hiredate"/> <result column="SAL" property="sal"/> <result column="COMM" property="comm"/> <result column="DEPTNO" property="deptno"/> <result column="LEVEL" property="level"/> </resultMap> <select id="getTreeList" resultMap="treeListMap"> SELECT * FROM emp </select>
<getTree.js>
//初始化ztree控制元件
$(function () {
$.fn.zTree.init($("#tree"), setting);
});
var setting={
async: {
enable: true,
type:'post',
url:"http://localhost:8080/handle/getTreeList.do",
dataType : "json"
},
data: {
key : {
name : "ename"
},
simpleData : { //簡單的資料來源
enable : true,
idKey : "empno", // 樹的結構資訊
pIdKey : "mgr",
rootPId : 0 , //根節點
}
},
callback: {
onClick: zTreeOnClick
},
view:{
showIcon: false,
showLine: true
}
};