支持Bean中的Primefaces 4.0树选择更新不起作用 [英] Primefaces 4.0 Tree selection update in backing bean does not work

查看:106
本文介绍了支持Bean中的Primefaces 4.0树选择更新不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having a primefaces Tree in a dialog. When I make selections in the Tree and click Add button, it adds all selections to a "p:selectManyMenu" in another panel. Code for the Dialog and Tree:

<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中进行选择并单击remove按钮时,它将从后备bean"searchController.selectedCategories"中删除选定的类别并更新对话框.删除按钮的代码:

So far it works fine. Now when I make selections in the ManyMenu and click remove button, it removes the selected categories from the backing bean "searchController.selectedCategories" and updates the dialog. Code for remove button:

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

removeCategory Java方法的代码

Code for removeCategory Java method

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,但旧值仍然存在.不知道出什么问题了.我在做错什么吗?

But when I open the dialog again, the previous selections persist. I am sure that the "searchController.selectedCategories" is updated but the selections in the Tree dont get updated. I also tried setting the "searchController.selectedCategories" value to null in remove button's action but the old values still persist. Not sure what is wrong. Am I doing something wrong?

推荐答案

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

We have to update both the selection array and also the Tree for it to reflect the changes. The removeCategory() should have the following:

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();
}

这篇关于支持Bean中的Primefaces 4.0树选择更新不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆