0
votes

I'm using a p: dataTable selectionMode = "multiple", with Checkbox, but the checkbox does not enable selection. Has anyone ever experienced this ?

<p:dataTable id="gridFaturas" value="#{faturaBean.plano.faturas}" var="fatura"
                            selection="#{faturaBean.faturasSelecionadas}" rowKey="#{fatura.dataVencimento}" selectionMode="multiple"
                            paginatorPosition="bottom" rows="13" paginator="true" style="margin-bottom:0" paginatorAlwaysVisible="false"
                            emptyMessage="Nenhuma Fatura Encontrada.">

                            <f:facet name="header">Faturas</f:facet>

                            <p:column selectionMode="multiple" style="width:25px;text-align:center" />
1
Do you mean that faturasSelecionadas is null ?!?Yagami Light
Please use these guidelines when writing a question: stackoverflow.com/help/mcve500 Server error

1 Answers

1
votes

This works for me (I don't know about your bean model but like I see is a document model)

1. Model

public class Fatura implements Serializable{    
    private long number;
    private Date dataVencimento;
    private String description;

    public Fatura() {
    }

    public Fatura(long number, Date dataVencimento, String description) {
        this.number = number;
        this.dataVencimento = dataVencimento;
        this.description = description;
    }

    public long getNumber() {
        return number;
    }

    public void setNumber(long number) {
        this.number = number;
    }

    public Date getDataVencimento() {
        return dataVencimento;
    }

    public void setDataVencimento(Date dataVencimento) {
        this.dataVencimento = dataVencimento;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 23 * hash + (int) (this.number ^ (this.number >>> 32));
        hash = 23 * hash + Objects.hashCode(this.dataVencimento);
        hash = 23 * hash + Objects.hashCode(this.description);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Fatura other = (Fatura) obj;
        if (this.number != other.number) {
            return false;
        }
        if (!Objects.equals(this.dataVencimento, other.dataVencimento)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Fatura{" + "number=" + number + ", dataVencimento=" +     dataVencimento + ", description=" + description + '}';
    }
 }

2. Controller Bean

@ManagedBean(name="faturaBean", eager = true)
@ViewScoped
public class FaturasBean {

    private List<Fatura> faturasSelecionadas;
    private List<Fatura> faturas;

    public List<Fatura> getFaturasSelecionadas() {
        return faturasSelecionadas;
    }

    public List<Fatura> getFaturas() {
        return faturas;
    }

    public void setFaturas(List<Fatura> faturas) {
        this.faturas = faturas;
    }

    public void setFaturasSelecionadas(List<Fatura> faturasSelecionadas) {
        this.faturasSelecionadas = faturasSelecionadas;
    }

    @PostConstruct
    public void init(){
        faturas = new ArrayList<>();
        faturasSelecionadas = new ArrayList<>();
        faturas.add(new Fatura(1, new Date(), "Example"));
        faturas.add(new Fatura(2, new Date(), "Do"));
        faturas.add(new Fatura(3, new Date(), "Faturas"));

        }
    }

3. View

<h:form id="table">
  <p:dataTable 
    id="gridFaturas" 
    value="#{faturaBean.faturas}" 
    var="fatura"
    selection="#{faturaBean.faturasSelecionadas}" 
    rowKey="#{fatura.number}"
    paginatorPosition="bottom"
    rows="13" 
    paginator="true" 
    style="margin-bottom:0" 
    paginatorAlwaysVisible="false"
    emptyMessage="Nenhuma Fatura Encontrada.">

    <p:ajax event="rowSelect" update=":table:modal" oncomplete="PF('info').show();"/>
    <f:facet name="header">Faturas</f:facet>
    <p:column headerText="Number" width="20%">
        #{fatura.number}
    </p:column>
    <p:column headerText="Description" width="60%">
        #{fatura.description}
    </p:column>
    <p:column width="20%" headerText="Selection" selectionMode="multiple" style="width:25px;text-align:center"></p:column>
  </p:dataTable>

  <p:commandLink update=":table:count" value="Count" />
  <br />
  <h:outputText id="count" value="Total selected: ${faturaBean.faturasSelecionadas.size()}" />

  <p:dialog id="modal" modal="true" widgetVar="info" closable="true">
    <p:panelGrid columns="2">
        <p:outputLabel value="Number"/>
        <p:outputLabel value="#{faturaBean.faturasSelecionadas[0].number}"/>
        <p:outputLabel value="DataVencimiento"/>
        <p:outputLabel value="#{faturaBean.faturasSelecionadas[0].dataVencimento}"/>
        <p:outputLabel value="Description"/>
        <p:outputLabel value="#{faturaBean.faturasSelecionadas[0].description}"/>
    </p:panelGrid>

  </p:dialog>
</h:form>

Please note: The multiple selection is only on last column of table. row is select by a number, this must be unique.

Please, check my test case

enter image description here