1

I want to update a selectOneMenu in form1 after submitting the commandbutton in form2. It is now only visible after a refresh of the page.

I have a selectOneMenu in form 1:

<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>

and a DialogBOX in form2:

<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>

I've tried update="form1:oneMenu" but it doesn't work. I've also read this post but it doesn't work either.

Thanks for help.

Community
  • 1
  • 1
Johnny2012
  • 1,512
  • 8
  • 31
  • 46
  • you mean you tried `update=":form1:oneMenu" and it doesn't work? Is `rendered="#"` a typo in your code? – Akos K Nov 15 '12 at 10:40
  • ya, I tried both `update=":form1:oneMenu"` and `update="form1:oneMenu"`. I don't know why and for what there is the rendered="#". I'm developing on a existing code and I'm new in JSF. – Johnny2012 Nov 15 '12 at 10:55
  • Related: [How to reference components in ajax?](http://stackoverflow.com/a/8644762/157882) – BalusC Nov 15 '12 at 11:16

1 Answers1

1

Make sure to use update=":form1:myPanel".

Example :

<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>

Otherwise

<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>

Update For Comment : Try that below. You can use h:selectOneMenu or p: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"};
    }
}

Note : If you input value AAA,BBB,CCC and DDD in dialog, the values of selectedOneMenu will be changed.

Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131
  • tried your second one, now there is no more error. Thanks for that. But it still does not update the menu. :S I've also set autoupdate="true" in the menu tag – Johnny2012 Nov 15 '12 at 11:20
  • @cycdemo, isn't your recommendation in that `getCities()` a little unsafe? Apart from being bad practice, JSF is wont to ignore submits on a command component bound to a value that changes midcycle will most likely result in a `Validation error: value not valid` error – kolossus Nov 15 '12 at 17:48