Search code examples
jsfprimefacesjsf-2

Primefaces datatable and current page not working


I'm using Prime Faces 6.0.

I've a datatable with a LazyDataModel. The user can change the page with the built-in paginator. When the user switch from the first page, to the second I need to know the currentPage, and I need it in the client part.

This is the code of the datatable

<p:dataTable
  id="listingsTable"
  value="#{listingsLazyDataModel}" 
  var="actualAd" 
  paginator="true"
  rows="#{applicationScopedBean.advertisingsPaginationSize}"
  lazy="true"
  paginatorTemplate="{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
  rowsPerPageTemplate="10,20,30"
  paginatorPosition="bottom"
  widgetVar="listingsTableDesktop"
  first="#{searchAdvertisingModel.currentPage*applicationScopedBean.advertisingsPaginationSize}">

...
...
<p:ajax event="page" listener="#{listingsController.pageChanged}" oncomplete="pageChangedOnDetails(PF('listingsTableDesktop'))" />
...
---
</p:dataTable>

and this is the js code

function pageChangedOnDetails(listingsTableDesktop){
    console.log(listingsTableDesktop.paginator.getCurrentPage());
}

The problem is that the getCurrentPage() method returns the previous page. Not the actual one. Just for information, the page is base 0. So the first page is the page 0.

Example of the actual behaviour vs expected one

  • User land in the page and see the first page.
  • User switch to the second page. getCurrentPage -> 0 WRONG expected 1
  • User switch to the third page. getCurrectPage -> 1 WRONG expected 2
  • User switch to the first page. getCurrentPage -> 2 WRONG expected 0

In which way can I solve this? Thank you


Solution

  • This problem was a bug found in PF 6.0 and 6.1, but with the current version (6.2) this problem is solved