论坛首页 AJAX版 AJAX

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

浏览 523 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2008-06-24 关键字: dojo tree 树 懒加载
/*
* @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();
}
   
时间:2008-06-25
在页面上是如何实现的?直接通过xhrget吗?
   
0 请登录后投票
时间:2008-06-25
前台页面可以参考dojox\data\demos\demo_LazyLoad.html
及store: dojox\data\demos\stores\LazyLoadJSIStore.js

stroe只需要自己进行一点点扩展
   
0 请登录后投票
时间:2008-08-20
这样的话,我发现了两个问题:
1.当我调用store.deleteItem的时候,除了root跟第一级子结点可以删除外,其它级的结点都不可以删除!请问这是怎么回事呢?
2.当我调用store._getItemByIdentity(id)或是store._getItemByIdentity(id)时,除了root跟第一子结点返回[object,object]外,其它级的结点都返回null,这又是怎么一回事呢??

请指教啊....
   
0 请登录后投票
时间:2008-08-31
我想问一下如果在json文件中出现中文该怎么解决乱码问题
   
0 请登录后投票
论坛首页 AJAX版 AJAX

跳转论坛:
JavaEye推荐