ExtJS4 动态加载TreePanel的问题

前台代码 :
[code="java"] store : Ext.create('Ext.data.TreeStore', {
root : {
id : '00',
text : '民星兽药',
expanded : true
//nodeType : 'async'
},
proxy : {
autoLoad:true,
type : 'ajax',
url : ctxpath + '/SystemManager/TreeList',
reader : {
type : 'json',
root : 'alltrees'
}[/code]

Action:
[code="java"] @Override
public String execute() throws Exception {
ActionContext actionContext = ActionContext.getContext(); // 取到struts容器
Map session = actionContext.getSession(); // 取得session
User user = (User) session.get("user");
try {
alltrees = userServices.listTrees(user.getUsername());
for(int i=0;i<alltrees.size();i++){
System.out.println(alltrees.get(i).getText());
}
} catch (Exception e) {
e.printStackTrace();
}
// this.success = true;
return SUCCESS;
}[/code]

listTrees对应的业务方法:
[code="java"]public List

listTrees(String username) throws Exception {
List menus = new ArrayList();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select a.pk_archives,a.archivesname,a.url_name,a.cls,a.leaf,a.pid from drug_user u join drug_role r "
+ "on u.role_id = r.limit_id join drug_limit l on r.limit_id = l.role_code "
+ "join drug_archives a on l.archives_id = a.limit_id where u.username = ?";
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, username);
rs = ps.executeQuery();
while (rs.next()) {
Menu menu = new Menu();
menu.setId(rs.getString("pk_archives"));
menu.setText(rs.getString("archivesname"));
menu.setUrl(rs.getString("url_name"));
menu.setCls(rs.getString("cls"));
menu.setLeaf(Boolean.parseBoolean(rs.getString("leaf")));
menu.setParentId(rs.getString("pid"));
menus.add(menu);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rs.close();
ps.close();
conn.close();
}
//System.out.println(menus.size());
List result = new ArrayList();
if (menus != null && menus.size() > 0) {
for (int i = 0; i < menus.size(); i++) {
String id = menus.get(i).getId();
for (int j = 0; j < menus.size(); j++) {
if (id.equals(menus.get(j).getParentId())) {
menus.get(i).getChildren().add(menus.get(j));
menus.get(j).setExpanded(true);
}
}
}
for (int k = 0; k < menus.size(); k++) {
if ("00".equals(menus.get(k).getParentId())) {
result.add(menus.get(k));
}
}
}
return result;
}[/code]

前台后太都没报错;
谷歌浏览器截取到返回页面的数据:
{"alltrees":[{"children":[{"children":[],"cls":"file","expanded":true,"id":"100221","leaf":true,"parentId":"100200","text":"叶子一","url":"\/veterinaryDrug\/SystemManager\/RoleList"}],"cls":"folder","expanded":false,"id":"100200","leaf":false,"parentId":"00","text":"系统管理","url":null},{"children":[],"cls":"folder","expanded":false,"id":"100201","leaf":false,"parentId":"00","text":"销售","url":null}]}

现象如图;
还有一个问题:为啥登录进来后程序会加载两次treeStore??

1个回答

[code="java"]
应该多贴点前端js代码 , 后端java只要返回json字符串即可
可以参考下地址 [url]
https://gist.github.com/x03570227/2994798
[/url]

至于请求两次, 可以是你自己autoload一次
还有看看其他有没有reload或者tree里面的 expand展开事件处理
[/code]

GQ2010
gq2010 你的treestore加载两次是怎么回事啊,我的treestore设置为自动加载了,点击加号时自动加载下面的子节点(监听了beforeexpand事件),但有时会请求两次,导致添加的子节点不对
6 年多之前 回复
u010729044
u010729044 多谢指教!加载两次的问题已经解决了。你给的例子让我受益匪浅。
接近 7 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐