2013-04-27 73 views
3

如何在重置類型時按下p:commandButton來重置p:selectOneMenu的值。代碼中,我有如下如何重置primefaces上的下拉命令按鈕重置類型

<h:form id="form"> 

    <p:panelGrid columns="2" cellspacing="10" > 
    <f:facet name="header">Login</f:facet> 
    <p:outputLabel value="Username" /> 
    <p:inputText value="#{user.username}" /> 
    <p:outputLabel value="Password" /> 
    <p:password value="#{user.password}"></p:password> 

    <p:outputLabel value="Locale" /> 
    <p:selectOneMenu > 
    <f:selectItem itemValue="Select Country" itemLabel="Select Country" /> 
    <f:selectItem itemValue="Poland" itemLabel="Poland"/> 
    </p:selectOneMenu> 
    <p:commandButton value="Submit"></p:commandButton> 
    <p:commandButton type="reset" value="Clear" update="form"></p:commandButton> 
    </p:panelGrid> 

</h:form> 

在這樣做時,用戶名和密碼被清除,但對於選擇國家下拉不復位。

回答

3

首先,您只是在p:selectOneMenu中顯示值,但未分配這些值,value屬性表示能夠將當前從客戶端選擇的值分配到支持bean值,因此;

<p:selectOneMenu id="myMenu" value="#{bean.selectedCountry}"> 
    <f:selectItem itemValue="Select Country" itemLabel="Select Country" /> 
    <f:selectItem itemValue="Poland" itemLabel="Poland"/> 
</p:selectOneMenu> 

現在,如果用戶選擇了波蘭國家將設置好的作爲selectedCountry支持bean也不要忘記實現getter和setter方法。

然後,如果你想重置組件的值,p:selectOneMenu生成一個標籤,改變它的文本可以做視圖的伎倆:

<p:commandButton onclick="resetter();" type="reset" value="Clear" update="form"></p:commandButton> 

而且js函數:

function resetter() { 
    document.getElementById('form:myMenu_label').innerHTML = 'Select Country'; 
}