[英]JSF Primefaces: how to update selectOneMenu(in form1) from dialogBox in form 2?
我想在提交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中的對話框:
<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"
但是它不起作用。 我也閱讀了這篇文章,但它也不起作用。
感謝幫助。
確保使用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:selectOneMenu
或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"};
}
}
注意:如果在對話框中輸入值AAA
, BBB
, CCC
和DDD
,則會更改selectedOneMenu
的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.