Search code examples
primefacesprimefaces-datatable

Filter and sorting at the time, Primefaces Datatable error


I'm using a normal datatable on primefaces, but have an error when using both sortBy and filterBy at the same time:

javax.faces.FacesException: DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled.

This is how it looks my table:

enter image description here

In the first image we can see how I'm filtering the table, but if I use the sort the error appears:

enter image description here

This is my primefaces code:

<p:dataTable value="#{lastPositionReportController.lastPositionReportDtos}"
                                         editable="true"
                                         paginator="true"
                                         paginatorPosition="bottom"
                                         paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {Exporters}"
                                         id="catalog_table"
                                         selectionMode="single"
                                         emptyMessage="Sin Registros por Mostrar"
                                         var="lastPos"
                                         rowKey="#{lastPos.gpsId}"
                                         rows="20"
                                         rowsPerPageTemplate="20,25,30"
                                         widgetVar="datatable"
                                         styleClass="tbl">
                                <f:facet name="{Exporters}">
                                    <div style="float:right">
                                        <h:commandLink immediate="true">
                                            <p:graphicImage name="img/xl.png" width="24"/>
                                            <pe:exporter type="xlsx" target="catalog_table"
                                                         postProcessor="#{lastPositionReportController.postProcessXLS}"
                                                         fileName="reporte_ultima_posicion"
                                                         facetBackground="#F88017"/>
                                        </h:commandLink>
                                    </div>
                                </f:facet>
                                <p:column headerText="Línea de Transporte" style="width: 10%;" sortBy="#{lastPos.transportLineName}"
                                          filterBy="#{lastPos.transportLineName}" exportable="true"
                                          filterMatchMode="contains">
                                    <f:facet name="header">
                                        Línea de Transporte
                                    </f:facet>
                                    <p:cellEditor>
                                        <f:facet name="output"><h:outputText value="#{lastPos.transportLineName}"/></f:facet>
                                        <f:facet name="input">
                                            <p:inputText value="#{lastPos.transportLineName}"></p:inputText>
                                        </f:facet>
                                    </p:cellEditor>
                                </p:column>
                                <p:column headerText="Número Económico" style="width: 10%;" sortBy="#{lastPos.economicNumber}"
                                          filterBy="#{lastPos.economicNumber}" exportable="true"
                                          filterMatchMode="contains">
                                    <f:facet name="header">
                                        Número Económico
                                    </f:facet>
                                    <p:cellEditor>
                                        <f:facet name="output"><h:outputText value="#{lastPos.economicNumber}"/></f:facet>
                                        <f:facet name="input">
                                            <p:inputText value="#{lastPos.economicNumber}"></p:inputText>
                                        </f:facet>
                                    </p:cellEditor>
                                </p:column>
                                <p:column headerText="GPS ID" style="width: 13%;" sortBy="#{lastPos.gpsId}"
                                          filterBy="#{lastPos.gpsId}" exportable="true"
                                          filterMatchMode="contains">
                                    <f:facet name="header">
                                        GPS ID
                                    </f:facet>
                                    <p:cellEditor>
                                        <f:facet name="output"><h:outputText value="#{lastPos.gpsId}"/></f:facet>
                                        <f:facet name="input">
                                            <p:inputText value="#{lastPos.gpsId}"></p:inputText>
                                        </f:facet>
                                    </p:cellEditor>
                                </p:column>
</p:dataTable>

If I just sort or filter the datatable works fine, this bug appears just when using both at the same time. I investigate the bug and found out something about the rowkey most be unique, and yes, it is, so I don't know exactly what can it be, apreciate any help, regards.

UPDATE

The object class lastPositionReportDto its defined like:

package com.bsdenterprise.qbits.gps.manager.dtos.reports;
import lombok.Data;

@Data
public class LastPositionReportDto {
    private String transportLineName;
    private String economicNumber;
    private String gpsId;
    private String phoneNumber;
    private String distanceBetweenAhmsa;
    private String lastReportTimeStamp;
    private String timeFromLastReportHours;
    private String onRoad;
    private String status;
    private Long deviceId;
    private String uuid;
    private String conclusion;
    private double speed;
}

On my controller I only create a:

List LastPositionReportDto lastPositionReportDtos 

and add lastPositionsReportDtos objects to the list.


Solution

  • filteredValue="#{lastPositionReportController.selectedLastPositionReportDtosList}"
    
    selection="#{lastPositionReportController.selectedLastPositionReportDtos}"
    

    Found that need to add those two lines to my datatable, a list Object and a normal Object, this fix the bug off filter and sorting.

    <p:dataTable value="#{lastPositionReportController.lastPositionReportDtos}"
                                             editable="true"
                                             paginator="true"
                                             paginatorPosition="bottom top"
                                             paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {Exporters}"
                                             id="catalog_table"
                                             selectionMode="single"
                                             emptyMessage="Sin Registros por Mostrar"
                                             var="lastPos"
                                             rowKey="#{lastPos.gpsId}"
                                             rows="20"
                                             filteredValue="#{lastPositionReportController.selectedLastPositionReportDtosList}"
                                             selection="#{lastPositionReportController.selectedLastPositionReportDtos}"
                                             rowsPerPageTemplate="20,25,30"
                                             widgetVar="datatable"
                                             styleClass="tbl">
    <p:datatable>