Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set page on p:datatable

I've a datatable in primefaces and I want, when I add a row in it, view the last page of the datatable.

My .xhtml page is:

<h:form id=...>
...
<p:dataTable var="webTemplate" id="templateTable" widgetVar="tbl1"/>
...
</h:form>

<h:form id=...>
...
<p:inputText id="txt_description" value="#{templateController.templateDescription}" label="templateDescription">
               <f:validateLength for="txt_name" minimum="1"/>
               <p:ajax event="change"
                     listener="#{calculatePageTable.setPageTableTemplate}"  onsuccess="setTabIndexTT()"/>
               </p:inputText>
...
</h:form>
<script type="text/javascript">
      function setTabIndexTT(){
                    tbl1.getPaginator().setPage(#{calculatePageTable.pageTableTemplate});
                }
      </script>

bean:

@ManagedBean
@SessionScoped
public class CalculatePageTable {

   private int pageTableTemplate = 0;
   private int pageTableField = 0;

   public void setPageTableTemplate() {

      final DataTable d = (DataTable) FacesContext.getCurrentInstance().getViewRoot()
            .findComponent("form:templateTable");
      pageTableTemplate = d.getPageCount() - 1;

   }

   public int getPageTableTemplate() {
      return pageTableTemplate;
   }

   public void setPageTemplateField() {

      final DataTable d = (DataTable) FacesContext.getCurrentInstance().getViewRoot()
            .findComponent("detailsTable:webTemplateUpdateTable");
      pageTableField = (d.getPageCount() - 1);
   }

   public int getPageTableField() {
      return pageTableField;
   }

}

But the js function setTabIndexTT() is never called by onsuccess ajax...

How do I can set last page of my datatable when adding a row?

Primefaces version is 3.1.1

like image 955
3vi Avatar asked Apr 04 '12 07:04

3vi


3 Answers

In your managed bean you can try this code:

public void setPageDataTable() {
    final DataTable d = (DataTable) FacesContext.getCurrentInstance().getViewRoot()
        .findComponent("form:templateTable");
    int first = 1;
    if (d.getRowCount() % ROWS_DATATABLE == 0) {
        first = (d.getRowCount() - ROWS_DATATABLE);
    }
    else 
    {
        first = (d.getRowCount()/ROWS_DATATABLE)*ROWS_DATATABLE;
    }
    d.setFirst(first);
}

Call this method when you add a new row

like image 191
Teg Avatar answered Nov 05 '22 23:11

Teg


if the datatable widgetVar is dataTableWidget then use this:

<script type="text/javascript">
            $(document).ready(function () {
                dataTableWidget.paginator.setPage(0);
            });
 </script>
like image 7
ehsan Avatar answered Nov 05 '22 23:11

ehsan


I would do it without any JavaScript. Primefaces's datatable has a first attribute, which is the index of the first data to display.

<p:dataTable first="#{calculatePageTable.first}"/>
...
<p:commandButton value="Add a row" action="#{calculatePageTable.addRow}"/>

And your backing bean:

public class CalculatePageTable {
    private int first = 1;

    public int getFirst(){
        return first;
    }

    public void addRow(){
        // 1. your stuff for adding the row 
        ...
        // 2. switch to the row
        first = getFirstRowOnLastPage(); 
    }

    private int getFirstRowOnLastPage(){
        ...
    }
}
like image 2
fischermatte Avatar answered Nov 06 '22 00:11

fischermatte