tags:

views:

115

answers:

1

Ok I've been stuck with this for a couple of days (understatement).

Say I have a selectOneMenu

    <h:selectOneMenu id="selectFamily" 
         valueChangeListener="#{menuBean.changeFamily}"    
         onclick="submit()" 
         immediate="true" >
        <f:selectItems id="familyNames" value="#{menuBean.familyNames}" />
      </h:selectOneMenu>

And I want to change the options on another selectOneMenu based on the options selected in this previous selectOneMenu.

   <h:selectOneMenu id="selectFunction" immediate="true">
        <f:selectItems id="functions" value="#{menuBean.functions}" />
    </h:selectOneMenu>

How do I do this?Reading around, I have a couple of ideas of how to do it, but I just can't seem to get it right. I'm sorry I'll just dump the bean class here hoping someone can guide me.

public class MenuBean {

/** Creates a new instance of MenuBean */
public MenuBean() {
} 

private SelectItem[] familyNames = {
    new SelectItem((Integer) 1,"Operations"),
    new SelectItem((Integer) 2, "Special"),
    new SelectItem((Integer) 3, "Support")};

public SelectItem[] getFamilyNames() {
    return familyNames;
}

private SelectItem[] functions = {new SelectItem("Select Function")};

private SelectItem[] operationsFunctions = {
    new SelectItem("Air/Ocean Freight"),
    new SelectItem("Customs"),
    new SelectItem("Land Transport"),
    new SelectItem("Logistics/SCM"),
    new SelectItem("Rail Transport"),
    new SelectItem("Special")
};

private SelectItem[] specialFunctions = {
    new SelectItem("General Management"),
    new SelectItem("Quality & Processes")
};

private  SelectItem[] supportFunctions = {
    new SelectItem("Finance/Controlling"),
    new SelectItem("Human Resources"),
    new SelectItem("ICT"),
    new SelectItem("Legal Affairs"),
    new SelectItem("Marketing/Public Relations"),
    new SelectItem("Procurement"),
};

public SelectItem[] getFunctions(int n) {

    if (n==1) {
        functions = operationsFunctions;
    } else if (n==2) {
        functions = specialFunctions;
    } else if (n==3) {
        functions = supportFunctions;
    }

    return functions;
}

public void setFunctions(SelectItem[] function) {
    this.functions = function;
}

public void changeFamily(ValueChangeEvent event) {
    FacesContext context = FacesContext.getCurrentInstance();
    int value = (Integer) event.getNewValue();
    setFunctions(getFunctions(value));

    context.renderResponse();        
}}
+1  A: 
ebaxt
appreciate it ebaxt, but its still not working.
el_zako
I tried it out on my own, see updated answer.
ebaxt
gee thanks it works!the <h:form> tag and Integer.valueOf made the difference.much appreciated. :)
el_zako