Alexander Rühl
Alexander Rühl

Reputation: 6939

PrimeFaces paginator selection

I have a PrimeFaces (5) DataTable and like to store the page size selection in the view, so that the user sees the same number of rows when he returns to the view.

But all I see from documentation is that there is a page event which can be captured, but which won't give me the currently selected page size. The closest I got was getting the event's source (the DataTable) and get the rows attribute, but it only contains the number of rows before applying the new value.

Here on SO, I found a solution here, but it seems to me not the best way to implement it having to use some inline JQuery stuff.

It seems to me a not-so-exotic feature to persist the page size selection within the session, so there must be a good solution for it, I assume?

Upvotes: 1

Views: 2429

Answers (1)

jp-jee
jp-jee

Reputation: 1523

As you already noticed, the page event is being fired before the selected number of rows has been applied to the table. At the moment, there is no event being fired after changing the number of rows.

However, there is a possible workaround:

  • bind your datatable to the backing bean
  • Use AJAX page-event without any listener. Instead, call a <p:remoteCommand> after ajax request is complete.
  • Add your actionListener method to the <p:remoteCommand>.
  • Within this method, check the datatable's number of rows. It's the new value.
  • Keep in mind, page event is not only fired when changing the number of rows, but also on page change.

Example:

XHTML/JSF

<h:form>
    <p:remoteCommand  name="pageChanged" actionListener="#{tableBean.onPageChanged}"/>
    <p:dataTable binding="#{tableBean.table}" paginator="true"  rowsPerPageTemplate="5,10,20" rows="5">
        <p:ajax event="page" process="@none"  oncomplete="pageChanged()" />
           <!-- columns here -->
    </p:dataTable>
</h:form>

TableBean.java

private DataTable table;

public void onPageChanged(){
   int numRows = table.getRows();
   // ...
}

//getter/setter for table

Upvotes: 3

Related Questions