java easyui tree_Java生成JQuery EasyUI的Tree

直接代码如下:

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 listNode,AQConfig conf) throws JsonProcessingException{

Map map = new HashMap();

for (AQNode node : listNode) {

AQNode pNode = (AQNode) map.get("n" + node.getP_id());

if (pNode != null) {

if (pNode.getChildren() == null) {

pNode.setChildren(new ArrayList());

}

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 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 getChildren() {

return children;

}

public void setChildren(List children) {

this.children = children;

}

}

@Test

public void testMain0() throws JsonProcessingException {

List list = new ArrayList();

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":"系统管理"}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值