模仿百度后台分页组件

效果如下:
[img]http://dl2.iteye.com/upload/attachment/0092/4120/3f4b4cf6-fd85-37f7-9145-f2fd5b585e50.png[/img]


代码如下:
public String createPageHtml(int totalPages,int pageNo,int showPages,String href) {
String htmlstr = "";
if(totalPages <= 1) {//只有1页,直接返回空串
return htmlstr;
} else {
if(pageNo <= 0 || pageNo > totalPages) {
pageNo = 1;
}

String href1 = href + "?totalPages=" + totalPages + "&showPages=" + showPages + "&pageNo=";

if(pageNo != 1) {
String href2 = href1 + (pageNo-1);
htmlstr = " <div class=\"fontPage\"><a href=\"" +href2 + "\" style=\"text-decoration: none;\"><上一页</a></div> ";
}

int loopCount = showPages;
int mod = totalPages % showPages;
//计算总组数
int a = (mod == 0) ? totalPages / showPages : (totalPages / showPages) + 1;
//这个a1是代表当前pageNo位于第几组
int a1 = pageNo%showPages == 0 ? (pageNo/showPages) : (pageNo/showPages) + 1;
if(a == a1 && mod > 0) {
loopCount = mod;
}

//这个startPage是代表当前pageNo所属组的一个元素编号的变量
//计算公式是: showPages * 第几组 - showPages + 1
int startPage = (showPages * a1) - showPages + 1;

for(int i = 1; i <= loopCount; i++) {
String href3 = href1 + startPage;
String s = " <div class=\"normalPage\"><a href=\"" + href3 + "\" style=\"text-decoration: none;\">" + startPage + "</a></div> ";
if(startPage == pageNo) {
s = " <div class=\"currPage\"><a href=\"" + href3 + "\" style=\"text-decoration: none;\">" + startPage + "</a></div> ";
}
htmlstr = htmlstr + s;
startPage ++;
}
String href4 = href1 + (pageNo+1);
htmlstr = htmlstr + " <div class=\"fontPage\"><a href=\"" + href4 + "\" style=\"text-decoration: none;\">>下一页</a></div> ";
}
return htmlstr;

}


css如下:
.currPage {
float:left;
width: 18px;
height: 18px;
text-align: center;
line-height:18px;
margin-right: 5px;
font-size: 13px;
text-decoration: none;
}

.fontPage {
float:left;
border: 1px solid;
width: 60px;
height: 18px;
border-color: gray;
text-align: center;
line-height:18px;
margin-right: 5px;
font-size: 12px;
}

.normalPage {
float:left;
border: 1px solid;
width: 18px;
height: 18px;
border-color: gray;
text-align: center;
line-height:18px;
margin-right: 5px;
font-size: 13px;
text-decoration: none;
}


调用方式如下:
<%=new PageServlet().createPageHtml(totalPages,pageNo,showPages,href) %>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值