How to set a Spinner value to an object

Asked

Viewed 294 times

1

I have the following table:

<p:dataTable id="tabelaDisciplinasSelelecionadas" var="disciplina" value="#{disciplinaBean.droppedDisciplinas}"
                                        rendered="#{not empty disciplinaBean.droppedDisciplinas}">

    <p:column headerText="Id">
        <h:outputText value="#{disciplina.idDisciplina}" />
    </p:column>

    <p:column headerText="Nome">
        <h:outputText value="#{disciplina.nomeDisciplina}"/>
    </p:column>

    <p:column headerText="Código">
        <h:outputText value="#{disciplina.codigoDisciplina}" />
    </p:column>

    <p:column headerText="Vagas">
        <h:outputLabel for="vagas" value="Vagas: " />
            <p:spinner id="vagas" value="#{editalDisciplinaBean.editalDisciplina.vagas}"  />
    </p:column>


    <p:column style="width:32px">
        <p:commandButton update=":formDisciplina:display" oncomplete="PF('disciplinaDialog').show()" icon="ui-icon-search">
            <f:setPropertyActionListener value="#{disciplina}"  target="#{disciplinaBean.disciplinaSelecionada}" />
                </p:commandButton>
            </p:column>

</p:dataTable>

Editaldisciplinabean.java

ManagedBean(name="editalDisciplinaBean")
@SessionScoped

public class EditalDisciplinaBean {

    private EditalDisciplina editalDisciplina;
    private EditalDisciplinaDAO editalDisciplinaDAO = new EditalDisciplinaDAO();
    private List<EditalDisciplina> lista;
    private List<EditalDisciplina> listaFiltro;

    private Edital edital;
    private Disciplina disciplina;

    public EditalDisciplina getEditalDisciplina() {
        if (editalDisciplina == null)
            editalDisciplina = new EditalDisciplina();
        return editalDisciplina;
    }

    public void setEditalDisciplina(EditalDisciplina editalDisciplina) {
        this.editalDisciplina = editalDisciplina;
    }

    public EditalDisciplinaDAO getDisciplinaDAO() {
        return editalDisciplinaDAO;
    }

    public void setDisciplinaDAO(EditalDisciplinaDAO disciplinaDAO) {
        this.editalDisciplinaDAO = disciplinaDAO;
    }

    public List<EditalDisciplina> getLista() {
        if (lista == null)
            lista = editalDisciplinaDAO.getListaEditalDisciplina();
        return lista;
    }

    public void setLista(List<EditalDisciplina> lista) {
        this.lista = lista;
    }

    public List<EditalDisciplina> getListaFiltro() {
        return listaFiltro;
    }

    public void setListaFiltro(List<EditalDisciplina> listaFiltro) {
        this.listaFiltro = listaFiltro;
    }

    public Edital getEdital() {
        return edital;
    }

    public void setEdital(Edital edital) {
        this.edital = edital;
    }

    public EditalDisciplinaDAO getEditalDisciplinaDAO() {
        return editalDisciplinaDAO;
    }

    public void setEditalDisciplinaDAO(EditalDisciplinaDAO editalDisciplinaDAO) {
        this.editalDisciplinaDAO = editalDisciplinaDAO;
    }

    public Disciplina getDisciplina() {
        return disciplina;
    }

    public void setDisciplina(Disciplina disciplina) {
        this.disciplina = disciplina;
    }

}

Vacancies is an attribute of my entity Editaldisciplina (which is a many relationship table for many of Notice and Discipline). So for each row of the table there is a Spinner for me to enter as many vacancies for that discipline, but it turns out that the vacancies are not in the discipline, so I can’t use the datatable variable, so when I give the Submit all Editaldiscipline objects get the same number of vacancies. How do I for each Editaldisciplina have their number of vacancies?

  • I’m trying to reproduce your state, but a concept escapes me from understanding... Each EditalDisciplina has a list that stores voids Disciplinas and a list that holds the vacancies of each Disciplina? I see a way for you to do what qr... but I don’t know if it would work. Put a Map inside the Notice, and with a method go adding each wave to its equivalent Discipline. This method should be in the button. Have you tried something like this?

1 answer

1


You can have a method that receives parameters in your bean instead of using property as you are doing.

Then the method would look for the number according to the dysline passed by parameter.

The code would look something like this:

<p:column headerText="Vagas">
    <h:outputLabel for="vagas" value="Vagas: " />
        <p:spinner id="vagas" value="#{editalDisciplinaBean.vagasPorDisciplina(idDisciplina)}"  />
</p:column>

The method can also receive the displin itself as a parameter:

<p:column headerText="Vagas">
    <h:outputLabel for="vagas" value="Vagas: " />
        <p:spinner id="vagas" value="#{editalDisciplinaBean.vagasPorDisciplina(disciplina)}"  />
</p:column>

I calculate that currently editalDisciplinaBean contains an instance of a EditalDisciplina. Instead, it would not have any instance of Editaldiscipline but rather search for this instance according to the parameter passed.

If the id of the dysline was passed, it would be something like this:

public int vagasPorDisciplina(int idDisciplina) {
    return entityManager.find(EditalDisciplina.class, idDisciplina).getSingleResult().getVagas();
}    
  • I’ll try to do it this way and later I give a feedback.

  • How would this method look in my editalDiscipline class?

  • @Joãoneto It’s hard to say considering what I know about your current code in the bean (nothing). I updated the answer with a suggestion. Consider updating your question with the current bean code.

  • I updated the Caffé code. I will have a test now and then I will try it this way. The problem is that so, in my table I have not only one discipline, there are several, so for each discipline in the table he should create a different Editaldiscipline, but it only creates one and arrow the same vacancy for all.

Browser other questions tagged

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