我有一个包含parentid列的数据库表,childid。我想创建一个使用primefaces树加载表数据的jsf树视图。给出的例子是静态数据。请有人帮帮忙。我很高兴。感谢。
答案 0 :(得分: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>
}
}
答案 1 :(得分:0)
您可以通过创建扩展“ DefaultTreeNode ”的类来创建动态treenode,并覆盖DefaultTreeNode方法以满足您的需求并使用正确的数据。
然后通过实例化您的新类来创建自己的节点。
您只需在bean中提供根节点(使用其get方法)。