ztree的json格式:(simpleData)
var zNodes =[ { id:1, pId:0, name:"父节点 1", open:true}, { id:11, pId:1, name:"叶子节点 1-1"}, { id:12, pId:1, name:"叶子节点 1-2"}, { id:13, pId:1, name:"叶子节点 1-3"}, { id:2, pId:0, name:"父节点 2", open:true}, { id:21, pId:2, name:"叶子节点 2-1"}, { id:22, pId:2, name:"叶子节点 2-2"}, { id:23, pId:2, name:"叶子节点 2-3"}, { id:3, pId:0, name:"父节点 3", open:true}, { id:31, pId:3, name:"叶子节点 3-1"}, { id:32, pId:3, name:"叶子节点 3-2"}, { id:33, pId:3, name:"叶子节点 3-3"} ];
要生成这样的json 数据 不能被 id 于pId 之间的关系给迷糊了。
java文件递归:
public int fullData(File root , List<SimpleData> breach, int pid, int id) {
File[] fileList = root.listFiles();
int len = fileList.length;
int fatherpid = pid;
int sonid = id;
String path ="";
for (int i = 0; i < len; i++) {
if (fileList[i].isDirectory()) {
//List<SimpleData> breach = new ArrayList<SimpleData> ();
SimpleData bsd = new SimpleData();
bsd.setpId(fatherpid);
bsd.setId(sonid);
bsd.setName(fileList[i].getName());
path = fileList[i].getAbsolutePath().substring(rootLen);
String path1 = path.replace("\\", "/");
bsd.setPath(path1);
breach.add(bsd);
sonid = fullData(fileList[i], breach, sonid, sonid+1);
} else {
SimpleData leaf = new SimpleData();
leaf.setpId(fatherpid);
leaf.setId(sonid);
leaf.setName(fileList[i].getName());
path = fileList[i].getAbsolutePath().substring(rootLen);
String path1 = path.replace("\\", "/");
leaf.setPath(path1);
breach.add(leaf);
sonid++;
}
}
return sonid;
}
这样 产生的数据 id是根据文件的个数来的
如果ztree 不是用了遍历文件夹,而是遍历大量数据的数据库话,那就不能用递归了,直接根据数据库的关系 遍历出来即可,只好是做异步加载