分页逻辑怎么写?

2023-12-27

任何人都可以提供一些想法/逻辑来为我正在处理的搜索页面编写分页逻辑吗? 我掌握的信息是总页数对于该搜索-每页10条记录我还收到了上一页和下一页的页码(编写逻辑没问题,我需要做的就是提取该信息并填充。我还获取我所在页面的信息。我只能显示 10 页,例如以下

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

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

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

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

 #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

这是我将如何实现它: 创建一个 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 Velocitycontext,您可以从此类中检索所需的所有信息。

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

你还可以有一个refresh()方法将过滤器恢复到其初始状态。

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

这只是一个指南,一个想法,它不是完全编写的可执行代码,只是一个让您开始朝着某个方向前进的示例。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

分页逻辑怎么写? 的相关文章

随机推荐

  • 选择除某些行之外的所有行

    假设我有一个具有以下结构的表 id model color 1 Ford yellow 2 Ford green 3 Ford red 4 Ford yellow 5 Subaru yellow 6 Subaru red 我需要进行一个查询
  • linux库问题

    外面的每个人 我正在编写一个 C 代码 当我编译它时 它有一个奇怪的问题 源码没问题 我使用以下选项编译它 gcc above sample c I home hadoop project hadoop 0 20 2 src c libhd
  • 开源 .NET 库可用于在 EDI 中写入 ANSI 837 文件吗? [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我们将数据存储在数据库中 并且需要根据该数据创建 ANSI 837 文件 是否有任何开源框架可以在 NE
  • Android 自动滚动 EditText 提示

    使 edittext 可滚动的问题 https stackoverflow com questions 6250041 problem with making edittext scrollable 有人提到 当 singleLine tr
  • WIF手动生成federationmetadata.xml

    我正在使用 Windows Identity Foundation 并尝试创建基于 MVC NET 的安全令牌服务并将其用作单点登录应用程序 我唯一的问题是我不知道如何生成 federationmetadata xml 文件 有没有什么工具
  • 如何获取topLayoutGuide的高度?

    moviePlayer MPMoviePlayerController contentURL url moviePlayer view frame CGRect x 0 y layoutguide height width self vie
  • 针对外部 Web 服务的 ASP.NET MVC Forms 身份验证

    我正在尝试编写一个 ASP NET MVC 应用程序 它是具有 SOAP Web 服务的 CRM 的前端 我希望用户使用他们的 CRM 用户名和密码登录我的 Web 应用程序 然后针对 CRM 进行身份验证 在页面上进行 Web 服务调用等
  • Android Chrome 自定义选项卡回退

    我正在实施后备 chrome 自定义选项卡 我指的是几个链接它有一些 https stackoverflow com questions 34328814 android open chrome custom tab from fragme
  • 从 C# 通用字典中过滤出值

    我有一本 C 字典 Dictionary
  • 如何将 asyncio 与 boost.python 一起使用?

    可以用Python3吗asyncio封装有Boost Python图书馆 I have CPython C 构建的扩展Boost Python 以及编写的函数C 可以工作很长时间 我想用asyncio调用这些函数但是res await cp
  • 如何将 Func 与返回 IOrderedQueryable 的 IQueryable 结合使用

    我正在做一些关于 EF 的研究 发现了一个接受的函数 Func
  • 调用amazondax服务时出现权限异常

    我正在通过 AWS Lambda 方法使用 amazondax 服务 并收到指示缺少权限的异常 但我不知道为此需要哪些权限 Lambda 方法和我的 DAX 集群都使用相同的 VPC 子网和安全组进行设置 我收到以下异常 ERROR 201
  • 检测 Windows Phone 的方向

    我使用 JavaScript 事件 onorientationchange 和参数 window orientation 来检测网站上的方向变化和方向值 这适用于 iPhone 和 Android 但是 Windows Phone 不会触发
  • 如何通过单击菜单中的 Li 将 HTML 页面加载到 DIV 中?

    我遇到了一个非常烦人的问题 所以 我的计划是制作一个包含不同含量锂的 UL 菜单 当我单击它们中的每一个时 我想将一个新的 HTML 页面加载到我的 Content DIV 中 我做了很多研究 发现了 Ajax 和 Jquery 我尝试了很
  • 重置jquery分页插件中的总页数

    我正在使用 TwbsPagination 插件在我的应用程序中显示分页 当我们在初始化时设置页面大小时 它工作正常 但是 根据搜索结果 我想重置总页数 当我尝试使用 pagination twbsPagination totalPages
  • 这是在 Java 中比较两个文档的最佳方法,没有任何复杂性和精确的结果

    我有两个 word 文档 我想在 java 中比较它们 我尝试使用 md5 哈希码 HashCode newFile Files asByteSource newFileInput hash Hashing md5 HashCode old
  • 如何使用 javascript 设置文本框的值

    我试图从查询字符串中获取一个值并将该值分配到文本框中 我能够从查询字符串中获取值 但无法将其分配给文本框 document getElementByName Contact0Email Value email 尝试了上面的代码 但似乎不起作
  • 使用 JAXB 解组期间覆盖声明的编码

    我有一个 XML 文件 其中设置了编码 但实际上文件是用 UTF 8 编码的 使用 JAXB 解组 XML 文件时是否可以覆盖 XML 文件中声明的编码 您可以从 a 中解组内容java io Reader为了提供实际的编码 Unmarsh
  • 在C中,是否保证数组起始地址小于其他元素的地址?

    换句话说 当做 index array x array 0 是否总是保证 根据 C 标准 array 0 地址顺序是有保证的 关系运算符的行为定义在C11 6 5 8p5 http port70 net nsz c c11 n1570 ht
  • 分页逻辑怎么写?

    任何人都可以提供一些想法 逻辑来为我正在处理的搜索页面编写分页逻辑吗 我掌握的信息是总页数对于该搜索 每页10条记录我还收到了上一页和下一页的页码 编写逻辑没问题 我需要做的就是提取该信息并填充 我还获取我所在页面的信息 我只能显示 10