2016-12-26 79 views
0

我是JSF的新手,我遇到了一些問題:selectOneMenu 我有combo1與國家和combo2城市,我想在combo1更改時重新加載combo2,並且總是讓combo2離開默認值( - 選擇 - )。 我正在使用ajax事件來重新加載值並且工作正常,但是我不能在每次更改時都保留默認值。在h上選擇默認值:selectOneMenu

<h:selectOneMenu id="country" value="#{bean.country}"> 
    <f:selectItem itemValue="" itemLabel="--Select--" /> 
    <f:selectItems value="#{bean.getCountrys()}" /> 
    <f:ajax event="change" listener="#{bean.getCitys}" render="city" execute="@this"></f:ajax> 
</h:selectOneMenu> 
<h:selectOneMenu id="city" value="#{bean.city}"> 
    <f:selectItem itemValue="" itemLabel="--Select--" /> 
    <f:selectItems value="#{bean.getCitys()}" /> 
</h:selectOneMenu> 

編輯: 感謝您的答覆,但它並沒有解決問題。讓我給你一個完整的例子,使其更清楚(我的英語不好...)。問題是第二個選擇的值與第一個選擇的值相同時,示例。 組合1國旗: 1.Germany 2.Italy 3.Spain 4.France

Combo2顏色。 (精選德國) 1.黑色 2.紅色。 3.黃色

我選擇黑色!,然後我選擇組合1意大利,因爲意大利國旗不具有黑色,它會返回到 - 選擇 - 但是,如果意大利國旗將會有黑色它會重新加載其餘的值,但會保留選定的黑色,而不是回到 - 選擇 - 現在更清楚了嗎?

回答

0

您幾乎就在那裏,只需將itemValue="#{null}"noSelectionOption="true"屬性添加到<f:selectItem>標記即可。

<h:selectOneMenu id="city" value="#{bean.city}"> 
    <f:selectItem itemValue="#{null}" itemLabel="--Select--" noSelectionOption="true"/> 
    <f:selectItems value="#{bean.getCitys()}" /> 
</h:selectOneMenu> 

最初回答here

+0

感謝您的迴應,但它並沒有解決問題。讓我給你一個完整的例子,使其更清楚(我的英語不好...)。問題是第二個選擇的值與第一個選擇的值相同時,示例。 – user3285427

+0

@ SebastianMotavita:如果您發現重複,您可以將問題標記爲這樣,而不是創建幾乎相同的答案並拆分「討論」。對於大多數'基本'問題,已經有一個答案,因爲你已經發現了.. – Kukeltje

+0

@ user3285427:那麼,當我更新第二個組合(它具有bean的城市值,也許是一個String)時,獲取從之前的選擇中保存的bean.city的值,在這種情況下爲'Black',也許getCitys方法不會像你那樣更新。 –