开发者

How to create a dynamic tree?

I´m having some trouble imple开发者_JAVA百科menting a dynamic tree structure using the primefaces tree implementation. In the showcase provided by primeface, the structure of the code is like below. This is however very static. I´m trying to figure out how I could do this with data fetched from a database, where the depth of the tree would be unknown at compile time.

I`m thinking that I probabply need some recursive method to implement this, but I cannot quite get my head around what the implementation would look like.

Any thoughts?

Below is the example code from 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);
}


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().
}


The example code from Primefaces is very static in my opinion too. I had the same problem and I figured it out by creating one dummy child node for each parent node. When expanding the parent node, the method 'onNodeExpand' removes the dummy child node and adds real child nodes.

<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);
    }
}


PrimeFaces ships an example for lazy loading in its documentation now: https://www.primefaces.org/showcase/ui/data/tree/lazyloading.xhtml. The rather generic class LazyLoadingTreeNode can be used to implement lazy loading of child tree nodes. I don't show any sample code here because it can be found in the PrimeFaces Showcase.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜