set page on p:datatable

后端 未结 4 591
小蘑菇
小蘑菇 2020-12-14 20:59

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:


...         


        
相关标签:
4条回答
  • 2020-12-14 21:04

    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

    0 讨论(0)
  • 2020-12-14 21:09

    You could avoid the explicit ID in your code by using bindings:

    xhtml:

    <p:dataTable var="webTemplate" id="templateTable" widgetVar="tbl1" binding="#{calculatePageTable.dataTable" />
    

    bean:

    public class CalculatePageTable {
    private DataTable dataTable;
    
    public DataTable getDataTable() {
        return dataTable;
    }
    
    public void setDataTable(DataTable dataTable) {
        this.dataTable = dataTable;
    }
    
    /* See Teg's answer */
    public void setPageDataTable() {
        int first = 1;
        if (dataTable.getRowCount() % ROWS_DATATABLE == 0) {
            first = (dataTable.getRowCount() - ROWS_DATATABLE);
        }
        else 
        {
            first = (dataTable.getRowCount()/ROWS_DATATABLE)*ROWS_DATATABLE;
        }
        dataTable.setFirst(first);
    }
    }
    
    0 讨论(0)
  • 2020-12-14 21:18

    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(){
            ...
        }
    }
    
    0 讨论(0)
  • 2020-12-14 21:24

    if the datatable widgetVar is dataTableWidget then use this:

    <script type="text/javascript">
                $(document).ready(function () {
                    dataTableWidget.paginator.setPage(0);
                });
     </script>
    
    0 讨论(0)
提交回复
热议问题