1

I have a Datatable with pagination and I want to change the current page with a commandlink outside the Datatable, how I can get it?

<p:commandLink title="changePage" action="#{myBean.changeMethod}"
   update="myDataTable" />
<p:dataTable id="myDataTable" var="myItem" paginator="true" rows="1"
   value="#{myBean.ListOfItem}" binding="#{myBean.DataTable}">
  ...
  ...
</p:dataTable>

Greetings!

Alavaros
  • 1,665
  • 7
  • 32
  • 52

3 Answers3

16

This will set the page of the table to the first one

public void changeMethod() {
final DataTable d = (DataTable) FacesContext.getCurrentInstance().getViewRoot()
    .findComponent("myDataTable");
int first = 0;
d.setFirst(first);

}

Or on client side:

widgetVar.getPaginator().setPage(pageindex);

EDIT : The "first" attribute indicates the index of the first row to be displayed

Emil Kaminski
  • 1,886
  • 2
  • 16
  • 26
  • Thanks for you answer! It's good to know it, but I really want set any page. The `` is into other datatable, when I click on commandLink, I can know what page number is, Is there any way to set the page with that number? Greetings. – Alavaros Jul 11 '13 at 08:09
  • 1
    Oh Wait!!! All I need is in your answer, I thought that with `setFirst` you change the paginator, I only did `d.setFirst(numberOfPage)`. Thanks!!! PD: Maybe if the method name were something like `setCurrentPage` would be more intuitive. – Alavaros Jul 11 '13 at 08:14
3

It's ok, but now in primefaces 5.1 on the client side you have to do like:

PF('widgetVar').getPaginator().setPage(pageindex);
Ben
  • 51,770
  • 36
  • 127
  • 149
1

For me works very nice, but you have to type the entire component name

FacesContext.getCurrentInstance().getViewRoot().findComponent("form:panel:datatable");