I have 2 dropdowns: Type & Code. I want the Code dropdown to change values depending on the Type dropdown if value = A or B or C. How can I pass value of A or B or C into listener so it can understand and process my List ?
<h:outputLabel value="Type" for="idType" />
<h:selectOneMenu id="idType" value="#{myController.type}">
<f:selectItem itemLabel="AAA" itemValue="AAA" />
<f:selectItem itemLabel="BBB" itemValue="BBB" />
<f:selectItem itemLabel="CCC" itemValue="CCC" />
<f:ajax event="valueChange" listener="#{myController.changeCodeList}" render="idCode" execute="@this" />
</h:selectOneMenu>
<h:outputLabel value="Code" for="idCode" />
<h:selectOneMenu id="idCode" value="#{myController.code}" >
<f:selectItem itemLabel="Select ..." noSelectionOption="true" />
<f:selectItems value="#{myController.codeList}" />
</h:selectOneMenu>
Implement ValueChangeListener interface and pass the implementation class name to valueChangeListener attribute of UI Component. Let us create a test JSF application to test the valueChangeListener in JSF. Create a project with a name helloworld under a package com.tutorialspoint.test as explained in the JSF - First Application chapter.
Pass the name of the managed bean method in valueChangeListener attribute of UI Component. Implement ValueChangeListener interface and pass the implementation class name to valueChangeListener attribute of UI Component.
Create LocaleChangeListener.java file under a package com.tutorialspoint.test. Modify it as explained below. Modify home.xhtml as explained below. Keep the rest of the files unchanged.
remove the event="valueChange"
from your <f:ajax
or replace it with event="change"
You don't have to pass the value as its already there (in changeCodeList
method)
public void changeCodeList(AjaxBehaviorEvent ev) {
System.out.println(type); //here is your value
//now repopulate your list based on the value
codeList = someMethod(type);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With