分页逻辑怎么写? [英] How to write Pagination logic?

查看:31
本文介绍了分页逻辑怎么写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能提供一些想法/逻辑来为我正在处理的搜索页面编写分页逻辑?我拥有的信息是该搜索的总页数 - 每页 10 条记录 我也收到了上一页和下一页的页码(编写逻辑没问题我需要做的就是提取该信息并填充.我还获得了我所在页面的信息.我只能显示 10 个页面,如下所示

假设总页数为 15,当用户点击下一步时,我需要像这样显示

在任何时候我只需要在分页中显示 10 页.

 #set($start = 1)#set($end = $Integer.parseInt($searchTO.getPagination().getNumberofPages()))#set($range = [$start..$end])#set($iter = 1)#foreach($i 在 $range)#foreach($searchTO.getPagination().getDirectPageLinks()中的$link)#if($i == $iter)#if ($Integer.parseInt($searchTO.getPagination().getPageNumber())==$iter)<a class="search_current" href="/?_page=SEARCH&_action=SEARCH$link">$i &nbsp|</a>#别的<a href="/?_page=SEARCH&_action=SEARCH$link">$i &nbsp|</a>#结尾#set($iter = 1)#休息#别的#set($iter=$iter+1)#结尾#结尾#结尾

解决方案

以下是我将如何实现它:创建一个过滤数据并包含分页相关信息的过滤器类通常是一个好主意.我使用这样的东西:

公共抽象类过滤器{/** 当前页码的成员标识符 */私人 int currentPageNo;/** 页面导航中当前起始页码的成员标识符 */私人 int currentStartPageNo;/** 页面导航中当前结束页码的成员标识符 */私人 int currentEndPageNo;/** 页面上元素数量的成员标识符 */私有 int elementsPerPage;/** 导航页数的成员标识符(即 2 到 11 或 3 到 12 等)*/私人 int pageNumberInNavigation;公共抽象查询 createCountQuery();公共抽象查询 createQuery();公共无效 setCurrentPageNo(){//你的代码在这里//验证,变量设置}公共长 getAllElementsCount(){//现在这取决于你使用的持久化框架,这段代码是//仅供参考,并具有类似 Hibernate 的语法查询查询 = createCountQuery();列表列表 = query.list();返回 !list.isEmpty() &&list.get(0) != null ?查询列表().获取(0):0;}公共列表 getElements(){//现在这取决于你使用的持久化框架,这段代码是//仅供参考,并具有类似 Hibernate 的语法查询查询 = createQuery();int from = ((currentPageNo - 1) * elementsPerPage);query.setFirstResult(from);query.setMaxResults(elementsPerPage);//如果你使用Hibernate,你不需要担心空检查,因为如果没有结果则返回一个空集合返回 query.list();}公共列表 getAllElements(){查询查询 = createQuery();返回 query.list();}公共无效刷新(){//你的代码在这里}公共列表下一个(){//如果存在则跳转到下一页setCurrentPageNo(getCurrentPageNo() + 1);获取元素();}公共列表 previoius(){//如果存在则跳转到上一页setCurrentPageNo(getCurrentPageNo() - 1);获取元素();}}

您可以拥有过滤器的特殊子类(取决于您要检索的内容),并且每个子类都将实现它的 createCountQuery()createQuery().>

然后你将你的 Filter 放到 Velocity 上下文中,你可以从这个类中检索你需要的所有信息.

当您设置当前页面时,您会更新您需要的所有其他信息(即 currentStartPageNo、currentEndPageNo).

您还可以使用 refresh() 方法将过滤器恢复到其初始状态.

当然,当用户导航到 Filter 所在的搜索页面时,您当然应该在会话中保留相同过滤器的实例(我的意思是您的 Web 框架,如 Struts、Turbine 等)属于.

这只是一个指导方针,一个想法,它不是完全编写好的可执行代码,只是一个帮助你开始一个方向的例子.

我还向您推荐一个名为 jqGrid 的 jQuery 插件,它具有分页支持(尽管您必须有一个支持才能检索数据)和更多很酷的东西.您可以使用它在网格中显示您的数据.我将它与 Velocity 一起使用没有问题.它有许多非常好的和有用的功能,例如过滤器工具栏、可编辑单元格、JSONXML 中的数据传输等.老实说,我不知道它是否有您需要的分页(我使用它时导航中只显示一个页面,您无法单击页面,只需使用下一个上一个按钮进行导航),但它可能支持

Can anyone provide some idea/logic to write the pagination logic for the search page i am working on? The information i have is total number of pages for that search- 10 records per page also i am been sent the both the previous and next page number(no problem writing the logic all i need to do i pull that info and populate. I am also getting the info which page i am on. I can only display 10 pages like below

<previous 1 |2 |3 | 4| 5 | 6 | 7 | 8 | 9 | 10 next>

Say if total pages are 15 and when user click next then i need to display like this

<previous 2 |3 |4 |5 |6 |7 |8 |9 |10 |11 next>

At any time i just need to show then 10 pages in the pagination.

 #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 &nbsp|</a>
                        #else
                            <a href="/?_page=SEARCH&_action=SEARCH$link">$i &nbsp|</a>
                        #end
                        #set($iter = 1)
                        #break
                    #else
                        #set($iter=$iter+1)
                    #end

                 #end

            #end

解决方案

Here is how I would implement it: It is generally a good idea to create a Filter class that filters data and contains pagination related information for you. I use something like this:

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();
     }

}

You could have special subclasses of filters (depending on what you want to retrieve) and and each subclass would implement it's createCountQuery() and createQuery().

You would put then your Filter to the Velocity context and you could retrieve all the information you need from this class.

When you set the current page pf course you update all the other information that you need (i.e. currentStartPageNo, currentEndPageNo).

You could also have a refresh() method to put the filter back to its initial state.

And of course you should keep the instance of the same filter on the session (I mean you web framework like Struts, Turbine etc.) while the user navigates on the search page to which the Filter belongs.

This is just a guideline, an idea, it is not fully written executable code, just an example to get you started in a direction.

I would also recommend you a jQuery plugin called jqGrid that has pagination support (although you have to have a backed to retrieve data) and a lot more cool stuff. You can use it to display your data in a grid. I use it together with Velocity with no problem. It has a lot of very nice and useful features like filter toolbar, editable cells, data transfer in JSON, XML etc. Honestly, I do not know if it has pagination like you need (I use it with only one page displayed in the navigation and you can not click on a page just use the next a prev buttons to navigate), but it may have support for that.

这篇关于分页逻辑怎么写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆