6
votes

I'm trying to implement a table with lazy loading. I think I got all steps from demo page and documentation but I always get a "No records found" message. I think I've reduced code to minimun expression, at least there should be one record :

Tables page:

<h:form id="listaEmpresas">
<p:dataTable id="tablaEmpresas" value="#{empresasTableMB.lazyDataModel}" var="empresa">
                    <p:column>
                        <f:facet name="header">
                            <h:outputText value="#{msgs.empresa_tabla_nombre}"/>
                        </f:facet>
                        <h:outputText value="#{empresa.nombre} "/>
                    </p:column>

</p:dataTable>
</h:form>

LazyDataModel:

@Override
public List<Empresa> load(int first, int pageSize, String sortField, SortOrder so, Map<String, String> filters) {
    List<Empresa> listaEmpresas = new ArrayList();
    Empresa e = new Empresa();
    e.setNombre("Company");
    listaEmpresas.add(e);
    this.setRowCount(1);
    return listaEmpresas;
 }


@Override
public void setRowIndex(int rowIndex) {
    if (rowIndex == -1 || getPageSize() == 0) {
        super.setRowIndex(-1);
    }
    else
        super.setRowIndex(rowIndex % getPageSize());
}

I must override setRowIndex or I get an exception "java.lang.ArithmeticException: / by zero". I'm using primefaces-3.1-SNAPSHOT, jsf 2.0.3, and tomcat 6.0. Please help. What I´m missing?

5
Why are You using so old and snapshot PF version? Try the latest stable version. primefaces.org/downloads.html - alex.parej

5 Answers

2
votes

Add lazy=true in your dataTable. After adding this datatable is able to call your load() method.

1
votes

you need to add field private int rowCount; and then in your load(...) method set value of number of record in your list to this field (rowCount). without this thing <p:dataTable ...> will gain "No record found" and also if you don't specify rows ="10" (for example) attribute it will not render rows!

0
votes

You need to implement LazyDataModel#getRowKey and LazyDataModel#getRowData as well.

Suppose that you have something like:

class Empresa {

    private long id;
    private String nombre;

    // getters and setters...

}

Then:

  • getRowKey returns the id for an Empresa object
  • getRowData gets an Empresa object by id
class MyLazyDataModel {

    // stuff you already have comes here...

    public Empresa getRowData(String rowKey) {
        return empresaRepository.getEmpresaById(Long.valueOf(rowKey));
    }

    public Object getRowKey(Empresa empresa) {
        return empresa.getId();
    }

}
0
votes

This behavior is described in https://code.google.com/p/primefaces/issues/detail?id=1544 and apparently you can avoid it by explicitly calling lazyDataModel.setPageSize();

There is a related post here:

Primefaces DataTable + JPA / Hibernate Pagination

0
votes

you are not calling the load method.first call the load method before the prime face data table declaration.