dojo1.1树的两种实现——懒加载树(需要时加载)

本文介绍了一种构建JSON格式树状结构的方法,包括根节点及其子节点的数据组织方式。通过具体示例展示了如何使用Java生成包含引用和子节点的复杂树形结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
* @return 类似于这样的节点列表,json格式:<br> { identifier: 'objectId', label: 'title',
* items: [ { "type":"root","title":"1a","objectId":"1","widgetId":"1",
* "children":[ {"_reference":"21"}, {"objectId":"22","_reference":"22"} ] },
* {"type":"stub","title":"21b","objectId":"21","widgetId":"21"},
* {"type":"stub","title":"22b","objectId":"22","widgetId":"22"} ] }
* @auth: hi.baidu.com/javaroad
*/
public String getRoots() throws JSONException {
ITreeFactory factory = (ITreeFactory) CrmContexts.getBean(CrmContexts.getRequestParam("treeId"));

Object[] nodes = factory.getRoots();
JSONArray ret = new JSONArray();
if (nodes == null) {
return ret.toString();
}
for (int i = 0; i < nodes.length; i++) {
if (nodes[i] == null)
continue;
JSONObject obj = new JSONObject();
Object node = nodes[i];
String id = factory.getId(node);
obj.put("objectId", id);
obj.put("type", "root");
obj.put("title", factory.getTitle(node));
if (!factory.isLeaf(node)) {
// 有子编码
Object[] children = factory.getChildren(node);
if (children != null && children.length > 0) {
// 有孩子节点
JSONArray child = new JSONArray();
for (int j = 0; j < children.length; j++) {
JSONObject node1 = new JSONObject();
node1.put("_reference", factory.getId(children[j])); // root使用reference,
// childer使用stub
// node1.put("objectId", factory.getId(children[j]));
child.put(j, node1);
}
obj.put("children", child);

// root需要把各个下级children也加进去
for (int j = 0; j < children.length; j++) {
JSONObject node1 = new JSONObject();
node1.put("objectId", factory.getId(children[j]));
// node1.put("title", factory.getTitle(children[j]));
node1.put("type", "stub");
ret.put(node1);
}
}
}
ret.put(obj);
}

String retu = "{identifier: 'objectId', label: 'title', items: " + ret.toString() + "}";
return retu;
}

/*
* @return 类似于这样的节点列表,json格式:<br> {
* "type":"node","title":"22b","objectId":"22","widgetId":"22", "children":[
* {"stub":"33"}, {"stub":"44"} ] }
* @auth: hi.baidu.com/javaroad
*/
public String getChildren() throws JSONException {
String id = CrmContexts.getRequestParam("objectId");
ITreeFactory factory = (ITreeFactory) CrmContexts.getBean(CrmContexts.getRequestParam("treeId"));
JSONObject obj = new JSONObject();
Object node = factory.getNodeById(id);
obj.put("objectId", id);
obj.put("title", factory.getTitle(node));
obj.put("type", "node");
if (!factory.isLeaf(node)) {
// 有子节点
Object[] children = factory.getChildren(node);
if (children != null && children.length > 0) {
// 有孩子节点
JSONArray child = new JSONArray();
for (int j = 0; j < children.length; j++) {
JSONObject node1 = new JSONObject();
// 这里统一用reference不用stub了
node1.put("stub", factory.getId(children[j]));
child.put(j, node1);
}
obj.put("children", child);
}
}

return obj.toString();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值