使用PageBean进行分页和Ajax显示

效果图:




PageBean 代码:

/**
 * 
 */
package com.project.bean;

import java.util.List;

/**
 * @author howroad
 * @Date 2018年1月16日
 * @version 1.0
 * @version 1.1 准备删除无参构造器
 */
public class PageBean<T> {
	private List<T> list;
	private int pageNo;
	private int pageSize;
	private int totalPage;
	private int totalRecord;
	public PageBean(List<T> list, int pageNo, int pageSize, int totalRecord) {
		this.list = list;
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.totalRecord = totalRecord;
		this.totalPage = totalRecord % pageSize == 0 ? totalRecord / pageSize : totalRecord / pageSize + 1;
	}
	public boolean hasPrev(int pageNo) {
		return pageNo > 1;
	}
	public boolean hasNext(int pageNo) {
		return pageNo < this.totalPage;
	}
	public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getTotalRecord() {
		return totalRecord;
	}
	public void setTotalRecord(int totalRecord) {
		this.totalRecord = totalRecord;
	}
	@Override
	public String toString() {
		return "PageBean [list=" + list + ", pageNo=" + pageNo + ", pageSize=" + pageSize + ", totalPage=" + totalPage
				+ ", totalRecord=" + totalRecord + "]";
	}
}

Jsp代码:

                    <table class="table table-bordered table-hover table-striped" id="my_table">
                    <thead>
                    <tr>
                        <th>事件名称</th>
                        <th>日期</th>
                        <th>发生位置</th>
                        <th>防治方案</th>
                        <th>灾情状况</th>
                    </tr>
                    </thead>
                    <tbody id="dlog_tbody"></tbody>
                </table>

				<nav class="text-center">
				 	<ul class="pagination" id="page_nav"></ul>
				</nav>
<input type="hidden" id="hid" value="0"/> 

Js代码

	function show_list(pageNo){
		$.ajax({
			method:"post",
			url:"/Disaster/ShowDlogAjaxServlet",
			data:{
				pageNo:pageNo,
				dlogName:$("#dlog_name").val(),
				dlogStage:$("#dlog_stage").val(),
				areaName:$("#area_name").val(),
				startDate:$("#start_date").val(),
				endDate:$("#end_date").val()
			},
			success:function(msg){
				json_to_table(msg,"my_table",["dlogId","dlogName","dlogDate","dlogArea.areaName","dlogPlan",disaster_satge_arr,"dlogStage"]);
				$("#hid").val("0");
			}
		});
	}

JS代码2

			//分页开始
			page_no = jsonArr.pageNo;
			total_page = jsonArr.totalPage;
			create_page_btn(page_no,total_page,"onpage");
			pageNo=page_no;
			//分页结束
			$("#hid").val("");

我封装的Js方法

/*
 * 用于分页的js工具
 * @author:howroad
 * @date:2018年4月10日
html页面必须要有的
1.分页按钮
<nav class="text-center">
	<ul class="pagination" id="page_nav"></ul>
</nav>
2.table id
3.隐藏表单 id:hid
*/

//分页工具
/**
 * 将ajax接受到的msg转化成table的数据
 * @param msg ajaxsuccess中的参数
 * @param table_id_str 字符串格式的table_id
 * @param propertys_arr msg中的list里面的每一个对象的属性名称
 * 		如果需要用数组修饰,在前面加一个数组(很少用),
 * 		如果是对象中的对象,用点来连接即可
 * @returns void
 * 例子:
 * json_to_table(msg,"my_table",["areaId","areaName","areaForest",land,"areaLand","areaAdv","areaClass.clsName"]);
 * json_to_table(msg,"my_table",["dlogId","dlogName","dlogDate","dlogArea.areaName","dlogPlan",disaster_satge_arr,"dlogStage"]);
 * json_to_table(msg,"my_table",["clsId","clsName","clsPerson","clsTel","clsArea.areaName"]);
 * 
 */
function json_to_table(msg,table_id_str,propertys_arr){
	var ojson = JSON.parse(msg);//转换成json对象
	page_no = ojson.pageNo;//当前页码
	var page_size = ojson.pageSize;//分页记录数量
	total_page = ojson.totalPage;//总页数
	var total_record = ojson.totalRecord;//总记录数
	var olist = ojson.list;//需要显示的数据
	var str = "";
	for(var i=0;i<olist.length;i++){
		str +="<tr index="+olist[i][propertys_arr[0]]+">";//ID
		for(var j=1;j<propertys_arr.length;j++){
			if(Array.isArray(propertys_arr[j])){
				str +="<td>"+propertys_arr[j][olist[i][propertys_arr[++j]]]+"</td>";//如果是数组
			}else if(propertys_arr[j].indexOf(".")!=-1){//判断是否有逗号
				var arr=propertys_arr[j].split('.');
				if(olist[i][arr[0]]==null){
					str +="<td></td>";
				}else{
					str +="<td>"+olist[i][arr[0]][arr[1]]+"</td>";//只判断两层即可
				}
				
			}else{
				str +="<td>"+olist[i][propertys_arr[j]]+"</td>";//其他属性
			}
		}
	}
	str += "</tr>";
	//填充表格
	for(var i=0;i<page_size-olist.length;i++){
		str+="<tr style='height:36.8px;'>";
		for(var j = 0;j<$("#"+table_id_str+" thead tr th").length;j++) {
			str+="<td></td>";
		}
		str+="</tr>"
	}	
	$("#"+table_id_str+" tbody").html(str);
	//绑定点击事件
	$("#"+table_id_str+" tbody tr").click(function(){
		$("#"+table_id_str+" tbody tr").removeClass("as");
		$(this).addClass("as");
		$("#hid").val($(this).attr("index"));
	});
	//分页按钮
	create_page_btn(page_no,total_page,"show_list");
}

//分页按钮跳转事件
function jump_page(){
	var page = $("#jump_input").val();
	if(page>=1){
		page=page>total_page?total_page:page;
		show_list(page);
	}else{
		$("#jump_input").val("");
	}
}

//只负责分页按钮显示的
function create_page_btn(page_no,total_page,show_list){
	var str2 = "";
	str2 +="<li "+(page_no==1?"class='active'":"")+"><a href='javascript:"+show_list+"(1)'>第 1 页</a></li>";
	var temp1 = page_no<=1?1:(page_no-1);
	str2 +="<li "+(page_no==1?"class='disabled'":"")+"><a href='javascript:"+show_list+"("+temp1+")'>上一页</a></li>";
	str2 +="<li><a>"+page_no+"</a></li>";
	str2 +="<li><a class='input-group input-group-sm' style='padding:5px 5px'><input type='text' class='form-control' style='width:50px;height:22px' id='jump_input'/></a></li>";
	str2 +="<li><a href='javascript:jump_page()'>跳转</a></li>";
	var temp2 = page_no>=total_page?total_page:(page_no+1);
	str2 +="<li "+(page_no==total_page?"class='disabled'":"")+"><a href='javascript:"+show_list+"("+temp2+")'>下一页</a></li>";
	str2 +="<li "+(page_no==total_page?"class='active'":"")+"><a href='javascript:"+show_list+"("+total_page+")'>第 "+total_page+" 页</a></li>";
	$("#page_nav").html(str2);
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* * @(#)PageControl.java 1.00 2004-9-22 * * Copyright 2004 2004 . All rights reserved. * PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.hexiang.utils; /** * PageControl, 分页控制, 可以判断总页数和是否有上下页. * * 2008-07-22 加入输出上下分页HTML代码功能 * * @author HX * @version 1.1 2008-9-22 */ public class PageBean { /** 每页显示记录数 */ private int pageCount; /** 是否有上一页 */ private boolean hasPrevPage; /** 记录总数 */ private int recordCount; /** 是否有下一页 */ private boolean hasNextPage; /**总页面数 */ private int totalPage; /** 当前页码数 */ private int currentPage; /** * 分页前的页面地址 */ private String pageUrl; /** * 输出分页 HTML 页面跳转代码, 分链接和静态文字两种. * 2008-07-22 * @return HTML 代码 */ public String getPageJumpLinkHtml() { if(StringUtil.isEmpty(pageUrl)) { return ""; } // 检查是否有参数符号, 没有就加上一个? if(pageUrl.indexOf('?') == -1) { pageUrl = pageUrl + '?'; } StringBuffer buff = new StringBuffer("<span id='pageText'>"); // 上一页翻页标记 if(currentPage > 1) { buff.append("[ <a href='" + pageUrl + "&page=" + (currentPage - 1) + "' title='转到第 " + (currentPage - 1) + " 页'>上一页</a> ] "); } else { buff.append("[ 上一页 ] "); } // 下一页翻页标记 if(currentPage < getTotalPage()) { buff.append("[ <a href='" + pageUrl + "&page=" + (currentPage + 1)+ "' title='转到第 " + (currentPage + 1) + " 页'>下一页</a> ] "); } else { buff.append("[ 下一页 ] "); } buff.append("</span>"); return buff.toString(); } /** * 输出页码信息: 第${currentPage}页/共${totalPage}页 * @return */ public String getPageCountHtml() { return "第" + currentPage + "页/共" + getTotalPage() + "页"; } /** * 输出 JavaScript 跳转函数代码 * @return */ public String getJavaScriptJumpCode() { if(StringUtil.isEmpty(pageUrl)) { return ""; } // 检查是否有参数符号, 没有就加上一个? if(pageUrl.indexOf("?") == -1) { pageUrl = pageUrl + '?'; } return "<script>" + "// 页面跳转函数\n" + "// 参数: 包含页码的表单元素,例如输入框,下拉框等\n" + "function jumpPage(input) {\n" + " // 页码相同就不做跳转\n" + " if(input.value == " + currentPage + ") {" + " return;\n" + " }" + " var newUrl = '" + pageUrl + "&page=' + input.value;\n" + " document.location = newUrl;\n" + " }\n" + " </script>"; } /** * 输出页面跳转的选择框和输入框. 示例输出: * <pre> 转到 <!-- 输出 HTML SELECT 元素, 并选中当前页面编码 --> <select onchange='jumpPage(this);'> <c:forEach var="i" begin="1" end="${totalPage}"> <option value="${i}" <c:if test="${currentPage == i}"> selected </c:if> >第${i}页</option> </c:forEach> </select> 输入页码:<input type="text" value="${currentPage}" id="jumpPageBox" size="3"> <input type="button" value="跳转" onclick="jumpPage(document.getElementById('jumpPageBox'))"> </pre> * @return */ public String getPageFormJumpHtml() { String s = "转到\n" + "\t <!-- 输出 HTML SELECT 元素, 并选中当前页面编码 -->\n" + " <select onchange='jumpPage(this);'>\n" + " \n"; for(int i = 1; i <= getTotalPage(); i++ ) { s += "<option value=" + i + "\n"; if(currentPage == i) { s+= " selected "; } s += "\t>第" + i + "页</option>\n"; } s+= " </select>\n" + " 输入页码:<input type=\"text\" value=\"" + currentPage + "\" id=\"jumpPageBox\" size=\"3\"> \n" + " <input type=\"button\" value=\"跳转\" onclick=\"jumpPage(document.getElementById('jumpPageBox'))\"> "; return s; } /** * 进行分页计算. */ private void calculate() { if (getPageCount() == 0) { setPageCount(1); } totalPage = (int) Math.ceil(1.0 * getRecordCount() / getPageCount()); // 总页面数 if (totalPage == 0) totalPage = 1; // Check current page range, 2006-08-03 if(currentPage > totalPage) { currentPage = totalPage; } // System.out.println("currentPage=" + currentPage); // System.out.println("maxPage=" + maxPage); // // Fixed logic error at 2004-09-25 hasNextPage = currentPage < totalPage; hasPrevPage = currentPage > 1; return; } /** * @return Returns the 最大页面数. */ public int getTotalPage() { calculate(); return totalPage; } /** * @param currentPage * The 最大页面数 to set. */ @SuppressWarnings("unused") private void setTotalPage(int maxPage) { this.totalPage = maxPage; } /** * 是否有上一页数据 */ public boolean hasPrevPage() { calculate(); return hasPrevPage; } /** * 是否有下一页数据 */ public boolean hasNextPage() { calculate(); return hasNextPage; } // Test public static void main(String[] args) { PageBean pc = new PageBean(); pc.setCurrentPage(2); pc.setPageCount(4); pc.setRecordCount(5); pc.setPageUrl("product/list.do"); System.out.println("当前页 " + pc.getCurrentPage()); System.out.println("有上一页 " + pc.hasPrevPage()); System.out.println("有下一页 " + pc.hasNextPage()); System.out.println("总页面数 " + pc.getTotalPage()); System.out.println("分页 HTML 代码 " + pc.getPageJumpLinkHtml()); } /** * @return Returns the 当前页码数. */ public int getCurrentPage() { return currentPage; } /** * 设置当前页码, 从 1 开始. * @param currentPage * The 当前页码数 to set. */ public void setCurrentPage(int currentPage) { if (currentPage <= 0) { currentPage = 1; } this.currentPage = currentPage; } /** * @return Returns the recordCount. */ public int getRecordCount() { return recordCount; } /** * @param recordCount * The recordCount to set. */ public void setRecordCount(int property1) { this.recordCount = property1; } /** * @return Returns the 每页显示记录数. */ public int getPageCount() { return pageCount; } /** * @param pageCount * The 每页显示记录数 to set. */ public void setPageCount(int pageCount) { this.pageCount = pageCount; } public String getPageUrl() { return pageUrl; } public void setPageUrl(String value) { pageUrl = value; } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值