如何根据其他选定的值更新选择菜单? [英] How to update a selectonemenu depending on other one selected value?

查看:97
本文介绍了如何根据其他选定的值更新选择菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使我的selectOneMenu内容时遇到麻烦,这取决于彼此选择的值.第一个的内容来自我的数据库中的一个表,并且运行良好,但是第二个的内容应该来自另一个表,但是我无法使其工作.这是我的index.html,我在这里试图证明其工作原理:

I've been having a trouble trying to make my selectOneMenu content, depend on the value selected on the other. The content from the first one comes from a table in my DataBase and works perfectly, but the second one is supposed to come from another table, but I can't make it work. Here is my index.html, where I'm just trying to prove how this works:

        <h:outputLabel value="Estado" styleClass="requiredLbl"/>
        <p:selectOneMenu id="Estado" value="#{beanInscripcion.id_estado}" valueChangeListener="#{beanInscripcion.buscarMunicipios(event)}" >  
            <f:selectItem itemLabel="Elegir Estado" itemValue="" />
            <f:selectItems value="#{beanInscripcion.estados}"  
                           var="edo" itemLabel="#{edo.nombre_estado}" itemValue="#{edo.id_estado}" />  
            <p:ajax update="Municipio"  listener="#{beanInscripcion.buscarMunicipios(event)}" />
        </p:selectOneMenu> 
        <p:separator /> 
        <h:outputLabel value="Municipio" styleClass="requiredLbl"/>
        <p:selectOneMenu id="Municipio" value="municipio">  
            <f:selectItems value="#{beanInscripcion.municipios}"  
                           var="mun" itemLabel="#{mun.nombre_municipio}" itemValue="#{mun.nombre_municipio}" />  
        </p:selectOneMenu>


这是我的Bean部分,我应该在其中获取第二个菜单的内容:


And here is the section of my Bean where I'm supposed to get the content for the second menu:

@ManagedBean(name = "beanInscripcion")
@ViewScoped
public class BeanInscripcion implements Serializable {

    static String strURL;
    private List<Estado> estados; 
    private List<Municipio> municipios;
    private int id_estado;
    public BeanInscripcion() throws SQLException{
            estados = new ArrayList<Estado>();
            buscarEstados();
    }

    public void buscarEstados() throws SQLException {
        Connection connection = getConnection();
        Statement statement = connection.createStatement();
        ResultSet result = statement.executeQuery("SELECT * FROM estado");
        result.beforeFirst();
        while (result.next()) {
            Estado estado = new Estado();
            estado.setId_estado(result.getInt("id_estado"));
            estado.setNombre_estado(result.getString("nombre_estado"));
            estados.add(estado);
        }
    }

    public void buscarMunicipios() throws SQLException {
        Connection connection = getConnection();
        Statement statement = connection.createStatement();
        ResultSet result = statement.executeQuery("SELECT id_municipio, nombre_municipio FROM municipio WHERE Estado_id_estado = '" + id_estado + "'");
        result.beforeFirst();
        while (result.next()) {
            Municipio municipio = new Municipio();
            municipio.setId_municipio(result.getInt("id_municipio"));
            municipio.setNombre_municipio(result.getString("nombre_municipio"));
            municipios.add(municipio);
        }
    }

    public Connection getConnection() {
        try {
            strURL = "jdbc:mysql://localhost:3306/mydb";
            Class.forName("com.mysql.jdbc.Driver");
            return DriverManager.getConnection(strURL, "root", "root");
        } catch (SQLException ex) {
            return null;
        } catch (ClassNotFoundException ex) {
            return null;
        }
    }

    public List<Estado> getEstados() {
        return estados;
    }

    public void setEstados(List<Estado> estados) {
        this.estados = estados;
    }

    public List<Municipio> getMunicipios() {
        return municipios;
    }

    public void setMunicipios(List<Municipio> municipios) {
        this.municipios = municipios;
    }

    public int getId_estado() {
        return id_estado;
    }

    public void setId_estado(int id_estado) {
        this.id_estado = id_estado;
    }
}


我已经为此工作了好几个小时,却一无所获,我真的很着急,如果能在这里给我一些帮助,我将不胜感激.非常感谢您注意:D


I've been working on this for hours and still nothing, I'm really in a hurry here, so I'd appreciate if you give me some help here. Thx a lot for your attention :D

推荐答案

    <p:selectOneMenu id="Municipio" value="municipio">中的
  1. value="municipio"表示该下拉列表中的值将永远不会更改,因为您已经将该字段上的值有效地硬编码为始终为municipio(甚至会失败转换). value属性应绑定到支持bean变量,如

  1. value="municipio" in <p:selectOneMenu id="Municipio" value="municipio"> means that the value in that dropdown will never change as you've effectively hardcoded the value on that field to always be municipio (and even that will fail conversion). The value attribute should be bound to a backing bean variable as in

  <p:selectOneMenu id="Municipio" value="#{beanInscripcion.municipio}" >  
    <f:selectItems value="#{beanInscripcion.municipios}"  
                   var="mun" itemLabel="#{mun.nombre_municipio}" itemValue="#{mun.nombre_municipio}" />  
</p:selectOneMenu>

在您的支持bean中,有

and in your backing bean, have

   Municipio municipio;
  //getter and setter

  • <p:ajax update="Municipio" listener="#{beanInscripcion.buscarMunicipios(event)}" />删除参数event.应该是

  • Remove the parameter event from <p:ajax update="Municipio" listener="#{beanInscripcion.buscarMunicipios(event)}" />. It should be

    <p:ajax update="Municipio"  listener="#{beanInscripcion.buscarMunicipios}" />
    

  • 删除valueChangeListener="#{beanInscripcion.buscarMunicipios(event)}".没必要,因为您已经定义了<p:ajax/>事件

  • Remove valueChangeListener="#{beanInscripcion.buscarMunicipios(event)}". It's unnecessary because you already have a <p:ajax/> event defined

    由于您尚未为该Municipio定制类型创建JSF Converter,最终将遇到提交该表单的问题.如果在所选组件中使用除String类型以外的任何其他内容,则这是强制性的.在此处

    You will eventually run into problems submitting that form because you haven't created a JSF Converter for that Municipio custom type. It's mandatory if you're using anything other than String types in your select components. See a short intro to converters/converting here

    这篇关于如何根据其他选定的值更新选择菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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