做了七天的动态树,虽然还没有做完但先把一些比较重要的东西记下来。
目前实现了点击节点动态加载树的功能,但点击左侧的箭头相应的事件还没有找到,还是希望有经验的朋友可以指点一二。
<h:panelGrid columns="2" width="100%" columnClasses="col1,col2">
<rich:tree style="width:300px" nodeSelectListener="#{tree1bean.nodeload}" toggleOnClick="true" switchType="ajax" value="#{tree1bean.treeNode}">
</rich:tree>
</h:panelGrid>
以上的toggleOnClick实现了点击节点时展开、关闭树节点。
switchType="ajax"实现了局部刷新,只是树加载的部分刷新,但不重载整个页面。
package org.jboss.test.main;
import java.util.List;
import javax.ejb.Remove;
import javax.faces.event.AbortProcessingException;
import javax.persistence.EntityManager;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.richfaces.component.UITree;
import org.richfaces.event.NodeExpandedEvent;
import org.richfaces.event.NodeSelectedEvent;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;
@Scope(ScopeType.SESSION)
@Name("tree1bean")
public class Tree1Bean{
private Tree tree;
private TreeNode treenode = null;
private List<Tree> onloadtree;
private List<Tree> onloadtree1;
private String selectnodesql = "from Tree t where t.parentid = ? order by t.id";
private int linkid;
private int flg;
private int parentid;
@In("entityManager")
private EntityManager em;
private void firstNode(int id,TreeNode node){
onloadtree = em.createQuery(selectnodesql).setParameter(1, id).getResultList();
for (int i=0;i<onloadtree.size();i++){
TreeNodeImpl nodeImpl = new TreeNodeImpl();
nodeImpl.setData(onloadtree.get(i).getName());
node.addChild(new Integer(onloadtree.get(i).getId()), nodeImpl);
secondeNode(onloadtree.get(i).getId(),nodeImpl);
}
}
private void secondeNode(int id,TreeNode node){
onloadtree1 = em.createQuery(selectnodesql).setParameter(1, id).getResultList();
for (int i=0;i<onloadtree1.size();i++){
TreeNodeImpl nodeImpl = new TreeNodeImpl();
nodeImpl.setData(onloadtree1.get(i).getName());
node.addChild(new Integer(onloadtree1.get(i).getId()), nodeImpl);
}
}
public TreeNode getTreeNode(){
if (treenode == null){
treenode = new TreeNodeImpl();
parentid = 0;
firstNode(parentid,treenode);
}
return treenode;
}
public void nodeload(NodeSelectedEvent event){
UITree tree = (UITree) event.getComponent();
String sss [] = tree.getRowKey().toString().split(":");
linkid = Integer.parseInt(sss[sss.length-1]);
System.out.println("linkid="+linkid);
tree.clearTreeNodeState();
firstNode(linkid,tree.getModelTreeNode());
}
public int getLinkid() {
return linkid;
}
public void setLinkid1(int linkid) {
this.linkid = linkid;
}
@Remove
public void destroy() {
// TODO Auto-generated method stub
}
}
以上的tree.clearTreeNodeState();实现了收缩节点以后其以后的节点都收缩了回去。回到了初始状态