java 实现前端分页,上一页,下一页


// 获取count
ArticleSearch search = new ArticleSearch();
search.setType(customerIndustry);
search.setBegin(page - 1);
search.setLength(5);
Integer count = articleService.getCountBySearch(search).getData();
if (count != null && count > 0) {
// 当前页数
model.addAttribute("currentPage", page);
// 总页数
int lengthPage = (int)Math.ceil((double)count/search.getLength());
model.addAttribute("lengthPage", lengthPage);
// 处理中间页数
List<Integer> pageList = handlePage(page, lengthPage);
model.addAttribute("pageList", pageList);
// 是否可点击上一页
model.addAttribute("canViewPre", !page.equals(pageList.get(0)));
// 是否可点击下一页
model.addAttribute("canViewNext", !page.equals(pageList.get(pageList.size() - 1)));
List<ArticleAO> articleList = articleService.getListBySearch(search).getData();
articleService.handleInfo(articleList);
model.addAttribute("articleList", articleList);
}

//
处理中间页数 private List<Integer> handlePage(int currentPage, int lengthPage) { List<Integer> pageList = new ArrayList<>(); int start; int end; // 总共需展示多少页 int sum = 10; // 当前页前面有几页 int front = 5; // 当前页后面有几页 int after = 4; // 计算start if (currentPage - front > 0) { start = currentPage - front; } else { start = 1; after = sum - currentPage; } // 计算end if (lengthPage > sum) { if (currentPage + after > lengthPage) { end = lengthPage; } else { end = currentPage + after; } } else { end = lengthPage; } for (int i = start; i <= end; i++) { pageList.add(i); } return pageList; }

 

posted @ 2021-06-29 16:17  二次元的程序猿  阅读(5215)  评论(0编辑  收藏  举报