【发布时间】:2014-07-14 15:38:09
【问题描述】:
我在对话框中有一个 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,但旧值仍然存在。不知道出了什么问题。我做错了吗?
【问题讨论】:
-
只是一个建议:尝试交换表单和对话框,然后更新整个表单
-
我认为你没有使用 requestscoped bean?
-
@Jaqen:我正在使用 ViewScoped bean
-
@Jaqen:交换表单和对话框并没有什么不同。
-
尝试更新整个对话框:
<p:commandButton value="Remove" style="font-size : 0.9em;" action="#{searchController.removeCategory()}" update=":categoriesDialogForm:categoriesDialog"/>
标签: jsf-2 primefaces