0

I want to change the SelectItem[] array in the second SelectOneMenu, if the value is changed in the first one. Is that possible?

Daniel Szalay
  • 4,041
  • 12
  • 57
  • 103

3 Answers3

2

I figured this out, but I used RichFaces's AJAX functionality, not only JSF. Just added the tag to my first selectOneMenu, and it works:)

<a4j:support event="onchange" action="#{bean.onChange}"
             reRender="otherSelectOneMenuID"/>

Thanks for the response anyway!

Daniel Szalay
  • 4,041
  • 12
  • 57
  • 103
  • Glad that you got it working. RichFaces does make it easier to do things than standard JSF! –  Aug 28 '09 at 13:08
1

Should be possible if you bind a value change listener to the first selectOneMenu.

Get the new value from the ValueChangeEvent and update the list accordingly. the JSF page should then display the updated list.

Hope that makes sense!

0

Well I used a4j and it worked.

<code>
//JSF
<h:outputLabel value="First selectOneMenu: "/>
<h:selectOneMenu value="#{yourBackingBean.selectedItem}">
<f:converter converterId="defaultConverter"/>
<f:selectItem id="df01" itemLabel="Item01" itemValue="1" />
<f:selectItem id="df02" itemLabel="Item02" itemValue="2" />
<f:selectItem id="df03" itemLabel="Item03" itemValue="3" />
<a4j:support event="onchange" reRender="secondSelectOneMenu"/> //secondSelectOneMenu is the id of the dropdown you want to change
</h:selectOneMenu>


<h:outputLabel value="Second selectOneMenu: "/>
<h:selectOneMenu value="#{yourBackingBean.attributeToStoreSelectedValue}" id="secondSelectOneMenu">
<f:converter converterId="defaultConverter"/>
<f:selectItem id="df00" itemLabel="Select" itemValue="0" /> //Default value
<f:selectItems value="#{yourBackingBean.returnByChoice}" />
</h:selectOneMenu>


//Converter

public class DefaultConverter implements Converter {
public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
    return value;
}

public String getAsString(FacesContext ctx, UIComponent component, Object value) {
    String label = "";
    if (value != null) {
        label = value.toString();
    }
    return label;
}
}

//Backing Bean Sample
public List<SelectItem> returnByChoice() { //it must return a list of SelectItems so it can be displayed on the jsf page
   String id = (String) getSelectedItem(); //this is the value chosen from the first dropDownMenu wich selectedItem is the attribute onthe binding of the first dropDownMenu.
   ArrayList<SelectItem> arrItems = new ArrayList<SelectItem>();
   if (id != null) {

            List<YourClass> yourObjectList = yourDao.findAllItemsFromType(new Integer(id));

         Iterator<YourClass> iterator = yourObjectList.iterator();
         String tempName = "";
         String tempId = "";
         YourClass tempYourObject = null;

        while (iterator.hasNext()) {
           tempYourObject = iterator.next();
           tempId = String.valueOf(tempYourObject.getId());
           tempName = tempYourObject.getName();
           arrItems.add(new SelectItem(tempId, tempName));
        }
    }
    return arrProfiles;
}
</code>
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
Didask
  • 1