TookTheRook
TookTheRook

Reputation: 827

JSF: Selecting value in one drop down enables another drop down

Lets say I have two selectOneMenu drop-downs:

Drop-down A:

<h:selectOneMenu value="valA"
immediate="false"                                           
required="true"                                             
valueChangeListener="#{JavaClass.someJavaMethod}"                                           
id="caImplClassSelector"                                            
rendered="#{JavaClass.someOtherMethod}">

Drop-down B:

<h:selectOneMenu value="valB"
immediate="false"                                           
required="true"                                             
valueChangeListener="#{JavaClass.someJavaMethod}"                                           
id="caImplClassSelector"
disabled="what should I write here?"                                            
rendered="#{JavaClass.someOtherMethod}">

How can I make sure that drop down B is disabled until a user selects a value in drop down A? I can make a method within the JavaClass that returns true or false depending on if a value in drop-down A has been selected or not, but is there any way to do the above wihout making that method?

Any help would be appreciated.

Upvotes: 1

Views: 3900

Answers (2)

r0ast3d
r0ast3d

Reputation: 2635

Declare a boolean property in your managed bean and generate the getter and setter methods for it.

By default set it to true.

In <h:selectOneMenu value="valA" immediate="false"
required="true"
valueChangeListener="#{JavaClass.someJavaMethod}"
id="caImplClassSelector"
rendered="#{JavaClass.someOtherMethod}">

In the first drop down based on application logic on the value change listener set the disabled property to false.

In the second drop down refer to the disabled property.

There could be some additional logic that you may have to handle.

Upvotes: 1

BalusC
BalusC

Reputation: 1108722

I have no idea what JSF/RF versions you are using, so here's just a generic answer.

Bind the value of the 1st dropdown to a bean property:

<h:selectOneMenu value="#{bean.firstMenu}">

Then let the disabled attribute of the 2nd dropdown intercept on that:

<h:selectOneMenu disabled="#{bean.firstMenu == null}">

Note that with JSF2/RF4 you don't need the valueChangeListener/immediate hacks which are been used in old ajaxless JSF 1.x versions.

Upvotes: 2

Related Questions