在primefaces treeTable中添加和删除节点

时间:2013-05-14 13:45:28

标签: jsf primefaces

我有这个:

<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?我可以把什么东西放回来?

由于

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

我认为在删除节点时没有必要用BFS遍历树,因为节点本身知道它的父节点:

IQueryable<T>

如果您需要检查price = co == null ? 0 : (co.price ?? 0) 是否是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而不是使用BFS遍历整个树。