Toggle navigation
VA伐木累
社区
VA伐木累
Proxy
JSON
BASE64
MyGit
登录
注册
×
登录
邮箱
密码
忘记密码?
分页号显示算法
•发布于
•作者
liuzy
•1149 次浏览
•最后一次编辑是
•来自
技术
## JAVA写法 ``` public class Test { static int total = 23;//总页数 static int width = 5;//显示数字个数 public static void main(String[] args) { for (int i = 1; i <= total; i++) { // click(i); click2(i); } } public static void click(int page) { int start = 1; int end = width; if (total > width) { if (page > width / 2) {//大于一半时往右移动 JS写法Math.ceil(width / 2) int move = page - width / 2; start += move; end += move; if (end > total) {//移动后超过了 int out = end - total; end = total; start -= out; } } } else { end = total; } System.out.print(page + ":\t"); for (int i = start; i <= end; i++) { System.out.print(i + "\t"); } System.out.print("\n"); } public static void click2(int page) { int start = 1; int end = width; String left = "";//左边有省略 String right = "";//右边有省略 if (total > width) { if (page > width / 2) {//大于一半时往右移动 JS写法Math.ceil(width / 2) left = "..."; int move = page - width / 2; start += move; end += move; if (end > total) {//移动后超过了 int out = end - total; end = total; start -= out; } } if (end < total) right = "..."; } else { end = total; } System.out.print(page + ":\t"); for (int i = start; i <= end; i++) { if (i == start) { System.out.print(left + i + "\t"); } else if (i == end) { System.out.print(i + right + "\t"); } else { System.out.print(i + "\t"); } } System.out.print("\n"); } } ``` ## 打印结果 ``` 1: 1 2 3 4 5... 2: 1 2 3 4 5... 3: ...2 3 4 5 6... 4: ...3 4 5 6 7... 5: ...4 5 6 7 8... 6: ...5 6 7 8 9... 7: ...6 7 8 9 10... 8: ...7 8 9 10 11... 9: ...8 9 10 11 12... 10: ...9 10 11 12 13... 11: ...10 11 12 13 14... 12: ...11 12 13 14 15... 13: ...12 13 14 15 16... 14: ...13 14 15 16 17... 15: ...14 15 16 17 18... 16: ...15 16 17 18 19... 17: ...16 17 18 19 20... 18: ...17 18 19 20 21... 19: ...18 19 20 21 22... 20: ...19 20 21 22 23 21: ...19 20 21 22 23 22: ...19 20 21 22 23 23: ...19 20 21 22 23 ``` ## JS写法 ``` var total = req.totalPage;//总页数 var width = 7;//显示数字个数 var start = 1; var end = width; if (total > width) { if (page > Math.ceil(width / 2)) {//大于一半时往右移动 var move = page - Math.ceil(width / 2); start += move; end += move; if (end > total) {//移动后超过了 var out = end - total; end = total; start -= out; } } } ``` ## Freemark写法 ``` <#macro pager> <#if page?? && page.numberOfElements gt 0 && page.totalPages gt 1> <#local currentPage=page.number+1/> <#--当前页--> <#local totalPage=page.totalPages/> <#--总页数--> <#local width=7/> <#--显示宽度(标签个数)--> <#local startPage=1/> <#--开始页--> <#local endPage=width/> <#--结束页--> <#if totalPage gt width> <#if currentPage gt (width/2)?ceiling> <#local move=currentPage-(width/2)?ceiling/> <#local startPage+=move/> <#local endPage+=move/> <#if endPage gt totalPage> <#local out=endPage-totalPage/> <#local startPage-=out/> <#local endPage=totalPage/> #if> #if> <#else> <#local endPage=totalPage/> #if>
<#if currentPage==1>
首页
上一页
<#else>
首页
上一页
#if> <#list startPage..endPage as i> <#if i==currentPage>
${i?c}
<#else>
${i?c}
#if> #list> <#if currentPage==totalPage>
下一页
尾页
<#else>
下一页
尾页
#if>
#if> #macro> ```
0 回复
作者
liuzy
积分: 841
“ 黑眼圈圈男 ”
无人回复话题
SonarQube 9.4 + PostgreSQL
shell倒计时
日常网络巧技
使用ssh创建socks5代理服务
NodeJS集群demo
作者其他话题
SonarQube 9.4 + PostgreSQL
shell倒计时
日常网络巧技
使用ssh创建socks5代理服务
NodeJS集群demo
回到顶部
友情链接:
JFinal
©2015 Powered by
jfinalbbs
沪ICP备15012258号