tree数据类型的介绍来自Easyui Tree树 EasyUi插件
[{
"id":1,
"text":"Folder1",
"iconCls":"icon-save",
"children":[{
"text":"File1",
"checked":true
},{
"text":"Books",
"state":"open",
"attributes":{
"url":"/demo/book/abc",
"price":100
},
"children":[{
"text":"PhotoShop",
"checked":true
},{
"id": 8,
"text":"Sub Bookds",
"state":"closed"
}]
}]
},{
"text":"Languages",
"state":"closed",
"children":[{
"text":"Java"
},{
"text":"C#"
}]
}]
使用递归获取其child集合
//因为数据库中的类型与ztree的不一致,调用的接口是之前的代码接口,有多余的字段,这里写一个转换类,去掉无用字段
private List single2Tree(List lists) {
List treeList = new ArrayList();
if (!(lists == null) && !lists.isEmpty()) {
for (EntityStandardCCLClassify singleStandardICSClassify : lists) {
SingleStandardICCLClassifyForTree tempTree = new SingleStandardICCLClassifyForTree();
tempTree.setId(singleStandardICSClassify.getCCLNo());
tempTree.setText(singleStandardICSClassify.getCCLName());
treeList.add(tempTree);
}
}
return treeList;
}
@Override
public List getZUITree(String parentCCLNo) {
//定义方法返回类型treeList
List treeList = new ArrayList();
List treeParent = new ArrayList();
if (null == parentCCLNo || "".equals(parentCCLNo)) {
//dao.selectLevel()的作用是查询最外层的层级,也就是第一层
treeParent = single2Tree(dao.selectLevel());
}else {
//dao.selectSecondary(parentCCLNo)的作用是查询parentCCLNo的子级
treeParent = single2Tree(dao.selectSecondary(parentCCLNo));
}
if (treeParent != null && !treeParent.isEmpty()) {
for (SingleStandardICCLClassifyForTree singleStandardICCLClassifyForTree : treeParent) {
List chileTree = single2Tree(dao.selectSecondary(singleStandardICCLClassifyForTree.getId()));
//数据库中没有最后一层级的标志,这里根据的是:如果根据当前的id查不到子级,那么他就是最后一级
if (null !=chileTree && !chileTree.isEmpty()) {
//如果不是最后一级,继续调用getZUITree()方法,并将查询到的值赋值给其父级的child属性
singleStandardICCLClassifyForTree.setChildren(getZUITree(singleStandardICCLClassifyForTree.getId()));
}
treeList.add(singleStandardICCLClassifyForTree);
}
}
return treeList;
}
封装了easyui-tree的部分属性,可以根据需求自行添加
import java.util.ArrayList;
import java.util.List;
public class SingleStandardICCLClassifyForTree
{
private String id;
private String text;
//注意这里
private List children = new ArrayList<>(0);
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public List getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
}
$("#test").tree({
url:"${webUrlPath}standardCCL/searchByCCLNoOfMy.action",
checkbox:false,
animate:false,
lines:false,
onLoadSuccess:function(node,data){
//设置选中
var nodeSelect = $('#test').tree('find', '${sacid}');
$(nodeSelect.target).addClass("tree-node-selected theme-color");
parentAll = nodeSelect.text;
expandParent($('#test'), nodeSelect);
},
onClick:function(node){
var id = node.id;
var searchType = "";
if(isMajor == "0"){
subSAC(node.text,id,isMajor,8);
}else{
subSAC(node.text,id,isMajor,5);
}
},
});