ExtJS Tree刷新后只选择节点,不展开
实例讲解:通过点击岗位,将岗位对应的模块菜单给展开。
1..先对树进行全面加载(无须获取Path路径了):
moduleloader.on("load",function(treeLoader, node){ //将树面板先展开再关闭,用于将所有子节点显示出来! module_tree.expandAll(); //注意!此处是将树面板全部展开!!!而不是节点!!! module_tree.collapseAll(); module_root.expand();//展开根节点 }注意:
必须全部展开,各级目录下的所有叶子节点才能充分加载完全!若未加载则为“undifined”,无法被勾选。
2.点击岗位获取对应模块菜单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; showCheckModules(); //module_root.reload(); } }); } }结果示例:{03,0302,0305,05,0504}
3.将对应选项进行勾选,但不展开:
function showCheckModules(){ //先清楚所有勾选项 var treeNodes = module_root.childNodes; for (var i = 0; i < treeNodes.length; i++) { var id = treeNodes[i].id; var node = module_tree.getNodeById(id); node.ui.toggleCheck(false); } //再将对应的给勾选上 var ids = checkModuleid?checkModuleid.split(","):null; if(!ids){//ids为空则返回 return; } for (var i = 0; i < ids.length; i++) { var id = ids[i]; if(id.length == 4){ var node = module_tree.getNodeById(id);//通过id获取节点 node.ui.toggleCheck(true);//选中节点 } } }
注意:
1.这种方式必须先把树全部展开一遍,即全部加载一遍,所以Sql查询时必须一次性全查出来
2.刷新后展开的无须一次性全部加载完!
4.加载菜单树:
Servlet:
if("getModuleTree".equals(action)){//得到菜单树 response.setContentType("text/html;charset=UTF-8"); String pid=request.getParameter("pid")==null?"":request.getParameter("pid"); if("module_root_value".equals(pid)){ pid=""; } String sloadAll=request.getParameter("loadAll"); int loadAll = 1;//注意此处为1时为一次性加载,为0时为分级加载(点击目录才加载) try{ loadAll=Integer.parseInt(sloadAll); }catch(Exception e){} List list = uDao.getModuleList(pid,loadAll); json = uDao.getTreeNodesOfJson(list, loadAll, pid); out = response.getWriter(); out.print(json); out.close(); return; }Dao:
public List getModuleList(String pid, int loadAll) { List list = new ArrayList(); Session s = null; try { s = HibernateUtil.getSession(); s.beginTransaction(); StringBuffer sql = new StringBuffer( "select * from(select moduleid,name,pid,qybj,isleaf from modules t where qybj=1 and enable = 1 start with moduleid in(select moduleid from modules where pid is null) connect by prior moduleid = pid order by dorder)"); if (loadAll == 0) {// 分级加载 if (pid == null || "".equals(pid)) sql.append(" where pid is null "); else sql.append(" where pid='").append(pid + "'"); } else { if (pid != null && !"".equals(pid)) sql.append(" where pid='").append(pid + "'"); } SQLQuery query = s.createSQLQuery(sql.toString()); List lst = query.list(); if (lst != null && lst.size() > 0) { for (int i = 0; i < lst.size(); i++) { Object[] flds = (Object[]) lst.get(i); UserSimpleBean sBean = new UserSimpleBean(); sBean.setBm((String) flds[0]); sBean.setMc((String) flds[1]); sBean.setPid((String) flds[2]); sBean.setIsLeaf(((BigDecimal) flds[4]).intValue()); list.add(sBean); } } s.getTransaction().commit(); } catch (Throwable e) { log.error(e.toString()); HibernateUtil.endSession(s); } finally { HibernateUtil.endSession(s); } return list; }
Sql语句:
select * from(select moduleid,name,pid,qybj,isleaf from modules t where qybj=1 and enable = 1 start with moduleid in(select moduleid from modules where pid is null) connect by prior moduleid = pid order by dorder)
注意:
分级加载时加上了“where pid = ?”的条件。
图示:
1.点击岗位勾选相应菜单,不展开
2.点击菜单展开: