美文网首页
Java生成JQuery EasyUI的Tree

Java生成JQuery EasyUI的Tree

作者: ABiaoyo | 来源:发表于2017-03-24 17:42 被阅读76次

直接代码如下:
package z.common.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class AQTreeGenUtil {

public static String gen(List<AQNode> listNode,AQConfig conf) throws JsonProcessingException{
    
    Map<String, Object> map = new HashMap<String, Object>();
    for (AQNode node : listNode) {
        AQNode pNode = (AQNode) map.get("n" + node.getP_id());
        if (pNode != null) {
            if (pNode.getChildren() == null) {
                pNode.setChildren(new ArrayList<AQNode>());
            }
            pNode.getChildren().add(node);
        }
        map.put("n" + node.getId(), node);
    }
    AQNode rootNode = (AQNode) map.get("n"+conf.getRootId());

    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(rootNode);
    
    return json;
}

}

class AQConfig{
private int rootId;

public int getRootId() {
    return rootId;
}
public void setRootId(int rootId) {
    this.rootId = rootId;
}

}

class AQNode{
private int id;
@JsonProperty("text")
private String name;
@JsonIgnore
private int p_id;

private List<AQNode> children;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getP_id() {
    return p_id;
}
public void setP_id(int p_id) {
    this.p_id = p_id;
}
public List<AQNode> getChildren() {
    return children;
}
public void setChildren(List<AQNode> children) {
    this.children = children;
}

}

@Test
public void testMain0() throws JsonProcessingException {
List list = new ArrayList<AQNode>();

    AQNode node1 = new AQNode();
    node1.setId(1);
    node1.setName("系统管理");
    list.add(node1);

    node1 = new AQNode();
    node1.setId(2);
    node1.setName("用户管理");
    node1.setP_id(1);
    list.add(node1);

    node1 = new AQNode();
    node1.setId(3);
    node1.setName("添加用户");
    node1.setP_id(2);
    list.add(node1);

    node1 = new AQNode();
    node1.setId(4);
    node1.setName("菜单管理");
    node1.setP_id(1);
    list.add(node1);

    node1 = new AQNode();
    node1.setId(5);
    node1.setName("删除菜单");
    node1.setP_id(4);
    list.add(node1);

    AQConfig conf = new AQConfig();
    conf.setRootId(1);
    
    String json = AQTreeGenUtil.gen(list,conf);
    System.out.println(json);
}

运行结果:
{"id":1,"children":[{"id":2,"children":[{"id":3,"children":null,"text":"添加用户"}],"text":"用户管理"},{"id":4,"children":[{"id":5,"children":null,"text":"删除菜单"}],"text":"菜单管理"}],"text":"系统管理"}

相关文章

网友评论

      本文标题:Java生成JQuery EasyUI的Tree

      本文链接:https://www.haomeiwen.com/subject/lamfottx.html