简体   繁体   中英

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

The base is 0 but your event function is running way to soon it seems.

Try running your function else where other then event="page".

I mean by this, when your page is a 1 (default) and you click for example on page 3, it outputs a 0 (0+1= page 1) and ONLY AFTER THAT it goes to page 3. If you click on page 6 now, it outputs a 2 (2+1= page 3) and ONLY AFTER that it goes to page 6.

This happen even with oncomplete instead of onclick. Hope it helped.

此问题是在PF 6.0和6.1中发现的一个错误,但是在当前版本(6.2)中,此问题已解决。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM