欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

分页方法

程序员文章站 2024-02-20 08:30:52
...

/**
* 分页
* @param al
* @param curPage
* @return
*/
protected HashMap getPageList(ArrayList al, int curPage) {
HashMap result=new HashMap();
int count = al.size();
ArrayList showPage=new ArrayList();
int totalPage=0;
int pageCount = 100;//一页显示的条数
int showPageSize=10;//显示的页数,[1][2][3]
if (count == 0){
result.put("pageSize", String.valueOf(pageCount));
result.put("currentPage", String.valueOf(curPage));
result.put("showPage", showPage);
result.put("totalPage", String.valueOf(totalPage));
result.put("data", al);
return result;
}


int pageLimit = (count - 1) / pageCount + 1;
int begin = 0;
int end = count;
if (curPage * pageCount < count) {
end = curPage * pageCount;
}
if ((curPage - 1) * pageCount > 1 && curPage <= pageLimit) {
begin = (curPage - 1) * pageCount;
}

ArrayList rt = new ArrayList(al.subList(begin, end));

//总页数
if(count%pageCount==0){
totalPage=count/pageCount;
}else{
totalPage=count/pageCount+1;
}

if(curPage>totalPage) curPage=totalPage;



//设置分页显示的数
if(totalPage>showPageSize){
int half=showPageSize/2;//显示分页页数的一半
if(curPage>totalPage-half){//当前页在末尾的几页时
for(int i=totalPage-showPageSize;i<totalPage+1;i++){
showPage.add(String.valueOf(i));
}
}else if(curPage>half){//当前页在中间的几页时
begin=curPage-half;
end=curPage+half;
for(int i=begin;i<end+1;i++){
showPage.add(String.valueOf(i));
}
}else{
for(int i=1;i<showPageSize+1;i++){
showPage.add(String.valueOf(i));
}
}
}else{//总页数小于需要显示的页数,则把页数添加到showPage中
for(int i=1;i<totalPage+1;i++){
showPage.add(String.valueOf(i));
}
}
// System.out.println("size:"+curPage);
result.put("pageSize", String.valueOf(pageCount));
result.put("currentPage", String.valueOf(curPage));
result.put("showPage", showPage);
result.put("totalPage", String.valueOf(totalPage));
result.put("data", rt);
return result;
}

页面:

<!-- 分页 -->
<table width="200px" align="center">
<tr>
<td align="center">
<html:link page="/issueAdmin.do?op=show_all&currentPage=1">首页</html:link>
</td>
<td align="center">
<logic:iterate id="ele" name="showPage">
<logic:equal name="ele" value="${currentPage}"><strong></logic:equal>
<html:link page="/***.do?op=show_all&currentPage=${ele}">[<bean:write name="ele"/>]</html:link>
<logic:equal name="ele" value="${currentPage}"></strong></logic:equal>
</logic:iterate>
</td>
<td align="center">
<html:link page="/***do?op=show_all&currentPage=${taotalPage}">尾页</html:link>
</td>
<td align="center">第<bean:write name="currentPage"/>页</td>
</tr>

</table>
相关标签: 分页