set page on p:datatable
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
if the datatable widgetVar is dataTableWidget then use this:
<script type="text/javascript">
$(document).ready(function () {
dataTableWidget.paginator.setPage(0);
});
</script>
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(){
...
}
}