JSP - Help in generating fixed number of link in pagination

放肆的年华 提交于 2019-12-18 17:04:56

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/4517845/jsp-help-in-generating-fixed-number-of-link-in-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!