0

我有一个包含 parentid、childid 列的数据库表。我想创建一个使用 primefaces 树加载表数据的 jsf 树视图。给出的例子是静态数据。有人可以帮忙吗。我很欣赏。谢谢。

4

2 回答 2

3

XHTML

<h:form id="form">  

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

</h:form>  

爪哇

import java.util.Random;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.primefaces.event.NodeExpandEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

@ManagedBean
@SessionScoped

public class DynamicTreeBean {  

    private TreeNode root;  

    public DynamicTreeBean() {  
        root = new DefaultTreeNode("Root", null);
        TreeNode dynamicTreeNode = new DefaultTreeNode("First", root);
      //<Add dummy node: This is to have + sign in node to be able to expand it in GUI>
        TreeNode node0 = new DefaultTreeNode("fake", dynamicTreeNode);
      //<Add dummy node>
    }  

    public TreeNode getRoot() {  
        return root;  
    }
    static Random random = new Random();
    public void onNodeExpand(NodeExpandEvent event) {  
        //<Remove dummy node>
        if(event.getTreeNode().getChildCount()==1){
            event.getTreeNode().getChildren().remove(0);
        }
        //</Remove dummy node>

        //<Generate some random children>
        int childrenCount = random.nextInt(9)+1;
        for(int i=0;i<childrenCount;i++){
            TreeNode tn = new DefaultTreeNode("-child-"+i, event.getTreeNode());

            //<Add dummy node>
            new DefaultTreeNode("fake", tn);
            //</Add dummy node>
        }
        //</Generate some random children>
    }
}  
于 2013-01-03T19:17:10.047 回答
0

You can create your dynamic treenodes by creating a class that extends "DefaultTreeNode" and overwrite DefaultTreeNode methods to fit your needs and to use your proper data.

Then you create your own nodes by instantiating your new class.

You have just to provide the root node in your bean (with its get method).

于 2012-04-23T15:41:43.437 回答