Following is the working example of Dynamic or server side pagination.
Your DataModel class
/**
*
*/
/**
* @author muhammad.shouab
*
*/
import java.util.List;
import javax.faces.model.DataModel;
public class PagedListDataModel extends DataModel {
private int rowIndex = -1;
private int totalNumRows;
private int pageSize;
@SuppressWarnings("unchecked")
private List list;
public PagedListDataModel() {
super();
}
@SuppressWarnings("unchecked")
public PagedListDataModel(List list, int totalNumRows, int pageSize) {
super();
setWrappedData(list);
this.totalNumRows = totalNumRows;
this.pageSize = pageSize;
}
public boolean isRowAvailable() {
if (list == null)
return false;
int rowIndex = getRowIndex();
if (rowIndex >= 0 && rowIndex < list.size())
return true;
else
return false;
}
public int getRowCount() {
return totalNumRows;
}
public Object getRowData() {
if (list == null)
return null;
else if (!isRowAvailable())
throw new IllegalArgumentException();
else {
int dataIndex = getRowIndex();
return list.get(dataIndex);
}
}
public int getRowIndex() {
return (rowIndex % pageSize);
}
public void setRowIndex(int rowIndex) {
this.rowIndex = rowIndex;
}
public Object getWrappedData() {
return list;
}
@SuppressWarnings("unchecked")
public void setWrappedData(Object list) {
this.list = (List) list;
}
}
JSF Component Util to find datatable at runtime
import java.util.Iterator;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
/**
* @author muhammad.shouab
*
*/
public class JSFComponentUtil {
public static UIComponent findComponentInRoot(String id) {
UIComponent component = null;
FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext != null) {
UIComponent root = facesContext.getViewRoot();
component = findComponent(root, id);
}
return component;
}
public static UIComponent findComponent(UIComponent base, String id) {
if (id.equals(base.getId()))
return base;
UIComponent kid = null;
UIComponent result = null;
Iterator kids = base.getFacetsAndChildren();
while (kids.hasNext() && (result == null)) {
kid = (UIComponent) kids.next();
if (id.equals(kid.getId())) {
result = kid;
break;
}
result = findComponent(kid, id);
if (result != null) {
break;
}
}
return result;
}
}
Your Controller Method
public DataModel getPagedDataModel()
{
try
{
HtmlDataTable table = (HtmlDataTable) JSFComponentUtil.findComponentInRoot("yourDataTableID");
Long totalListSize = Your Manager Method which return data List size
List data = Your Manager Method which return data List
PagedListDataModel dataModel = null;
if (totalListSize != null && totalListSize > 0)
{
dataModel = new PagedListDataModel(data, totalListSize.intValue(), table.getRows());
}
return dataModel;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
Your JSF Page
please help me with one example using jsf 2.0 and richfaces 4.0
Server side Pagination using JSF 2.0 Data table and JPA 2.0 and EJB 3.1 with rihcfaces 4.0