`
sacred02
  • 浏览: 6814 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

dojo1.1树的两种实现——一次性加载整棵树

J# 
阅读更多
/**
* @descrtion: 直接获取树的整个结点, 非懒加载方式, 加上缓存, 根本就不怕它慢. 非懒加载的树主要用于静态的树,例如选择城市等
* @return
* @throws JSONException
* @author hi.baidu.com/javaroad */
public String getTreeRecursive() throws JSONException {
   ITreeFactory factory = (ITreeFactory) CrmContexts.getBean(CrmContexts.getRequestParam("treeId"));
   // 加上缓存
   String key = CrmContexts.getRequestParam("CacheKey");
   Object retRoot = CacheUtil.get("TREEROOT", key);

   if (retRoot == null) {
    JSONArray ret = new JSONArray();
    Object[] objPool = new Object[2000]; // 结点对象池2000个够了
    Object[] nodes = factory.getRoots();
    int index = 0; // 记录结点对象池最后一个元素的位置
    if (nodes == null) {
     return ret.toString();
    }
    for (int i = 0; i < nodes.length; i++) {
     objPool[index++] = nodes[i];
    }

    for (int i = 0; i < objPool.length; i++) {
     Object node = objPool[i];
     if (node == null) {
      continue;
     }
     JSONObject obj = new JSONObject();
     String id = factory.getId(node);
     obj.put("objectId", id);
     if (i < nodes.length) {
      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++) {
        objPool[index++] = children[j];
        JSONObject node1 = new JSONObject();
        node1.put("_reference", factory.getId(children[j]));
        child.put(j, node1);
       }
       obj.put("children", child);
      }
     }
     ret.put(obj);
    }
    retRoot = ret;
    CacheUtil.put("TREEROOT", key, retRoot);
   }

   String retu = "{identifier: 'objectId', label: 'title', items: " + retRoot.toString() + "}";
   return retu;
}
分享到:
评论
1 楼 azllza 2008-06-27  
哈哈  买袭击 被我发现了啊

相关推荐

Global site tag (gtag.js) - Google Analytics