Zaheer Baloch
Zaheer Baloch

Reputation: 536

JSF - Populating a drop down depending on value in other drop down

I need to populate a drop down based on the selected value in another drop down. I can use AJAX requests to do that but I am searching for a solution using JSF only. The values to populate the drop down need to come from a table every time user changes his selection.

Thanks

Upvotes: 1

Views: 12008

Answers (1)

Bozho
Bozho

Reputation: 597402

You didn't specify the version, so I'll assume JSF 1.2. The <a4j:support> tag is part of RichFaces:

<h:selectOneMenu id="firstDropDown" value="#{bean.firstDropDownSelection}">
   <f:selectItems value="#{bean.items}" />
   <a4j:support event="onchange" reRender="secondDropDown" 
       immediate="true" action="#{bean.fetchItems2}" />
</h:selectOneMenu>

<h:selectOneMenu id="secondDropDown" value="#{bean.secondDropDownSelection}">
   <f:selectItems value="#{bean.items2}" />
</h:selectOneMenu>

And in the method bean.fetchItems2 you load your collection items2 with the appropriate items.

What happens, is when the value of the first drop down changes, the second drop down is rerendered and its value is fetched from the server again.

Upvotes: 2

Related Questions