java 分页组件_java 代码组装的分页组件

/**

*

* @param totalPage 总页数

* @param cPage 当前页

* @param function 页面上点击页码调用的 js 方法

* @param pageInput 页面上输入页码的 input 框的 id

* @return

*/

public static String pagination(int totalPage, int cPage, String function, String pageInput) {

if (totalPage == 0) {

return "";

}

StringBuffer sb = new StringBuffer();

sb.append("

\n");

boolean c = false; // 是否拼接过当前页标记,防止重复拼接

if (cPage == 1) { // 当前页为第一页时

if (!c) {

sb.append("").append(1).append("\n");

c = !c;

}

} else {

sb.append("").append(1).append("\n");

}

// 页数过多时中间用省略号表示

if (cPage > 3) {

sb.append("...\n");

}

// 拼接当前页的前一页

if (cPage > 2) {

sb.append("").append(cPage - 1).append("\n");

}

if (!c) {

sb.append("").append(cPage).append("\n");

c = !c;

}

// 拼接当前页的后一页

if (cPage < (totalPage - 1)) {

sb.append("").append(cPage + 1).append("\n");

}

if (cPage < (totalPage - 2)) {

sb.append("...\n");

}

if (cPage == totalPage) { // 当前页是最后一页时

if (!c) {

sb.append("").append(totalPage).append("\n");

}

} else {

sb.append("").append(totalPage).append("\n");

}

// 直接跳转输入框和按钮

sb.append("跳转到第\n");

sb.append("\n");

sb.append("页\n");

sb.append("GO!\n");

sb.append("

");

return sb.toString();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Java分页实现的示例代码: ```java public class Paging { private int pageSize; // 每页显示的记录数 private int currentPage; // 当前页码 private int totalCount; // 总记录数 private int totalPage; // 总页数 public Paging(int pageSize, int currentPage, int totalCount) { this.pageSize = pageSize; this.currentPage = currentPage; this.totalCount = totalCount; // 计算总页数 if (this.totalCount % this.pageSize == 0) { this.totalPage = this.totalCount / this.pageSize; } else { this.totalPage = this.totalCount / this.pageSize + 1; } } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } /** * 获取当前页的起始记录索引 * * @return */ public int getStartIndex() { return (this.currentPage - 1) * this.pageSize; } /** * 获取当前页的结束记录索引 * * @return */ public int getEndIndex() { return Math.min(this.currentPage * this.pageSize - 1, this.totalCount - 1); } } ``` 使用方法: ```java // 假设数据库中有100条记录,每页显示10条记录 Paging paging = new Paging(10, 1, 100); System.out.println("总共有" + paging.getTotalPage() + "页"); System.out.println("当前页的起始记录索引为:" + paging.getStartIndex()); System.out.println("当前页的结束记录索引为:" + paging.getEndIndex()); ``` 输出结果: ``` 总共有10页 当前页的起始记录索引为:0 当前页的结束记录索引为:9 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值