首页 > 其他分享 >PaginationSupport加强版的分页代码

PaginationSupport加强版的分页代码

时间:2023-03-18 11:32:18浏览次数:52  
标签:PaginationSupport 加强版 pageSize int totalCount result return public 分页


PaginationSupport.java,这个类网上有,我把它加强了一下,看下面

import java.util.ArrayList;
import java.util.List;

public class PaginationSupport {
public static int PAGESIZE = 10;

private int pageSize = PAGESIZE;

private List items;

private int totalCount;

private int[] indexes = new int[0];

private int startIndex = 0;

private int lastStartIndex;

public PaginationSupport(){
}

public PaginationSupport(List items, int totalCount) {
setPageSize(PAGESIZE);
setTotalCount(totalCount);
setItems(items);
setStartIndex(0);
}

public PaginationSupport(List items, int totalCount, int startIndex) {
setPageSize(PAGESIZE);
setTotalCount(totalCount);
setItems(items);
setStartIndex(startIndex);
}

public PaginationSupport(List items, int totalCount, int pageSize,
int startIndex) {
setPageSize(pageSize);
setTotalCount(totalCount);
setItems(items);
setStartIndex(startIndex);
}

public List getItems() {
return items;
}

public void setItems(List items) {
this.items = items;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getTotalCount() {
return totalCount;
}

public void setTotalCount(int totalCount) {
if (totalCount > 0) {
this.totalCount = totalCount;
int count = totalCount / pageSize;
if (totalCount % pageSize > 0)
count++;
indexes = new int[count];
for (int i = 0; i < count; i++) {
indexes[i] = pageSize * i;
}
} else {
this.totalCount = 0;
}
}

public int[] getIndexes() {
return indexes;
}

public void setIndexes(int[] indexes) {
this.indexes = indexes;
}

public int getStartIndex() {
return startIndex;
}

public void setStartIndex(int startIndex) {
if (totalCount <= 0)
this.startIndex = 0;
else if (startIndex >= totalCount)
this.startIndex = indexes[indexes.length - 1];
else if (startIndex < 0)
this.startIndex = 0;
else {
this.startIndex = indexes[startIndex / pageSize];
}
}

public int getNextIndex() {
int nextIndex = getStartIndex() + pageSize;
if (nextIndex >= totalCount)
return getStartIndex();
else
return nextIndex;
}

public int getPreviousIndex() {
int previousIndex = getStartIndex() - pageSize;
if (previousIndex < 0)
return 0;
else
return previousIndex;
}

public int getLastIndex(){
int[] indexes = this.getIndexes();
if(indexes != null && indexes.length >0){
lastStartIndex = indexes[indexes.length-1];
}
return lastStartIndex;

}

// 一共多少页
public int getPages(){
if(getTotalCount()%pageSize==0){
return getTotalCount()/pageSize;
}
return (getTotalCount()/pageSize)+1;
}
//当前第几页
public int getCurrentPage(){
return (getStartIndex()/pageSize)+1;
}

//感谢dengyin2000提供如下算法
/**
* 记录显示的页码组
* @param totalPages
* @param currentPage
* @return
*/
public ArrayList<Integer> page(int totalPages, int currentPage) {
int adjacents = 3;//这个参数可以调ArrayList的长度,试试就知道了
ArrayList<Integer> result = new ArrayList<Integer>();
if (totalPages < (5 + (adjacents * 2))){ // not enough links to make it worth breaking up
writeNumberedLinks(1, totalPages, result);
}else{
if ((totalPages - (adjacents * 2) > currentPage) && (currentPage > (adjacents * 2))){ // in the middle
writeNumberedLinks(1, 1, result);
writeElipsis(result);
writeNumberedLinks(currentPage - adjacents - 1, currentPage + adjacents, result);
writeElipsis(result);
writeNumberedLinks(totalPages, totalPages, result);
}else if (currentPage < (totalPages / 2)){
writeNumberedLinks(1, 3 + (adjacents * 2), result);
writeElipsis(result);
writeNumberedLinks(totalPages, totalPages, result);
}else{ // at the end
writeNumberedLinks(1, 1, result);
writeElipsis(result);
writeNumberedLinks(totalPages - (2 + (adjacents * 2)), totalPages, result);
}
}
return result;
}

/**
* @param result
*
*/
private void writeElipsis(ArrayList<Integer> result) {
result.add(-1);//-1是用来打点的
}

/**
* @param i
* @param lastIndex
* @param result
*/
private void writeNumberedLinks(int i, int lastIndex, ArrayList<Integer> result) {
for (int d=i; d <= lastIndex; d++) {
result.add(d);
}
}

}



在jsp中的用法:


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html:hidden property="startIndex"/>
<table width="100%" border="0" cellpadding="2" cellspacing="0" bordercolor="#4DB0E7" class="TableMain">
<tr bgcolor="#FFFFFF">
<td width="17%">总记录 ${ requestScope.pagination.totalCount } 条
<input title="首页" class="menuList" name="1" type="submit" onClick="this.document.forms[0].startIndex.value='0';" value="首 页">
<input title="上一页" class="menuList" name="1" type="submit" onClick="this.document.forms[0].startIndex.value=${ requestScope.pagination.previousIndex };" value="上一页">
<c:forEach var="i" items="${requestScope.pages}">
<c:choose>
<c:when test="${i==-1}" >
...
</c:when>
<c:when test="${i==requestScope.pagination.currentPage}" >
<a href="userManage.portal?action=listUser&page=${ i }" target="mainFrame" style="color:red;">${ i }</a>
</c:when>
<c:otherwise>
<a href="userManage.portal?action=listUser&page=${ i }" target="mainFrame" >${ i }</a>
</c:otherwise>
</c:choose>
</c:forEach>
<input title="下一页" class="menuList" name="1" type="submit" onClick="this.document.forms[0].startIndex.value=${ requestScope.pagination.nextIndex };" value="下一页">
<input title="尾页" class="menuList" name="1" type="submit" onClick="this.document.forms[0].startIndex.value=${ requestScope.pagination.lastIndex };" value="尾页">
共有${ requestScope.pagination.pages }页,当前是第${ requestScope.pagination.currentPage }页
</td>
</tr>
</table>




上面的首页,上一页,下一页,尾页我使用了submit提交,其实也可以用<a href="...">


只是我一开始做的时候只有首页,上一页,下一页,尾页四个button,没有考虑中间的1,2,3,4,5,6....所以有些混乱,勉强看吧。


下面的是在action中用法:


String action=request.getParameter("action");
if("listUser".equals(action)){
String page=request.getParameter("page");
if(page!=null){
int p=Integer.parseInt(page);
form.setStartIndex((p-1)*Constant.PAGESIZE);//p总是从1开始,但记录从0开始,所以p-1
}
PaginationSupport pagination=this.getBaseService().findPageByCriteria(detachedCriteria, Constant.PAGESIZE, form.getStartIndex());//该方法网上有,查一下
List list = pagination.getItems();
request.setAttribute("list", list);
request.setAttribute("pagination", pagination);
request.setAttribute("pages", pagination.page(pagination.getPages(), pagination.getCurrentPage()));
return mapping.findForward("listUser");
}



这里要传递的是startIndex的值,所以form中需要定义int startIndex的getter,setter



这个分页我个人认为比较好了,代码比较优美。


类似如下效果:


总记录 108 条 首页 上一页  1 2 3 4 5 ...10 下一页 尾页   共有10页,当前是第1页


标签:PaginationSupport,加强版,pageSize,int,totalCount,result,return,public,分页
From: https://blog.51cto.com/u_5454003/6129458

相关文章

  • 后台所有订单分页查询
    /***backend后台分页查询所有订单*@parampage*@parampageSize*@return*/@GetMapping("/page")publicR<Page<Orders>>a......
  • 订单分页查询
    /***订单分页查询*@parampage*@parampageSize*@return*/@GetMapping("/userPage")publicR<Page<OrdersDto>>page(Inte......
  • sqlserver2008 两种分页操作
    1.有唯一项字段(例如id)SELECTtop分页大小*FROM表名whereidnotin(selecttop(分页大小*(第几页-1))idfrom表名where搜索字段1='aaa'and搜索字段2='bbb'o......
  • Vue利用slice()方法实现分页操作
    Vue利用slice()方法实现分页操作https://blog.csdn.net/pleaseprintf/article/details/129187584系列文章目录提示:这里可以添加系列文章的所有文章的目录,目录需要......
  • MP带条件分页查询
    配置文件@ConfigurationpublicclassMybatisPlusConfig{//分页拦截器,提供逻辑分页,查第几页就是第几页//mybaits是内存分页,将所有数据都查出来再分页@B......
  • lodop打印 table表格分页带表头页码
    lodop.PRINT_INIT("wageSalaryRetireRecordService");varstrBodyStyle="<style>"+document.getElementById("print_style").innerHTML+"</s......
  • ElasticSearch 实现分词全文检索 - Scroll 深分页
    目录ElasticSearch实现分词全文检索-概述ElasticSearch实现分词全文检索-ES、Kibana、IK安装ElasticSearch实现分词全文检索-Restful基本操作ElasticSearch......
  • Qt 利用滚动条分页显示
    Qt利用滚动条分页显示问题:qt控件加载大量数据时初始化时间很长,界面比较卡顿。如QCombobox,QTableView;解决:这些控件都有滚动条,可以利用滚动条进行分页显示。一开始......
  • 分页记录处理
    publicIPagerunPageSqlTF(Pagepage,StringsqlStr){ IPagedataList=baseMapper.runPageSql(page,sqlStr); //返回数据 List<HashMap>queryResultList=n......
  • 套餐信息分页查询
    需求分析:系统中的套餐数据很多的时候,如果在一个页面中全部展示出来会显得比较乱,不便于查看,所以一般的系统中都会以分页的方式来展示数据梳理交互过程:1、页面发......