LStrike
LStrike

Reputation: 1648

Prevent primefaces datatable to jump on first page after rendering

Is there a way to prevent the primefaces datatable to jump back on the first page after adding or removing an item?

Example: I have a datatable with a lazy datamodel and pagination. At the moment I have two pages. Now I add one item and after rendering the paginator jumps to the first page.

Is it possible that this not happen?

    <p:dataTable id="persAnwTbl" 
    var="_anweisung"
    value="#{handler.dataModel}"
    style="width:100%"
    widgetVar="persAnwTblVar"
    emptyMessage="#{messages.noData}"
    lazy="true"
    paginatorAlwaysVisible="true"
    paginator="true"
    paginatorTemplate="{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {RowsPerPageDropdown} {NextPageLink} {LastPageLink}"
    currentPageReportTemplate="{totalRecords} #{labels.datensaetze} - #{labels.seite} {currentPage} / {totalPages}"
    rows="#{handler.rows}"
    rowsPerPageTemplate="5,10,15,20,30,50,100,200,500,1000"
    binding="#{handler.dataTable}"
    sortBy="#{_anweisung.module.displayName}"
    filteredValue="#{handler.filteredValues}">
...
...
...
/>

Edit: 2019-05-13 --- 03:05pm

<p:dataTable id="persAnwTbl" var="_anweisung"
                             value="#{handler.dataModel}"
                             style="width:100%"
                             widgetVar="persAnwTblVar"
                             emptyMessage="#{messages.noData}"
                             lazy="true"
                             paginatorAlwaysVisible="true"
                             paginator="true"
                             paginatorTemplate="{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {RowsPerPageDropdown} {NextPageLink} {LastPageLink}"
                             currentPageReportTemplate="{totalRecords} #{labels.datensaetze} - #{labels.seite} {currentPage} / {totalPages}"
                             rows="#{handler.rows}"
                             rowsPerPageTemplate="5,10,15,20,30,50,100,200,500,1000"
                             binding="#{handler.dataTable}"
                             sortBy="#{_anweisung.module.displayName}"
                             filteredValue="#{handler.filteredValues}"
                             first="#{handler.first}">

...
...
...
</p:dataTable>

Upvotes: 1

Views: 941

Answers (2)

jackycflau
jackycflau

Reputation: 1121

Smutje's answer is right, but not suffice for my code to work.(I am using PF 6.0.19 btw).

Fews steps are required for making the whole thing work

  1. Add first attribute to your p:datatable (Smutje's answer)

  2. You have to call DataTable.setFirst() as well in your bean listener method.

    E.g.

    public void addItem(){
        //Your add item code
        //Assume your `first` getter method can be called by handler.getFirst()
        ((DataTable) FacesContext.getCurrentInstance.getViewRoot().findComponent("YOUR_DATATABLE_ID")).setFirst(handler.getFirst());
    } 
    

Upvotes: 0

Smutje
Smutje

Reputation: 18163

Sure, just bind the offset of the first displayed row (i.e., the start of the current page) to a field of your model:

<p:dataTable
 <!-- ... -->
 first="#{handler.first}"
 <!-- ... -->
>
</p:dataTable>

Upvotes: 3

Related Questions