从数据库中读取数据列表,通过后台代码将数据转换成树形,代码如下
/**
* 树节点
*
*/
public class TreeNode{
//节点id
private String id;
//父节点id
private String parentId;
//文本
private String text;
//节点状态
private String state = "open";
//是否选中
private Boolean isChecked = false;
//子节点集合
private List<TreeNode> children;
public TreeNode(){}
public TreeNode(String parentId,String id,String text){
this.id = id;
this.parentId = parentId;
this.text = text;
}
public TreeNode(String id,String parentId,String text,String state, Boolean isChecked){
this.id = id;
this.parentId = parentId;
this.text = text;
this.state = state;
this.isChecked = isChecked;
}
/**
* 返回json
* @return
*/
/* public String toJsonString(){
String result = "{"
+ "\"id\" : \"" + id + "\""
+ ", \"text\" : \"" + text + "\"";
if (children != null && children.size() != 0) {
result += ", \"children\" : " + children.toString();
} else {
result += ", \"leaf\" : true";
}
return result + "}";
}*/
//get set
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Boolean getIsChecked() {
return isChecked;
}
public void setIsChecked(Boolean isChecked) {
this.isChecked = isChecked;
}
public List<TreeNode> getChildren() {
return children;
}
public void setChildren(List<TreeNode> children) {
this.children = children;
}
}
/**
* 将list展示成一棵树
*
*/
public class ShowTreeUtils {
/**
* 将list转换成tree
* @param list
* @return
* @throws IOException
*/
public static String showTree(List<TreeNode> menuList) throws IOException{
List<TreeNode> nodeList = new ArrayList<TreeNode>(); //存放父节点的list
for(TreeNode node1 : menuList){
boolean isChildMark = false; //标志该节点是否是子节点,isChildMark为false表示不是子节点
for(TreeNode node2 : menuList){
if(node1.getParentId()!=null && node1.getParentId().equals(node2.getId())){
isChildMark = true;
if(node2.getChildren() == null){
node2.setChildren(new ArrayList<TreeNode>());
}
node2.getChildren().add(node1);
//node2.setState("closed");
break;
}
}
if(!isChildMark){
nodeList.add(node1); //将父节点放到nodeList中
}
}
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(nodeList);
return json;
}
}
//测试类
public class TreeTest {
public static void main(String[] args) throws IOException {
List<TreeNode> menuList = new ArrayList<TreeNode>();
TreeNode root = new TreeNode("000000000000", "500000000000", "公共通讯录目录"); //根节点
TreeNode root1 = new TreeNode("000000000000", "600000000000", "个人通讯录目录"); //根节点
TreeNode b = new TreeNode("500000000000", "63000000000000000001", "青海省民政厅");
TreeNode c = new TreeNode("63000000000000000001", "63010000000000000002", "西宁");
TreeNode d = new TreeNode("63010000000000000002", "63010200000000000004", "城东");
TreeNode e = new TreeNode("63000000000000000001", "63210000000000000011", "海东");
TreeNode f = new TreeNode("63210000000000000011", "63212100000000000012", "平安");
TreeNode g = new TreeNode("63210000000000000011", "63212200000000000013", "民和");
TreeNode h = new TreeNode("63210000000000000011", "63212300000000000014", "乐都");
menuList.add(root);
menuList.add(root1);
menuList.add(b);
menuList.add(c);
menuList.add(d);
menuList.add(e);
menuList.add(f);
menuList.add(g);
menuList.add(h);
//转为json格式
String json = ShowTreeUtils.showTree(menuList);
System.out.println("json:"+json);
}
}