Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass value into Listener in selectOneMenu

Tags:

ajax

jsf

jsf-2

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>
like image 856
Peter Avatar asked Nov 06 '12 04:11

Peter


People also ask

How to test the valuechangelistener in JSF?

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.

How to pass the name of the managed bean method to valuechangelistener?

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.

How to create localechangelistener in tutorialspoint?

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.


1 Answers

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);
}
like image 106
Daniel Avatar answered Nov 15 '22 05:11

Daniel