How can I select this Datatable?

Asked

Viewed 160 times

0

I created a datatable of variable columns whose collection takes place from a list of objects with several different attributes. In detail, I have an Avaliaprojeto class, which has a Projeto attribute, a Criterio and a Escala.

The number of columns is given by the number of criteria + 1 (to place the project), where the header is the criteria name and the value is its scale (which is different for each project). However, as they are of many different attributes, I am in intense doubt about how I can select this line for future changes.

Follow the POJO of Avaliaprojeto, the Table code and the Managedbean Code that uses Table.

Datatable

<p:dataTable id="dataTable" class="dataTable" value="#{topsisBean.nProjetos}" rows="10" paginator="true" 
        paginatorPosition="bottom" emptyMessage="Nenhuma avaliação de projeto cadastrada" binding="#{topsisBean.dataTable}"
        selection="?????" selectionMode="single">
        <p:columns value="#{topsisBean.columns}" var="c" headerText="#{c.header}">
            <h:outputText value="#{c.property}" />
        </p:columns>
    </p:dataTable>

POJO

package br.com.somore.model.pojo.topsis;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import br.com.somore.control.SampleEntity;

@Entity
@Table(name="avaliaprojeto", schema="somore")
public class AvaliaProjeto implements Serializable, SampleEntity {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int idAvaliaProjeto;

    @OneToOne
    @JoinColumn(name="idProjeto")
    private Projeto projeto;

    @OneToOne
    @JoinColumn(name="idCriterio")
    private Criterio criterio;

    @OneToOne
    @JoinColumn(name="idEscala")
    private Escala escala;

    double valorCriterio;

    //Gets Sets e Construtor
}

Managedbean

package br.com.somore.control.beans;

//imports

@ManagedBean(name="topsisBean")
@SessionScoped
public class TopsisBean implements Serializable {    

    private static final long serialVersionUID = 1L;

    //DataTable
    DataTable dataTable;
    int nProjetos = 0;


    static public class ColumnModel implements Serializable {

        private static final long serialVersionUID = 1L;

        private String header;
        private String property;

        public ColumnModel(String header, String property) {
            this.header = header;
            this.property = property;
        }

        public String getHeader() {
            return header;
        }

        public String getProperty() {
            return property;
        }
    }

    private int countCriterios() {
        int i = 0;
        Projeto p = avaliaProjetos.get(0).getProjeto();

        for(AvaliaProjeto ap : avaliaProjetos) {
            if(ap.getProjeto().equals(p))
                i++;
        }

        return i;
    }

    private void organizarColumns() {   
        List<AvaliaProjeto> aux = (avaliaProjetos = avaliaProjetoDAO.listar()); 
        int count = countCriterios();
        columns = new ArrayList<>();

        projetos = new ArrayList<>();

        for(AvaliaProjeto ap : aux) {
            //Se não há nenhum projeto, adiciona-se um projeto
            if(projetos.isEmpty()) {
                projetos.add(ap.getProjeto());
                nProjetos++;
            }

            //Se o projeto não está contido, adiciona-se um novo projeto
            if( ! projetos.contains(ap.getProjeto())) {
                projetos.add(ap.getProjeto());
                nProjetos++;
            }
        }

        for(Projeto p : projetos) {
            columns.add(new ColumnModel(
                "Projeto",
                p.getNomeProjeto()
            ));
        }

        //A partir da lista auxiliar de avaliações de projeto, busca-se o critério e escala desejados
        for(int i = 0; i < count; i++) {
            columns.add(new ColumnModel(
                    aux.get(i).getCriterio().getNomeCriterio(),
                    verificarTipoCriterio(aux.get(i))
            ));
        }
    }

    private String verificarTipoCriterio(AvaliaProjeto ap) {
        if(ap.getValorCriterio() != 0)
            return String.valueOf(ap.getValorCriterio());
        else
            return ap.getEscala().getImpactoEscala();
    }
}

Photo

DataTable de Avaliação de Projetos

The ideal would be if there was the possibility to take the whole line of the datatable. I tried to make a Table Binding. But I didn’t see how I could get the selected line there :(.

  • If you need anything else, just ask me to edit.

1 answer

1


To dataTable has attributes precisely to this situation that are the selection="#{topsisBean.projetoSelecionado}" and rowSelectListener="#{topsisBean.onRowSelect}"

And in your Managedbean

private Projeto projetoSelecionado; // + get /set

public void onRowSelect(SelectEvent event) {  
    // Para recuperar o Projeto no click basta fazer isso
    // ((Projeto) event.getObject());  

    // sua ação futura
}

More attributes and other examples just take a look here.

  • Thanks for the clarification Maicon.

Browser other questions tagged

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