日期:2014-05-16  浏览次数:20373 次

ExtJs纵行Tree以及Tree下节点循环遍历
前台JS
Ext.onReady(function(){
    var treeLoader = new Ext.tree.TreeLoader({
   dataUrl:'../servlet/basic?action=treePanelTest',
   listeners:{
      beforeload:function(treeLoader,node){
      alert(node.attributes.id);
      treeLoader.baseParams.parentID = node.attributes.id;
        //将AsyncTreeNode中的id传给变量parentID,然后交个Servlet处理
       }
       }
          });
    var form = new Ext.tree.TreePanel({
    renderTo:'hello',
    root: new Ext.tree.AsyncTreeNode({id:'-1',text : '根节点'}),
    rootVisible:true,//判断根节点是否显示
    loader:treeLoader
      });
});
后台Servlet:
JSONArray array = new JSONArray();
JSONObject json;
if ("-1".equals(req.getParameter("parentID"))) {
List<Company> list = CompanyService.getAllCompanyList();
for (Company company : list) {
     json = new JSONObject();
     json.accumulate("id", company.getId());
     json.accumulate("text", company.getName());
     json.accumulate("leaf", false);
     array.add(json);
}
} else {
long parentID = Long.parseLong(req.getParameter("parentID"));
Company company = CompanyService.findCompanyByID(parentID);
for (Company dept : company.getDepartments()) {
json = new JSONObject();
json.accumulate("id", dept.getId());
json.accumulate("text", dept.getName());
json.accumulate("leaf", false);
array.add(json);
}
for (User user : company.getStaff()) {
json = new JSONObject();
json.accumulate("id", user.getId());
json.accumulate("text", user.getUserName());
json.accumulate("leaf", true);
array.add(json);
}
}
       String retJSON = array.toString();