8

My requirement is like this. I have a selectMenu with some values (Examples: Engineering, Medicine, Law etc..,) . Suppose if I select Engineering in the drop down, I want another dropdown menu created dynamically which has values related to Engineering (Example: Electronics, Computers, Electricals etc..,). How do I achieve this in JSF 2.0 ?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Check our `[selectonemenu]` wiki page for more hints how to get most of those components: http://stackoverflow.com/tags/selectonemenu/info (you can access it by hovering the `[selectonemenu]` tag which I placed below your question until a black box shows up and then clicking therin the *info* link). – BalusC Sep 04 '13 at 13:30

1 Answers1

9

You need to perform an ajax request when first h:selectOneMenu's selection change. This request will update the selectable items in the second h:selectOneMenu. After the ajax request, you must render the second h:selectOneMenu again, with the updated values.

Page:

<h:selectOneMenu value="#{bean.selectedSubject}">
    <f:ajax listener="#{bean.changeSubject}" render="speciality_selection" />
    <f:selectItems value="#{bean.subject}" />
</h:selectOneMenu>

<h:selectOneMenu id="speciality_selection" value="#{bean.selectedSpeciality}">
    <f:selectItems value="#{bean.subjectSpecialities}" />
</h:selectOneMenu>

Managed bean:

public void changeSubject(){
    //Loads the specialities depending on the selected subject
    subjectSpecialities = loadSpecialitiesForSubject(selectedSubject);
}
Aritz
  • 30,971
  • 16
  • 136
  • 217