ExtJS Tree刷新后自动展开并选择节点
实例讲解:通过点击岗位,将岗位对应的模块菜单给展开。
1.点击岗位获取对应模块菜单ID:
function check(node,checked){ if(checked == true){ //得到岗位对应的功能 Ext.Ajax.request({ url : 'user.up?doType=getModuleById', params : {postID : node.id}, success : function(response, options) { checkModuleid = response.responseText; module_root.reload();//这是个根节点,此处不用treeLoader.reload() } }); } }
结果示例:{03,0302,0305,05,0504}
2.通过获取到的ID获取对应的Path路径:
moduleloader.on("load",function(treeLoader, node){ var ids = checkModuleid?checkModuleid.split(","):null; if(!ids){//||ids.length==0 return; } for(var i = 0;i<ids.length;i++){ Ext.Ajax.request({ url: "user.up?doType=moduleExpandTo", params: { id:ids[i]}, success:function(response,option){ var result = Ext.util.JSON.decode(response.responseText); if(result.math=="no")return; module_tree_node.expandPath('module_tree/module_root/' + result.path, 'id', onExpandPathComplete); } }); } });
Servlet:
if("moduleExpandTo".equals(action)){
response.setContentType("text/json;charset=UTF-8");
out=response.getWriter();
String id = request.getParameter("id");
String path = uDao.getPathOfModuleNode(id);
if(path ==null||"".equals(path)){
json = "{match:'no'}";
}else{
json = "{match:'yes',path:'"+path+"'}";
}
out.print(json);
out.close();
return;
}
Dao:
public String getPathOfModuleNode(String id) {
String path = null;
StringBuffer sql = new StringBuffer(
"select moduleid,name from modules where enable = 1 connect by prior pid = moduleid start with moduleid='")
.append(id);
sql.append("' order by LEVEL DESC");
Session s = null;
try {
s = HibernateUtil.getSession();
s.beginTransaction();
SQLQuery query = s.createSQLQuery(sql.toString());
List lst = query.list();
if (lst != null && lst.size() > 0) {
StringBuffer sp = new StringBuffer("");
for (int i = 0; i < lst.size(); i++) {
Object[] flds = (Object[]) lst.get(i);
sp.append((String) flds[0]);
if (i < lst.size() - 1) {
sp.append("/");
}
}
path = sp.toString();
}
s.getTransaction().commit();
} catch (Throwable e) {
log.error(e.toString());
HibernateUtil.endSession(s);
} finally {
HibernateUtil.endSession(s);
}
return path;
}
3.展开树的方法:
function onExpandPathComplete(flag, node) { if(!flag) { return; } if(node){ node.ensureVisible();//确保该节点可见——即不可见时将展开其父目录使其可见。 node.select() ; //选中该节点!——即底纹变浅蓝色(相当于对该节点做了单击!!!) //方式二:treePanel.getSelectionModel().select(node); if(node.leaf){ node.ui.toggleCheck(true); //选中复选框 } } }
注意:
1.被选中项显示勾的方式——即复选框勾选的方式:
2.展开树——即展开选中节点的父节点的方式:
2.选中节点——相当于鼠标单击了该节点:
(1).
(2).
图示:
相应例子:http://atian25.iteye.com/blog/724092