E3-关于复选框Tree权限管理
wjcom1127
2009-06-03
java后台构造树的代码 /** * 复选框树-权限管理 * @param pRequest * @param pResponse * @throws Exception */ public void showCheckTree(final HttpServletRequest pRequest, final HttpServletResponse pResponse) throws Exception{ //得到数据 List orgs = this.getOrgs(); UserDataUncoder orgUncoder = new OrgUncoder(); AbstractWebTreeModelCreator treeModelCreator = new AbstractWebTreeModelCreator(){ protected Node createNode(Object pUserData, UserDataUncoder pUncoder) { Org org = (Org)pUserData; WebTreeNode result = new WebTreeNode(org.getName(), "org" + org.getId()); result.setValue(org.getId()); return result; } }; treeModelCreator.init(pRequest); TreeModel treeModel = treeModelCreator.create(orgs,orgUncoder); TreeDirector director = new DefaultTreeDirector(); director.setComparator(new AbstractNodeComparator(){ protected Comparable getComparableProperty(Node pNode) { Org org = (Org)pNode.getUserData(); return new Integer( org.getViewOrder() ); } }); WebTreeBuilder treeBuilder = new PrvCheckXTreeBuilder(); //PrvCheckXTreeBuilder treeBuilder = new PrvCheckXTreeBuilder(); //treeBuilder.setCascadeCheck(true); treeBuilder.init(pRequest); director.build(treeModel, treeBuilder); String treeScript = treeBuilder.getTreeScript(); pRequest.setAttribute("treeScript", treeScript); pRequest.getRequestDispatcher("/e3/samples/jcom/CheckTree.jsp").forward(pRequest,pResponse); }
checkTree.jsp显示树 <%= request.getAttribute("treeScript") %>
1:得到的结果如下图:
2:选择保存
得到的节点ID为:100,100100,100100101,100100102
3:进入后台将节点ID保存到数据库,代码如下: //数据进入数据库 DbManager.excute(sql); //后台重新构造 复选框树 /** * 复选框树-权限管理 * @param pRequest * @param pResponse * @throws Exception */ public void showCheckTree(final HttpServletRequest pRequest, final HttpServletResponse pResponse) throws Exception{ //得到数据 List orgs = this.getOrgs(); UserDataUncoder orgUncoder = new OrgUncoder(); AbstractWebTreeModelCreator treeModelCreator = new AbstractWebTreeModelCreator(){ protected Node createNode(Object pUserData, UserDataUncoder pUncoder) { Org org = (Org)pUserData; WebTreeNode result = new WebTreeNode(org.getName(), "org" + org.getId()); result.setValue(org.getId()); return result; } }; treeModelCreator.init(pRequest); TreeModel treeModel = treeModelCreator.create(orgs,orgUncoder); TreeDirector director = new DefaultTreeDirector(); director.setComparator(new AbstractNodeComparator(){ protected Comparable getComparableProperty(Node pNode) { Org org = (Org)pNode.getUserData(); return new Integer( org.getViewOrder() ); } }); WebTreeBuilder treeBuilder = new PrvCheckXTreeBuilder(); //PrvCheckXTreeBuilder treeBuilder = new PrvCheckXTreeBuilder(); //treeBuilder.setCascadeCheck(true); treeBuilder.init(pRequest); director.build(treeModel, treeBuilder); String treeScript = treeBuilder.getTreeScript(); pRequest.setAttribute("treeScript", treeScript); pRequest.getRequestDispatcher("/e3/samples/jcom/CheckTree.jsp").forward(pRequest,pResponse); }
4:CheckTree.jsp //显示树 <%= request.getAttribute("treeScript") %> //同时用js进行判断 <script type="text/javascript"> <!-- var root = getTreeRoot();// 获取跟节点 root.expandAll();//『默认展开』 var children = root.childNodes; for(var i=0; i<children.length; i++){ var child = children[i]; var schild = child.childNodes; for(var j=0;j<schild.length;j++){ var childs = schild[j]; var svu = childs.value; <% // alt 是ArrayList的对象 保存的是从数据库里取出来的100,100100,100100101,100100102 if(alt.size()>0){ for(int j=0;j<alt.size();j++){ String sg = (String)alt.get(j); %> if(svu=="<%=sg%>"){//如果当前节点ID等于从数据库里取出的节点ID,就把该节点设为选中状态 childs.setChecked(true); } <% } } %> } } //-->
5:至此,以上本人以为以上流程没有问题 但显示的结果是
但我想要的结果是:
至此问题出来了, 我自己也测试过了,当从数据库得到ID等于“权限管理”ID时 用childs.setChecked(true);方法 则下面的4个子节点都默认被选中了
辛苦各位,被这困扰好几天了!杨哥.............................
|
|
huangyh
2009-06-03
兄弟,你搞太复杂了,直接在
protected Node createNode(Object pUserData, UserDataUncoder pUncoder) { Org org = (Org)pUserData; WebTreeNode result = new WebTreeNode(org.getName(), "org" + org.getId()); result.setValue(org.getId()); return result; } }; 在这里设置 result.setSelected(true);就可以了,(你在着判断一个接点是否该选种. 方法名忘记了,可能是result.setSetChecked(true); |
|
huangyh
2009-06-03
你这问题问的清楚明了
|
|
wjcom1127
2009-06-03
我想兄弟是没明白我问这个问题的目的!
protected Node createNode(Object pUserData, UserDataUncoder pUncoder) { Org org = (Org)pUserData; WebTreeNode result = new WebTreeNode(org.getName(), "org" + org.getId()); result.setValue(org.getId()); return result; } }; 在此也可以设置!这个我知道! 但还要没有解决我问的问问题 后台用: result.setSelected(true); result.setSetChecked(true); 和我在前台用: childs.setChecked(true); 达到的效果都一样 就是都全部选中了!并没有解决我问的问题! |
|
huangyh
2009-06-04
你确定,毛道理哦,晚上回去测试下
|
|
huangyh
2009-06-04
已经测试了,不会存在你说的问题 .
|
|
InnocentBoy
2009-06-05
貌似梅花雪Tree的改版吗!!!
|
|
wjcom1127
2009-06-08
谢咯!兄弟搞定了!
|
|
hurenliwenwen
2009-09-03
|
|
hurenliwenwen
2009-09-04
我也做这个功能那 但你的例子好深奥 我看不懂 有时间可以和你聊聊嘛 js中怎么得到存在session里的值啊 你的那种方法“<=>”我用过 取不出来 希望给留言 指点迷津 谢谢
|