小编典典

如何创建动态树?

java

我在使用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);
}

阅读 215

收藏
2020-11-30

共1个答案

小编典典

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