Search code examples
jqueryjsfjsf-2primefacesdatatable

How to jump in Primefaces dataTable to last page from the bean?


I have into my .xhtml a datatable looking like this :

<p:dataTable var="category" value="#{beanView.categories}" paginator="true" rows="5" 
 paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
 rowsPerPageTemplate="5,10" id="categoryDT" styleClass="categoryClass"    
 editable="true" lazy="false" rowKey="#{category.id}">
 <!-- columns -->
<f:facet name="footer">
    <p:commandButton id="btnNewCat" 
      oncomplete="jQuery('.categoryClass').find('span.ui-icon-pencil').last().click();" 
    actionListener="#{beanView.onNewCategory}" 
    update="categoryDT" icon="ui-icon-plus" title="New Category" value="New Category" ></p:commandButton>
</f:facet></p:dataTable>

And my bean beanView :

public void onNewCategory(final ActionEvent actionEvent) {

    try {
        int rowCount = dataTable.getRowCount();
        int rowsPerPage = dataTable.getRows();
        int lastPage = rowCount / rowsPerPage;
        int rowFirst = (lastPage * rowsPerPage);

        if (rowCount >= rowsPerPage) {
            dataTable.setFirst(rowFirst);
            dataTable.setPageLinks(lastPage + 1);
            dataTable.setRowIndex(rowCount - 1);
        } else {
            ....
        }

        ....
     }
 }

But this actually doesnt work! the last page will not be show!

It's possible to Click on the last Page button of my datatable from the bean? or is there another way to solve it!

Thanks for helping


Solution

  • You can achieve this by the oncomplete of the commandButton:

    <p:commandButton oncomplete="PF('dataTableWV').paginator.setPage(PF('dataTableWV').paginator.cfg.pageCount - 1);"/>
    

    Or from the managed bean:

    RequestContext.getCurrentInstance().execute("PF('dataTableWV').paginator.setPage(PF('dataTableWV').paginator.cfg.pageCount - 1);");
    

    Note: dataTableWV is the widgetVar of the dataTable.