2

I'm working with PrimeFaces 6.0. I'm creating a data table which uses lazy loading. I have set this.setRowCount(count) in my load method. My paginator is not shown correctly though (it only shows disabled previous, next, begin, end buttons).

When I filter my table or change the sorting, then the paginator is showing pages. Also, when I run this JavaScript, the paginator is showing pages:

PF('myTable').clearFilters();

Any ideas on how to get the paginator working without applying hacks?

This is my table (the relevant attributes):

<p:dataTable value="#{controller.lazy}"
             lazy="true"
             var="subitem"
             rowKey="#{subitem.id}"
             paginator="true" paginatorPosition="bottom"
             rows="10"
             sortBy="#{subitem.emailAddress}" sortOrder="ascending"
             widgetVar="myTable"
>

My lazy model (relevant parts):

public class LazyDataModel<T> extends org.primefaces.model.LazyDataModel<T> {

  private final QueryBuilder<T> queryBuilder;

  private final Class<T> type;

  public LazyDataModel(final QueryBuilder<T> queryBuilder, final Class<T> type) {
    this.queryBuilder = queryBuilder;
    this.type = type;
  }

  private void updateRowCount() {
    setRowCount(queryBuilder.count().intValue());
  }

  @Override
  public List<T> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String, Object> filters) {
    if (filters != null) {
      queryBuilder.addFilters(filters);
    }
    if (multiSortMeta != null) {
      multiSortMeta.forEach(s -> queryBuilder.orderBy(s.getSortField(), s.getSortOrder() == SortOrder.ASCENDING));
    }
    updateRowCount();
    TypedQuery<T> typedQuery = queryBuilder.createQuery();
    typedQuery.setMaxResults(pageSize);
    typedQuery.setFirstResult(first);
    return typedQuery.getResultList();
  }

  @Override
  public List<T> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
    List<SortMeta> multiSortMeta = null;
    if (sortField != null && sortOrder != null) {
      multiSortMeta = Arrays.asList(new SortMeta(null, sortField, sortOrder, null));
    }
    return load(first, pageSize, multiSortMeta, filters);
  }

}
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102

1 Answers1

1

With Lazy datatable you need to set the row count using LazyDataModel#setRowCount(int n) also outside the load method, i.e. during the initialization of the bean.

See also:

How to query data for Primefaces dataTable using lazy loading and pagination

Community
  • 1
  • 1
perissf
  • 15,979
  • 14
  • 80
  • 117