/**
* 分页函数
*
* @param currentPage 当前页数
* @param pageSize 每一页的数据条数
* @param list 要进行分页的数据列表
* @return 当前页要展示的数据
*/
public static Page getPages(Integer currentPage, Integer pageSize, List list) {
Page page = new Page();
int size = list.size();
if (pageSize > size) {
pageSize = size;
}
// 求出最大页数,防止currentPage越界
int maxPage = size % pageSize == 0 ? size / pageSize : size / pageSize + 1;
if (currentPage > maxPage) {
currentPage = maxPage;
}
// 当前页第一条数据的下标
int curIdx = currentPage > 1 ? (currentPage - 1) * pageSize : 0;
List pageList = new ArrayList();
// 将当前页的数据放进pageList
for (int i = 0; i < pageSize && curIdx + i < size; i++) {
pageList.add(list.get(curIdx + i));
}
page.setCurrent(currentPage).setSize(pageSize).setTotal(list.size()).setRecords(pageList);
return page;
}
public static PageInfo pageHelper(List list, Integer pageNum, Integer pageSize) {
Page page = new Page(pageNum, pageSize);
int total = list.size();
page.setTotal(total);
int startIndex = (pageNum - 1) * pageSize;
int endIndex = Math.min(startIndex + pageSize, total);
if (startIndex > endIndex) {
page.addAll(new ArrayList());
PageInfo pageInfo = new PageInfo<>(page);
return pageInfo;
} else {
page.addAll(list.subList(startIndex, endIndex));
PageInfo pageInfo = new PageInfo<>(page);
return pageInfo;
}
}
page pageSize不允许为0