JSF Primefaces:如何从表单2中的dialogBox更新selectOneMenu(在form1中)?

时间:2012-11-15 10:37:38

标签: java jsf primefaces

我想在form2中提交命令按钮后更新form1中的selectOneMenu。它现在仅在刷新页面后才可见。

我在表单1中有一个selectOneMenu:

<h:form id="form1" rendered="#">

            <p:spacer height="30" />
            <h:selectOneMenu id="oneMenu" value="#{bean.value}">
                <f:selectItem itemLabel="Select Value" itemValue="" />
                <f:selectItems id="itemValues"
                    value="#{bean.allItems}" var="allItems"
                    itemValue="#{allItems}" itemLabel="#{allItems.name}" />
            </h:selectOneMenu>

和form2中的DialogBOX:

<h:form id="form2">
            <p:dialog header="Create new Item" widgetVar="newItem"
                resizable="false">

                <h:panelGrid columns="2" style="margin-bottom:10px">

                    <h:outputLabel for="item" value="Itemname:" />
                    <p:inputText id="itemname" value="#{bean.itemName}" />
                </h:panelGrid>

                <p:commandButton value="Submit"
                    actionListener="#{bean.newItem}"
                    update="form1:oneMenu" oncomplete="newItem.hide();"  />

            </p:dialog>

我已经尝试了update="form1:oneMenu"但它不起作用。我还看了this post 但它也不起作用。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

请务必使用update=":form1:myPanel"

示例:

<h:form id="form1">
    <h:selectOneMenu id="oneMenu" value="#{bean.value}">
    ...
    </h:selectOneMenu>
</h:form>

<h:form id="form2">
    <p:dialog ..>
        ...
        <p:commandButton value="Submit" update=":form1:oneMenu" ..../>
    </p:dialog>
</h:form>

否则

<h:form id="form1">
    <p:selectOneMenu id="oneMenu" value="#{bean.value}">
    ...
    </p:selectOneMenu>
</h:form>

<h:form id="form2">
    <p:dialog ..>
        ...
        <p:commandButton value="Submit" update=":form1:oneMenu" ..../>
    </p:dialog>
</h:form>

更新评论:请尝试以下操作。您可以使用h:selectOneMenup:selectOneMenu

<h:form id="form1">
    <p:selectOneMenu style="width:195px;" required="true" id="cityMenu">
        <f:selectItems value="#{SelectOneMenuBean.cities}"/>
    </p:selectOneMenu>
    <p:commandButton oncomplete="newItem.show()" value="Show Dialog"/>
</h:form>
<h:form id="form2">
    <p:dialog header="Create new Item" widgetVar="newItem" resizable="false">
        <h:panelGrid columns="2" style="margin-bottom:10px">
            <h:outputLabel for="item" value="Itemname:" />
            <p:inputText id="itemname" value="#{SelectOneMenuBean.selectedValue}" />
        </h:panelGrid>

        <p:commandButton value="Submit" update=":form1:cityMenu" oncomplete="newItem.hide();"  />

    </p:dialog>     
</h:form>

public class SelectOneMenuBean {
    private String selectedValue;

    public String getSelectedValue() {
        return selectedValue;
    }

    public void setSelectedValue(String selectedValue) {
        this.selectedValue = selectedValue;
    }

    public String[] getCities() {
        if(selectedValue != null && selectedValue.equals("AAA")) {
            return new String[] {"City_1 of AAA", "City_2 of AAA", "City_3 of AAA"};
        } else if(selectedValue != null && selectedValue.equals("BBB")) {
            return new String[] {"City_1 of BBB", "City_2 of BBB", "City_3 of BBB"};
        } else if(selectedValue != null && selectedValue.equals("CCC")) {
            return new String[] {"City_1 of CCC", "City_2 of CCC", "City_3 of CCC"};
        } else if(selectedValue != null && selectedValue.equals("DDD")) {
            return new String[] {"City_1 of DDD", "City_2 of DDD", "City_3 of DDD"};
        } 
        return new String[] {"No City"};
    }
}

注意:如果您在对话框中输入值AAABBBCCCDDD,则selectedOneMenu的值将被改变。