Primefaces 4.0支持bean中的树选择更新不起作用

时间:2014-07-14 15:38:09

标签: jsf-2 primefaces

我在对话框中有一个primefaces树。当我在树中进行选择并单击“添加”按钮时,它会将所有选择添加到另一个面板中的“p:selectManyMenu”。 对话框和树的代码:

<h:form id="categoriesDialogForm">
<p:dialog id="categoriesDialog" header="Add a Category" widgetVar="categoriesDialogVar" closeOnEscape="true" height="500" resizable="false" >
    <p:panelGrid>
        <p:row>
            <p:column>
                <p:tree id="categoriesTree" value="#{searchController.categoriesTree}" var="node" selectionMode="checkbox" style="width : auto;" selection="#{searchController.selectedCategories}" propagateSelectionDown="false" propagateSelectionUp="false">
                    <p:treeNode>
                        <h:outputText value="#{node}" />
                    </p:treeNode>
                </p:tree>
            </p:column>                        
        </p:row>                        
    </p:panelGrid>
    <f:facet name="footer">                 
        <div align="center">
            <p:commandButton value="Add" style="font-size: 0.8em;" action="#{searchController.addCategory()}" update=":advSearchForm:subCatPanel"/>
        </div>
    </f:facet>            
</p:dialog>
</h:form>

到目前为止一切正常。现在,当我在ManyMenu中进行选择并单击“删除”按钮时,它将从支持bean“searchController.selectedCategories”中删除所选类别并更新对话框。删除按钮的代码:

<p:commandButton value="Remove" style="font-size : 0.9em;" action="#{searchController.removeCategory()}" update=":categoriesDialogForm:categoriesTree"></p:commandButton>

removeCategory Java方法的代码

public void removeCategory() {   
 //Updating the selectedSubjectSet collection
 //removeSubjects is a Set for which the node selection should be removed
 for (String subToRemove : removeSubjects) {
        selectedSubjectSet.remove(subToRemove);
 }   

//Rebuilding the array of selections
//selectedSubjectSet is a Set whose selections in the Tree should remain
List<TreeNode> result = new ArrayList<>();
for (TreeNode t : selectedCategories) {
    if (selectedSubjectSet.contains(t.getData().toString())) {
        result.add(t);
    }
}
selectedCategories = result.toArray(new TreeNode[result.size()]);   
removeSubjects.clear();
}

但是当我再次打开对话框时,之前的选择仍然存在。 我确信“searchController.selectedCategories”已更新,但树中的选择不会更新。我还尝试在删除按钮的操作中将“searchController.selectedCategories”值设置为null,但旧值仍然存在。不确定有什么问题。我做错了吗?

1 个答案:

答案 0 :(得分:1)

我们必须同时更新选择数组和Tree,以反映更改。 removeCategory()应该具有以下内容:

public void removeCategory() {   
  //Updating the selectedSubjectSet collection
  //removeSubjects is a Set for which the node selection should be removed
  for (String subToRemove : removeSubjects) {
      selectedSubjectSet.remove(subToRemove);
  }   

  //Rebuilding the array of selections
  //selectedSubjectSet is a Set whose selections in the Tree should remain
  List<TreeNode> result = new ArrayList<>();
  for (TreeNode t : selectedCategories) {
     if (selectedSubjectSet.contains(t.getData().toString())) {
      result.add(t);
     }
  }
  selectedCategories = result.toArray(new TreeNode[result.size()]);   

  //Updating the Tree for selections
  //Need to iterate the Level 2 Nodes as well
  List<TreeNode> tree = categoriesTree.getChildren();        
    for (TreeNode node : tree) {
        String subject = (String) node.getData();
        if (removeSubjects.contains(subject)) {
            node.setSelected(false);
        }
        List<TreeNode> subTree = node.getChildren();
        for (TreeNode subNode : subTree) {
            subject = (String) subNode.getData();
            if (removeSubjects.contains(subject)) {
                subNode.setSelected(false);
            }
        }
    }

  removeSubjects.clear();
}