JSP - 帮助在分页中生成固定数量的链接 [英] JSP - Help in generating fixed number of link in pagination

查看:28
本文介绍了JSP - 帮助在分页中生成固定数量的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的分页效果很好,但我无法理解如何生成固定数量的页面链接.例如,我需要以这种方式有 5 个固定链接: 1 - 2 - 3 - 4 - 5 >如果我点击第三页,我总是会看到 5 个链接:<3 - 4 - 5 - 6 -7 >

现在使用我的算法我只能生成所有链接,但我不知道如何创建我上面解释的内容.这是我的代码(仅用于生成 href):

<c:if test="${param.pageNumber > 1}"><a href="javascript: previousRecords();"class="previous"><em>previous</em></a></c:if><c:forEach var="i" begin="1" end="${tot + 1}" step="1" varStatus ="status"><a href="javascript: goToPage(${i});"id="paginator${i}" class="pageNumber"><span class="pageNumberRight">${i}</span></a></c:forEach><c:if test="${param.pageNumber < tot}"><a href="javascript: nextRecords();"class="next"><em>next</em></a></c:if>

有人可以帮我吗?非常感谢.

解决方案

事情变得复杂了.

<%-- 当前页面(基于 1)--%><c:set var="l" value="5"/><%--要显示的页面链接数量--%><c:set var="r" value="${l/2}"/><%--前方/后方的最小链路范围--%><c:set var="t" value="${tot}"/><%--总页数--%><c:set var="begin" value="${((p - r) > 0 ? ((p - r) <(t - l + 1) ? (p - r) : (t -l)) : 0) + 1}"/><c:set var="end" value="${(p + r) < t ? ((p + r) > l ? (p + r) : l) : t}"/><c:forEach begin="${begin}" end="${end}" var="page">${页}...</c:forEach>

my pagination works good but I'm not able to understand how generate a fixed number of links to the pages. For example, I need to have 5 fixed links in this way: 1 - 2 - 3 - 4 - 5 > if I click on the third page I will see always 5 links: < 3 - 4 - 5 - 6 -7 >

Now with my algorithm I'm only able to generate all the links, but I have no idea how create what I have explained above. This is my code(only for href generation):

<div class="pageBoxRight">
 <c:if test="${param.pageNumber > 1}">
 <a href="javascript: previousRecords();" class="previous"><em>previous</em></a>
 </c:if>
 <c:forEach var="i" begin="1" end="${tot + 1}" step="1" varStatus ="status">
 <a href="javascript: goToPage(${i});" id="paginator${i}" class="pageNumber"><span class="pageNumberRight">${i}</span></a>
 </c:forEach>
 <c:if test="${param.pageNumber < tot}">
 <a href="javascript: nextRecords();" class="next"><em>next</em></a>
 </c:if>
</div>

Can someone help me? Thanks a lot.

解决方案

It get complicated.

<c:set var="p" value="${param.pageNumber}" /> <%-- current page (1-based) --%>
<c:set var="l" value="5" /> <%-- amount of page links to be displayed --%>
<c:set var="r" value="${l / 2}" /> <%-- minimum link range ahead/behind --%>
<c:set var="t" value="${tot}" /> <%-- total amount of pages --%>

<c:set var="begin" value="${((p - r) > 0 ? ((p - r) < (t - l + 1) ? (p - r) : (t - l)) : 0) + 1}" />
<c:set var="end" value="${(p + r) < t ? ((p + r) > l ? (p + r) : l) : t}" />

<c:forEach begin="${begin}" end="${end}" var="page">
    ${page}...
</c:forEach>

这篇关于JSP - 帮助在分页中生成固定数量的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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