如何在第一个selectOneMenu更改时加载第二个selectOneMenu? [英] How to load second selectOneMenu on change of first selectOneMenu?

查看:135
本文介绍了如何在第一个selectOneMenu更改时加载第二个selectOneMenu?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个<h:selectOneMenu>组件,其中一个取决于另一个的选择.当选择了第一菜单部件的一个值,然后用所述第一菜单的所述事件的第二变化:

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

它类似于所选国家/地区的国家和城市.第一个菜单的加载方式如下:

It is like 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 load the next menu. I've tried it as follows:

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

    }

    return null;
}

在方法cmbDatos_action()的哪一部分中可以放置代码以加载第二个菜单?

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

推荐答案

valueChangeListener应该引用带有ValueChangeEvent参数并返回void的方法.

The valueChangeListener should refer a method taking ValueChangeEvent argument and returning void.

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

但是您会在验证方面遇到麻烦,因为您基本上是使用onchange="submit()"提交 entire 表单.您还需要将immediate="true"添加到第一个菜单组件.另请参见本文,以获取具有具体示例的详细说明.

But you will run into trouble with regard to validation because you're basically submitting the entire form with onchange="submit()". You'd need to add immediate="true" to the first menu component as well. See also this article for a detailed explanation with concrete examples.

由于您似乎已经在使用JSF 2.x,所以我建议您忘记基于JSF 1.x的解决方法,而直接使用JSF 2.x <f:ajax>方法,该方法非常容易编程.

As you seem to be already using JSF 2.x, I recommend to forget this JSF 1.x based workaround and just go straight with the JSF 2.x <f:ajax> approach which is so much simpler to program.

这是一个基于国家/地区"和城市"示例的启动示例:

Here's a kickoff example, based on "Countries" and "Cities" example:

<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);
    }

    // ...
}

这篇关于如何在第一个selectOneMenu更改时加载第二个selectOneMenu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆