Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement LazyDataModel in p:dataTable

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?

like image 448
Hernan Diaz Avatar asked Jan 13 '12 12:01

Hernan Diaz


2 Answers

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

like image 141
V_K Avatar answered Nov 13 '22 05:11

V_K


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!

like image 26
krupet Avatar answered Nov 13 '22 05:11

krupet