如何创建动态树?

时间:2011-07-12 06:48:45

标签: java tree primefaces

我在使用primefaces树实现实现动态树结构时遇到了一些麻烦。在primeface提供的展示中,代码的结构如下所示。然而,这是非常静态的。我试图弄清楚如何使用从数据库获取的数据来做到这一点,在数据库中,树的深度在编译时是未知的。

我认为我可能需要一些递归方法来实现它,但我无法理解实现的样子。

有什么想法吗?

以下是来自primefaces的示例代码

private TreeNode root;
private TreeNode selectedNode;

public TreeBean() {
    root = new DefaultTreeNode("Root", null);
    TreeNode node0 = new DefaultTreeNode("Node 0", root);
    TreeNode node1 = new DefaultTreeNode("Node 1", root);
    TreeNode node2 = new DefaultTreeNode("Node 2", root);

    TreeNode node00 = new DefaultTreeNode("Node 0.0", node0);
    TreeNode node01 = new DefaultTreeNode("Node 0.1", node0);

    TreeNode node10 = new DefaultTreeNode("Node 1.0", node1);
    TreeNode node11 = new DefaultTreeNode("Node 1.1", node1);

    TreeNode node000 = new DefaultTreeNode("Node 0.0.0", node00);
    TreeNode node001 = new DefaultTreeNode("Node 0.0.1", node00);
    TreeNode node010 = new DefaultTreeNode("Node 0.1.0", node01);

    TreeNode node100 = new DefaultTreeNode("Node 1.0.0", node10);
}

3 个答案:

答案 0 :(得分:4)

private TreeNode root;

private TreeNode selectedNode;

public TreeBean() {
root = new DefaultTreeNode("Root", null);
List rootNodes<Employee> = SearchDao.getRootNodes();

Iterator it = rootNodes.iterator();
while (it.hasNext()) {

TreeNode node1 = new DefaultTreeNode(**it.next()**, root);
    **/* in place of it.next() I need to display empName. When I click on empName, I need to get the Id(Pkey). */**

}

}

public TreeNode getRoot() {
return root;
}

public TreeNode getSelectedNode() {
return selectedNode;
}

public void setSelectedNode(TreeNode selectedNode) {
this.selectedNode = selectedNode;
}



public void addChildNode(ActionEvent actionEvent) {
System.out.println("Selected Node: "+getSelectedNode().toString());
TreeNode newNode = new DefaultTreeNode("Node New", getSelectedNode());
getSelectedNode().setExpanded(true);
}
public void addTopicBelow(ActionEvent actionEvent){
TreeNode newNode = new DefaultTreeNode("Node New", getSelectedNode().getParent());
}
public void deleteNode(ActionEvent actionEvent){
 System.out.println("Node to be deleted: "+getSelectedNode().toString());
 //getSelectedNode().
}

答案 1 :(得分:3)

Primefaces的示例代码在我看来也是非常静态的。我有同样的问题,我通过为每个父节点创建一个虚拟子节点来解决它。展开父节点时,方法“ onNodeExpand ”将删除虚拟子节点并添加真实子节点。

<h:form id="form">
    <p:tree id="tree" value="#{treeController.root}" var="node" dynamic="true" cache="false">
        <p:ajax event="expand" listener="#{treeController.onNodeExpand}" />
        <p:treeNode>
            <h:outputText value="#{node}" />
        </p:treeNode>
    </p:tree>
</h:form>



package com.test.web;

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

@Named(value = "treeController")
@SessionScoped
public class TreeController implements Serializable {

    private TreeNode root;

    @PostConstruct
    public void buildTree() {
        root = new DefaultTreeNode("Root", null);  
        createNode("Node 0", root);
        createNode("Node 1", root);
        createNode("Node 2", root);                        
    }

    public TreeNode getRoot() {
        return root;
    }

    public void setRoot(TreeNode root) {
        this.root = root;
    }

    public void onNodeExpand(NodeExpandEvent event) {  
         DefaultTreeNode parent = (DefaultTreeNode) event.getTreeNode();
         if (parent.getChildCount() == 1 && parent.getChildren().get(0).getData().toString().equals("DUMMY")) {
             parent.getChildren().remove(0);
             createNode("Node A", parent);
             createNode("Node B", parent);
             createNode("Node C", parent);
         }
    }

    private void createNode(String tag, TreeNode parent) {
        TreeNode node = new DefaultTreeNode(tag, parent); 
        // Create Dummy node, just to make the parent node expandable
        new DefaultTreeNode("DUMMY", node);
    }
}

答案 2 :(得分:0)

PrimeFaces 现在在其文档中提供了一个延迟加载示例:https://www.primefaces.org/showcase/ui/data/tree/lazyloading.xhtml。相当通用的类 LazyLoadingTreeNode 可用于实现子树节点的延迟加载。我没有在这里展示任何示例代码,因为它可以在 PrimeFaces Showcase 中找到。