2012-11-15 29 views
1

我想在form2中提交commandbutton後更新form1中的selectOneMenu。現在僅在刷新頁面後纔可見。JSF Primefaces:如何從窗體2中的dialogBox更新selectOneMenu(在form1中)?

我在表格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> 

和窗口2一個對話框:

<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 但它也不起作用。

感謝您的幫助。

+0

你的意思是你嘗試過'update =「:form1:oneMenu」,它不起作用? 'rendered =「#」'你的代碼中有一個錯字嗎? –

+0

雅,我試過'update =「:form1:oneMenu」'和'update =「form1:oneMenu」'。我不知道爲什麼,還有什麼是渲染=「#」。我正在開發一個現有的代碼,並且我是JSF中的新成員。 – Johnny2012

+0

相關:[如何在Ajax中引用組件?](http://stackoverflow.com/a/8644762/157882) – BalusC

回答

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"}; 
    } 
} 

注:如果輸入值AAABBB,在對話框CCCDDDselectedOneMenu值將被改變。

+0

嘗試了你的第二個,現在沒有更多的錯誤。感謝那。但它仍不會更新菜單。 :S我也在菜單標記 – Johnny2012

+0

@cycdemo中設置了autoupdate =「true」,這不是你在getCities()中的建議,有點不安全?除了不好的做法之外,JSF也不會忽略綁定到一個改變midcycle的值的命令組件上的提交很可能會導致一個'Validation error:value not valid'錯誤 – kolossus

相關問題