0

My datatable has about 1000 items in it. If I go to page 5 and then navigate to something else on the page, I need to set the current page for the data that the datatable is trying to collect, but no rows are displayed, because it seems to "remember" the last page it was at.

I'd like to set the current page to the first page in the managed bean - it there a way to do this?

I've seen you can add an event like this in an onclick, but I think my only choice will be to add this to say, the method in my lazyloader - to ensure it's on the first page when there are less than pagesize items to display:

myWidget.getPaginator().setPage(0);

Thanks, Mike

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Mike
  • 609
  • 12
  • 36
  • 1
    Possible duplicate of [Primefaces: set page on datatable](https://stackoverflow.com/questions/10006778/primefaces-set-page-on-datatable) – Kukeltje Feb 21 '19 at 13:18
  • Take advantage of the backing bean. If you want the datatable to remember the position only when you are on the current page, place the backing bean in `@ViewScoped` and bind it to ` – Adam Waldenberg Feb 22 '19 at 11:02
  • Please edit your question to clarify what you are doing an what the issues is. Additionally providing enough source code to form a [mcve] and mentioning version numbers will also help. – Selaron Feb 22 '19 at 11:16

1 Answers1

-1

If it is just that you want to switch Primefaces Datatable to first page from ManagedBean code you can use DataTable.setFirst() to achive this:

Example managed bean:

import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import org.primefaces.component.datatable.DataTable;

@Named
@RequestScoped
public class MyBean {

    private DataTable findDataTable() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        // find the DataTable component from viewRoot
        DataTable dataTable = (DataTable) ctx.getViewRoot().findComponent("myForm:myDataTable");
        return dataTable;
    }

    public void gotoFirstPage() {
        DataTable dataTable = findDataTable();
        // go to first page and initiate data reload.
        dataTable.setFirst(0);
        dataTable.loadLazyData();
    }
    public List<String> getEntries() {
        // ...
    }
}

Example form in XHTML:

<h:form id="myForm">
    <p:dataTable id="myDataTable" value="#{myBean.entries}" var="entry"
        rows="10" paginator="true">
        <p:column>#{entry}</p:column>
    </p:dataTable>

    <p:commandButton value="go to first page!"
        action="#{myBean.gotoFirstPage()}" process="@this" update="@form"/>
</h:form>

Pressing the button would make the dataTable jump to the first page. This gets visible as soo as the table is updated / rendered again.

Selaron
  • 6,105
  • 4
  • 31
  • 39
  • Or use `binding=` on the datatable which prevent traversing the viewroot and makes you less dependend on the JSF Api (not a problem per say but still) – Kukeltje Feb 21 '19 at 13:11
  • Oh and why would the comment in https://stackoverflow.com/questions/18765343/datatable-paginator-setfirst-not-working not be a solution. And https://stackoverflow.com/questions/10006778/primefaces-set-page-on-datatable is a duplicate btw... – Kukeltje Feb 21 '19 at 13:18
  • @Kukeltje You are right, did not research duplicates. Regarding `binding` I thought it's discouraged and might lead to other issues. – Selaron Feb 21 '19 at 15:32
  • The OP is talking about navigation. Hence, this is not a solution - because it only works if the action is invoked. So if the user changes views by either using the browser history or direct address/URL invocation this wont help. The proper solution is to take advantage of the scope of the backing bean - not work against it. – Adam Waldenberg Feb 22 '19 at 10:56