0

我有这个:

<p:treeTable id="#{treeTableId}" value="#{tableBeanRoot}" var="element"
    resizableColumns="true"
    selection="#{budgetTemplateElementBean.selectedNode}"
    selectionMode="single" widgetVar="#{treeTableId}widget">

    <f:facet name="header">

    </f:facet>

    <p:column style="width:150px"> 
             .....
    </p:column>
             .....
    <f:facet name="footer">
        <p:commandButton value="#{msg.addChildNode}"
            actionListener="#{actionBean.addChildNodeAction}"
            process="@this,#{treeTableId}" update="#{treeTableId} :growl" />
        <p:commandButton value="#{msg.deleteNode}"
            actionListener="#{actionBean.deleteNodeAction}"
            process="@this,#{treeTableId}" update="#{treeTableId} :growl" />
    </f:facet>
</p:treeTable>

如何在 Primefaces treeTable 中添加和删除 treeNodes?我可以在后豆里放什么?

谢谢

4

2 回答 2

7

添加删除节点的一​​种解决方案如下:

public boolean removeElemetOfTreeNode(TreeNode rootNode,
        TreeNode nodeToDelete) {
    if (rootNode.getChildren().remove(nodeToDelete)) {
        return true;
    } else {
        for (TreeNode childNode : rootNode.getChildren()) {
            if (childNode.getChildCount() > 0) {
                return removeElemetOfTreeNode(childNode, nodeToDelete);
            }

        }
        return false;
    }
}

public void addNodeAction(ActionEvent event) {
    if (selectedNode == null) {
        // TODO: añadir excepcion no seleccionado
    }
    Random randomGenerator = new Random();
    TreeNode pepe = new DefaultTreeNode(new Configuration("new node"
            + randomGenerator.nextInt(100), false, "new node"
            + randomGenerator.nextInt(100), false),
            selectedNode.getParent());
    return;
}

public void addChildNodeAction(ActionEvent event) {
    if (selectedNode == null) {
        // TODO: añadir excepcion no seleccionado
    }
    Random randomGenerator = new Random();
    TreeNode pepe = new DefaultTreeNode(new Configuration("new node"
            + randomGenerator.nextInt(100), false, "new node"
            + randomGenerator.nextInt(100), false), selectedNode);
    return;
}
于 2013-05-14T13:48:15.967 回答
1

我认为删除节点时无需使用BFS遍历树,因为节点本身知道其父节点:

public void removeElemetOfTreeNode(TreeNode nodeToDelete) {
    TreeNode parent = nodeToDelete.getParent();
    if(parent == null){
        // throw exception or handle case of root node differently 
    }
    parent.getChildren().remove(nodeToDelete);
}

如果您需要检查它是否nodeToDelete是它的后代,rootNode那么从开始nodeToDelete并向上直到到达它的根然后比较它rootNode而不是使用 BFS 遍历整个树也更有效。

于 2017-06-21T16:20:02.560 回答