任何人都可以提供一些想法/逻辑来为我正在研究的搜索页面编写分页逻辑吗?
我所拥有的信息是该搜索的总页数 – 每页10条记录我也被发送了前一页和下一页的编号(没有问题写我需要做的逻辑我拉这些信息并填充.我是也获取我所在页面的信息.我只能显示10页,如下所示
我所拥有的信息是该搜索的总页数 – 每页10条记录我也被发送了前一页和下一页的编号(没有问题写我需要做的逻辑我拉这些信息并填充.我是也获取我所在页面的信息.我只能显示10页,如下所示
- <prevIoUs 1 |2 |3 | 4| 5 | 6 | 7 | 8 | 9 | 10 next>
- <prevIoUs 2 |3 |4 |5 |6 |7 |8 |9 |10 |11 next>
- #set($start = 1)
- #set($end = $Integer.parseInt($searchTO.getPagination().getNumberofPages()))
- #set($range = [$start..$end])
- #set($iter = 1)
- #foreach($i in $range)
- #foreach($link in $searchTO.getPagination().getDirectPageLinks())
- #if($i == $iter)
- #if ($Integer.parseInt($searchTO.getPagination().getPageNumber())==$iter)
- <a class="search_current" href="/?_page=SEARCH&_action=SEARCH$link">$i  |</a>
- #else
- <a href="/?_page=SEARCH&_action=SEARCH$link">$i  |</a>
- #end
- #set($iter = 1)
- #break
- #else
- #set($iter=$iter+1)
- #end
- #end
- #end
解决方法
以下是我将如何实现它:
通常,最好创建一个Filter类来过滤数据并为您包含与分页相关的信息.我使用这样的东西:
通常,最好创建一个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()和createQuery().
然后,您可以将Filter放到Velocity上下文中,然后您可以从此类中检索所需的所有信息.
当您设置当前页面时,您将更新所需的所有其他信息(即currentStartPageNo,currentEndPageNo).
您还可以使用refresh()方法将过滤器恢复到其初始状态.
当然,当用户在Filter所属的搜索页面上导航时,您应该在会话中保留相同过滤器的实例(我的意思是您的Web框架,如Struts,turbine等).
这只是一个指导方针,一个想法,它不是完全编写的可执行代码,只是一个让你开始朝着一个方向前进的例子.
我还建议你一个名为jqGrid的jQuery插件,它具有分页支持(尽管你必须有一个支持检索数据)和更多很酷的东西.您可以使用它在网格中显示数据.我和Velocity一起使用它没问题.它有许多非常好用的功能,如过滤器工具栏,可编辑单元格,JSON数据传输,XML等.老实说,我不知道它是否具有您需要的分页(我只使用导航中显示的一个页面,而您无法点击页面只需使用下一个上一个按钮进行导航),但它可能支持那.