Select default value on h:selectOneMenu

3.9k views Asked by At

I am new on JSF and i am having some problem with h:selectOneMenu I have combo1 with countrys and combo2 with citys, I want to reload combo2 when combo1 changes and always leave combo2 with a default value (--select--). I am using ajax event to reload values and that works fine, but I can't leave the default value I want every time it changes.

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

Edit: Thanks for the responses, but it didnt solve the problem. Let me give you a full a example to make it more clear (my english is bad...) . The problem is when the second selection has the same value than the first one, example. Combo 1 flags: 1.Germany 2.Italy 3.Spain 4.France

Combo2 colors. (selected germany) 1. Black 2. Red. 3. Yellow

I select black!, then I select in combo 1 Italy, as italy flag doesnt have the black color on it it gets back to --Select-- But, if Italy flag would have the black color it reloads the rest of the values but leaves black as selected, instead of get back to --select-- Is it more clear now?

1

There are 1 answers

3
Sebastian Motavita On

You are almost there, just add the itemValue="#{null}" and noSelectionOption="true" attributes to the <f:selectItem> tag.

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

Originally anwered here.