这是我将如何实现它:
创建一个 Filter 类来过滤数据并包含与分页相关的信息通常是一个好主意。我用这样的东西:
public abstract class Filter{
/** Member identifier for the current page number */
private int currentPageNo;
/** Member identifier for the current start page number in the page navigation */
private int currentStartPageNo;
/** Member identifier for the current end page number in the page navigation */
private int currentEndPageNo;
/** Member identifier for the number of elements on a page */
private int elementsPerPage;
/** Member identifier for the number of pages you have in the navigation (i.e 2 to 11 or 3 to 12 etc.) */
private int pageNumberInNavigation;
public abstract Query createCountQuery();
public abstract Query createQuery();
public void setCurrentPageNo(){
//Your code here
//Validation, variable setup
}
public Long getAllElementsCount(){
//Now this depends on the presistence framework you use, this code is
//just for guidance and has Hibernate-like syntax
Query query = createCountQuery();
List list = query.list();
return !list.isEmpty() && list.get(0) != null ? query.list().get(0) : 0;
}
public List getElements(){
//Now this depends on the presistence framework you use, this code is
//just for guidance and has Hibernate-like syntax
Query query = createQuery();
int from = ((currentPageNo - 1) * elementsPerPage);
query.setFirstResult(from);
query.setMaxResults(elementsPerPage);
//If you use Hibernate, you don't need to worry for null check since if there are no results then an empty collection is returned
return query.list();
}
public List getAllElements(){
Query query = createQuery();
return query.list();
}
public void refresh(){
//Your code here
}
public List next(){
//Move to the next page if exists
setCurrentPageNo(getCurrentPageNo() + 1);
getElements();
}
public List previoius(){
//Move to the previous page if exists
setCurrentPageNo(getCurrentPageNo() - 1);
getElements();
}
}
您可以有过滤器的特殊子类(取决于您想要检索的内容),并且每个子类都会实现它createCountQuery()
and createQuery()
.
你会把你的Filter
to the Velocity
context,您可以从此类中检索所需的所有信息。
当您设置课程的当前页面时,您会更新所需的所有其他信息(即 currentStartPageNo、currentEndPageNo)。
你还可以有一个refresh()
方法将过滤器恢复到其初始状态。
当然,当用户在搜索页面上导航时,您应该在会话中保留相同过滤器的实例(我的意思是像 Struts、Turbine 等 Web 框架)。Filter
属于.
这只是一个指南,一个想法,它不是完全编写的可执行代码,只是一个让您开始朝着某个方向前进的示例。
我还向您推荐一个名为的 jQuery 插件jqGrid http://www.trirand.com/blog/它具有分页支持(尽管您必须有支持才能检索数据)和许多更酷的东西。
您可以使用它在网格中显示数据。我与它一起使用Velocity
没有问题。它有很多非常好用的功能,例如过滤器工具栏、可编辑单元格、数据传输JSON
, XML
ETC。
老实说,我不知道它是否具有您需要的分页功能(我使用它时导航中仅显示一个页面,您不能单击页面,只能使用下一个上一个按钮进行导航),但它可能支持那。