How to load the second selectOneMenu when changing the first selectOneMenu?

I have 2 <h:selectOneMenu>components, and one of them depends on the choice of the other. When you select one value of the first component of the menu, the second changes with the event onchange="submit()"and the valueChangeListener="#{Usuario.cmbDatos_action}"first menu:

<h:selectOneMenu id="cmbCombo" binding="#{Usuario.cmbDatos}" value="#{Usuario.id}" 
    onchange="submit()" valueChangeListener="#{Usuario.cmbDatos_action}">
    <f:selectItems value="#{beanCombos.datos}"></f:selectItems>
</h:selectOneMenu>

This is similar to the countries and cities of the selected country. The first menu is loaded as follows:

@ManagedBean
@RequestScoped
public class BeanCombos {

    private List<SelectItem> Datos;

    public BeanCombos() {
        try {
            clsConexion objConexion = new clsConexion();
            String strSQL = "SELECT * FROM Usuarios";            
            objConexion.ResultSetSQL = objConexion.EjecutarConsulta(strSQL);
            Datos = new ArrayList<SelectItem>();

            while (objConexion.ResultSetSQL.next()) {
                Usuario objUsuario = new Usuario();                
                objUsuario.setId(String.valueOf(objConexion.ResultSetSQL.getInt("Codigo")));
                objUsuario.setNombre(objConexion.ResultSetSQL.getString("Nombres").toUpperCase());
                Datos.add(new SelectItem(objUsuario.getId(), objUsuario.getNombre()));
            }
        } catch(Exception ex) {
            String strError = ex.getMessage().toString();            
        }
    }

    public List<SelectItem> getDatos() {
        return Datos;
    }
}

But when I select one value of the first menu, I don’t know how to load the next menu. I tried this as follows:

public String cmbDatos_action() {
    try {
        int intValor = Integer.parseInt(cmbDatos.getValue().toString());
    } catch(Exception ex) {

    }

    return null;
}

In which part of the method cmbDatos_action()can I put the code to load the second menu?

+2
source share
1 answer

valueChangeListenermust refer to a method that takes an argument ValueChangeEventand returns void.

public void cmbDatos_action(ValueChangeEvent event) {
    // ...
}

, onchange="submit()". immediate="true" . . .

, , JSF 2.x, JSF 1.x JSF 2.x <f:ajax>, .

, "" "":

<h:selectOneMenu value="#{bean.country}" converter="countryConverter">
    <f:selectItems value="#{bean.countries}" var="country" itemValue="#{country}" itemLabel="#{country.name}" />
    <f:ajax listener="#{bean.changeCountry}" render="cities" />
</h:selectOneMenu>
<h:selectOneMenu id="cities" value="#{bean.city}" converter="cityConverter">
    <f:selectItems value="#{bean.cities}" var="city" itemValue="#{city}" itemLabel="#{city.name}" />
</h:selectOneMenu>

,

@ManagedBean
@ViewScoped
public class Bean {

    private List<Country> countries;
    private Country country;
    private List<City> cities;
    private City city;

    @EJB
    private DataService service;

    @PostConstruct
    public void init() {
        countries = service.getCountries();
    }

    public void changeCountry() {
        cities = service.getCities(country);
    }

    // ...
}
+10

All Articles