How can I make the selection of this DataTable?

0

I've created a datatable of variable columns whose collection occurs from a list of objects with several different attributes. In detail, I have a class ProjectProject, which has a Project attribute, a Criteria, and a Scale.

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

The Project POJO follows, the Table code, and the ManagedBean code used by the 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

The ideal would be if you could get the whole line of the dataTable. I tried to make a binding from the Table. But I have not seen how I can get the selected line there: (.

    
asked by anonymous 07.06.2015 / 23:30

1 answer

1

The dataTable has attributes for this situation that are selection="#{topsisBean.projetoSelecionado}" and rowSelectListener="#{topsisBean.onRowSelect}"

And on 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 at here .

    
07.06.2015 / 23:41