基于Seam/JSF开发,直接使用Rich组件确实是使界面达到较为美观和专业的最简单途径。
一般来说,基于rich:datascroller和rich:dataTable进行配合,马上就实现了表格及分页,但这种分布是一种“伪分页”,即:只是表现层分页,在数据层并没有进行分页查询。
下面给出一个我在使用的支持数据层分布查询的方法:
基类:
public abstract class BaseExtendedDataModel<T, ID extends Serializable> extends ExtendedDataModel {
@Logger
private Log log;
public List<T> listRow = null;
private int firstRow_old = 0;
private ID currentId;
private Map<ID, T> wrappedData = new HashMap<ID, T>();
private Long rowCount; // better to buffer row count locally
public abstract Long getCount();
public abstract List<T> getList(Integer firstRow, Integer maxResults);
public abstract T findById(ID id);
public void wrap(FacesContext context, DataVisitor visitor, Range range, Object argument, List<T> list) throws IOException {
wrappedData = new HashMap<ID, T>();
for (T row : list) {
ID id = null;
try {
id = (ID) PropertyUtils.getProperty(row, "id");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
// wrappedKeys.add(id);
wrappedData.put(id, row);
visitor.process(context, id, argument);
}
}
public boolean hasById(ID id) {
return wrappedData.get(id) != null;
}
@Override
public void walk(FacesContext context, DataVisitor visitor, Range range, Object argument) throws IOException {
int firstRow = ((SequenceRange) range).getFirstRow();
int maxResults = ((SequenceRange) range).getRows();
if (firstRow != firstRow_old || listRow == null){
listRow = getList(firstRow, maxResults);
firstRow_old = firstRow;
}
wrap(context, visitor, range, argument, listRow);
}
/*
* This method normally called by Visitor before request Data Row.
*/
@Override
public void setRowKey(Object key) {
this.currentId = (ID) key;
}
@Override
public int getRowCount() {
if (rowCount == null)
return (rowCount = this.getCount()).intValue();
else
return rowCount.intValue();
}
@Override
public boolean isRowAvailable() {
if (currentId == null) {
return false;
} else {
return hasById(currentId);
}
}
/**
* This is main way to obtain data row. It is intensively used by framework. We strongly recommend use of local cache in that method.
*/
@Override
public Object getRowData() {
if (currentId == null) {
return null;
} else {
T ret = wrappedData.get(currentId);
if (ret == null) {
ret = this.findById(currentId);
wrappedData.put(currentId, ret);
return ret;
} else {
return ret;
}
}
}
/**
* Unused rudiment from old JSF staff.
*/
@Override
public Object getWrappedData() {
throw new UnsupportedOperationException();
}
private int rowIndex = 0;
@Override
public int getRowIndex() {
//throw new UnsupportedOperationException();
return rowIndex;
}
@Override
public void setRowIndex(int rowIndex) {
//throw new UnsupportedOperationException();
this.rowIndex = rowIndex;
}
@Override
public void setWrappedData(Object data) {
throw new UnsupportedOperationException();
}
@Override
public Object getRowKey() {
if (true)
throw new UnsupportedOperationException();
return currentId;
}
}
数据列表类
public class PeopleListExtendedDataModel extends BaseExtendedDataModel<People, Long>{
...
@Override
public People findById(Long id) {
return entityManager.find(People.class, id);
}
@Override
public Long getCount() {
return (Long)createQuery(true).getSingleResult();
}
@Override
public List<People> getList(Integer firstRow, Integer maxResults) {
return createQuery(false).setFirstResult(firstRow).setMaxResults(maxResults).getResultList();
}
...
}
页面代码:
<rich:datascroller align="left" for="peopleList" maxPages="20" rendered="#{peopleListExtendedDataModel.size>20}"/>
<rich:dataTable width="100%" id="peopleList" rows="20" columnClasses="col"
value="#{peopleListExtendedDataModel}" var="people">
分享到:
相关推荐
对DataTable分页获取数据
这是我用来测试的一个工程,都不能在rich:dataTable中使用action actionListener 博文链接:https://insomniask.iteye.com/blog/111030
对DataTable进行分页,对DataTable进行分页
分页、排序、检索 Datatable与json之间数据转换,一个详细完整的例子!,希望对大家有用。
datatable分页,控件,全源码开放
DataTable分页
JSF2.xdatatable分页控件与左侧菜单最简单应用 JSF2.x,功能强大,使用方便。全世界使用JSF的人越来越多。而且也有很多很好的控件给发出来了,如:richfaces、primefaces... 至此,分页已经实现。下面是使用截图:
在实际编程工程中,常常遇到这样的情况:DataTable并不是数据库中的,或者DataTable尚未写到数据库,或者从数据库中读出的DataTable已经在本地被改动,又没有写回数据库(可能还要作其他改动),在这些情况下,其实...
《Visual Studio2005入门.Net系列视频教程》 <br/> 本系列教程的课程安排参考了多本.net 1.1以及在目前的2.0新书的基础上认真编订。不敢夸口本系列多了不起,但至少敢保证你看完本系列,再做完每个小结的...
运用page.jar库里的分页标签可以很快的实现分页,不用考虑具体实现
重新二次封装,让分页代码减少至5行以内,简单方便,并且可扩展操作每行每列的数据
angular2-datatable, 带有排序和分页的Angular2简单表组件 table 组件,具有对Angular2进行排序和分页的功能 演示在plunker中检查实时演示安装npm i -S angular2-datatable用法示例AppModule.ts
*jQuery通过json方法获取datatable数据 *将datatable数据转换成table *将table分页显示
datatable实现多级展开和折叠
整理文档,搜刮出一个jquery DataTable实现前后台动态分页,稍微整理精简一下做下分享。 html代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...
jquery datatable serverside page 服务器端分页
*适用于查询,一次加载,减少访问服务器频率 *使用json获取datatable数据 *将datatable转换为table *将table分页 *checkbox全选
asp.net,Datalist控件分页显示数据,绑定datatable 带有向前向后翻页