How to make 3 nested Selectonemenu?

Asked

Viewed 663 times

8

I’m creating a page that has to be 3 SelectOneMenu of the first faces. In the first I will carry a center (of the university), the second will carry an edict and the third would carry the disciplines of that edict.

The first SelectOneMenu works, when I change option it loads only the edits of that option. But when I change the option of the second SelectOneMenu he no longer calls the method. Why isn’t he calling the method? What’s the error? Follow the codes:

deferirInscricoes.xhtml

<h:form id="formInscricoes">

    <p:selectOneMenu id="selectCentro" value="#{inscricaoBean.centroSelecionado}" converter = "centroConverter">
        <f:selectItem itemValue="#{null}" itemLabel="- Selecione um Centro -" />
        <f:selectItems value="#{inscricaoBean.listaCentros}" var="centro" itemValue="#{centro}" itemLabel="#{centro.nome}"/>
        <p:ajax event="change" process="@this" listener="#{inscricaoBean.carregaEditais}" update="selectEdital" />
    </p:selectOneMenu>

    <p:selectOneMenu id="selectEdital" value="#{inscricaoBean.editalSelecionado}" converter = "editalConverter">
        <f:selectItem itemValue="#{null}" itemLabel="- Selecione um Edital -" />
        <f:selectItems value="#{inscricaoBean.listaEditaisPorCentro}" var="edital" itemValue="#{edital}" itemLabel="#{edital.titulo}"/>
        <p:ajax event="change" process="@this" listener="#{inscricaoBean.carregaDisciplinas}" update="selectDisciplinas" />
    </p:selectOneMenu>

    <p:selectOneMenu id="selectDisciplinas" value="#{inscricaoBean.disciplinaSelecionada}" converter = "disciplinaConverter">
        <f:selectItem itemValue="#{null}" itemLabel="- Selecione uma Disciplina -" />
        <f:selectItem itemValue="#{null}" itemLabel="- Teste -" />
        <f:selectItems value="#{inscricaoBean.listaDisciplinaPorEdital}" var="disciplina" itemValue="#{disciplina}" itemLabel="#{disciplina.nome}"/>
    </p:selectOneMenu>  
 <h:form>

inscribed on Bible.java

private List<Centro> listaCentros;
private List<Edital> listaEditaisPorCentro;
private List<Disciplina> listaDisciplinaPorEdital;
CentroDAO centroDAO = new CentroDAO();
EditalDAO editalDAO = new EditalDAO();
DisciplinaDAO disciplinaDAO = new DisciplinaDAO();
private Centro centroSelecionado;
private Edital editalSelecionado;
private Disciplina disciplinaSelecionada;

// Todos os getters e setters acima foram criado no padrão.

public void carregaEditais() {
    System.out.println("Editais carregados.");
    listaEditaisPorCentro = editalDAO.getListaEdital(centroSelecionado);
}

public void carregaDisciplinas() {
    System.out.println("Disciplinas carregadas.");
    listaDisciplinaPorEdital = disciplinasDAO.getListaDisciplinaPorEdital(edital);
}

}

Centroconverter.java

@FacesConverter("centroConverter")
public class centroConverter implements Converter {

    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
            try {
                CentroDAO centroDAO = new CentroDAO();
                Integer id = Integer.parseInt(value);
                Centro centro = centroDAO.getCentro(id);
                return centro;

            } catch(Exception e) {
                System.out.println("Problema no centroConverter.!");
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro de conversão", "Not a valid theme."));
            }
        }
        else {
            return null;
        }
    }

    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
            Centro c = new Centro();
            c = (Centro) object;
            return ""+c.getIdCentro();
        }
        else {
            return null;
        }
    }   
}     

Editalconverter.java

@FacesConverter("editalConverter")
public class editalConverter implements Converter {

    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
            try {
                EditalDAO editalDAO = new EditalDAO();
                Integer id = Integer.parseInt(value);
                Edital edital = editalDAO.getEdital(id);
                return edital;

            } catch(Exception e) {
                System.out.println("Problema no editalConverter.!");
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro de conversão", "Not a valid theme."));
            }
        }
        else {
            return null;
        }
    }

    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
            Edital e = new  Edital();
            e = (Edital) object;
            return ""+e.getIdEdital();
        }
        else {
            return null;
        }
    }   
}     

Disciplinaconverter.java

@FacesConverter("disciplinaConverter")
public class disciplinaConverter implements Converter {

    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
            try {
                DisciplinaDAO disciplinaDAO = new DisciplinaDAO();
                Integer id = Integer.parseInt(value);
                Disciplina disciplina = disciplinaDAO.getDisciplina(id);
                return disciplina;

            } catch(Exception e) {
                System.out.println("Problema no disciplinaConverter.!");
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro de conversão", "Not a valid theme."));
            }
        }
        else {
            return null;
        }
    }

    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
            Disciplina d = new Disciplina();
            d = (Disciplina) object;
            return ""+d.getIdDisciplina();
        }
        else {
            return null;
        }
    }   
}     

The first method it calls, loads the list. But the second does not. I put a print just to test, but the method is not being called.

  • 2

    Have you tried instead of valueChangeListener use <p:ajax>?

  • put <p:ajax> that works

  • @Luídne put the p ajax, but it doesn’t work the same way. He doesn’t call the method at all.

  • @Pedrolaini also.

2 answers

1

I know the post is old, but maybe someone is going through similar problem and follows a very important tip:

I was having similar problem and I broke my head went in several post on the net and the problem was actually in the relationship between Model X converter x selectOneMenu, In order for it to work properly you need "Models" to have the methods equals() and hascode() implemented, in the case of this post would implement in the classes Center, Notice and Discipline.

Not having these methods implemented can cause an error in the JSF validation phase by preventing normal sequence of phases.

0

That atheist valueChangeListener is only called when the user selects an option, and you did not update your second select, so it will not load the new information.

My suggestion add in your first select:

<p:ajax event="change" process="@this" listener="{inscricaoBean.clearEdital}" update="ID_DO_PROX_SELECT" />

remove the valueChangeListener="#{inscricaoBean.clearEdital}" and change its method:

public void clearEdital(){
    listaEditalPorCentro = editalDAO.getListaEdital();
    System.out.println("Centro: "+centroSelecionado);
    System.out.println("Edital: "+listaEditalPorCentro);

}

Your centerSelected will already be with the new value because of process="@this"

  • I did the same way you said, then I select the first select, it executes the method and updates the second, when I select the second it does not call the method and does not store the value in the editalSelected. But the update it works.

  • You did the same thing for the second?

  • Yes yes, I did exactly the same thing, copied and pasted and modified what needed to be modified.

  • Let me see if I understand correctly, when selecting the first option a new list is generated for the second select based on what was selected, and so on?

  • That’s exactly it, in the second select there must be a method that loads the third list (just as it was done in the first).

  • Can you post all the methods you are using? Just to see if any details are missing.

  • I’m on mobile now, waiting for a friend to bring the internet cable and post updated. Beauty?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.