0

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?

user3285427
  • 71
  • 2
  • 6

1 Answers1

0

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.

Community
  • 1
  • 1
Sebastian Motavita
  • 355
  • 1
  • 3
  • 9
  • Thanks for the response, 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. – user3285427 Dec 27 '16 at 13:14
  • @SebastianMotavita: If you find a duplicate, you can flag the question as such instead of creating an almost identical answer and splitting 'discussion'. For most 'basic' problems, there already is an answer as you found out.. – Kukeltje Dec 27 '16 at 16:02
  • @user3285427: Well, I thinl that when you are updating the second combo (which has the city value of the bean, maybe a String), it is getting the value of bean.city, which is saved from previous selection, in this case 'Black', maybe the method getCitys is not updating as you which. – Sebastian Motavita Dec 28 '16 at 16:26