jqGrid实现分页

      今天看到javaeye上有人使用了jqGrid实现了数据的分页,参考它的代码,实现了这个功能,搬出来晒晒,作为自己以后学习的资料!

(1)页面代码:

Html代码   收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>Insert title here</title>  
  6. <link rel="stylesheet" type="text/css" media="screen"  
  7.     href="js/themes/basic/grid.css" />  
  8. <script type="text/javascript" src="js/jquery.js"></script>  
  9. <script type="text/javascript" src="js/jquery.jqGrid.js"></script>  
  10. <script type="text/javascript">  
  11. jQuery(document).ready(function(){  
  12.     jQuery("#myTab").jqGrid({  
  13.         datatype: "json", //将这里改为使用JSON数据  
  14.         url:'DataServlet', //这是Action的请求地址  
  15.         mtype: 'POST',  
  16.         height: 250,  
  17.         width: 400,  
  18.         colNames:['编号','姓名', '电话'],  
  19.         colModel:[  
  20.             {name:'id',index:'id', width:60, sorttype:"int"},  
  21.             {name:'name',index:'name', width:90},  
  22.             {name:'phone',index:'phone', width:100}       
  23.         ],  
  24.         pager: 'pager', //分页工具栏  
  25.         imgpath: 'js/themes/basic/images', //图片存放路径  
  26.         rowNum:10, //每页显示记录数  
  27.         viewrecords: true, //是否显示行数  
  28.         rowList:[10,20,30], //可调整每页显示的记录数  
  29.         multiselect: false, //是否支持多选  
  30.         caption: "信息显示"  
  31.     });  
  32. });  
  33. </script>  
  34. </head>  
  35. <body>  
  36. <table id="myTab" class="scroll" cellpadding="0" cellspacing="0"></table>  
  37. <div id="pager" class="scroll"></div>  
  38. </body>  
  39. </html>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" media="screen"
	href="js/themes/basic/grid.css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.jqGrid.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
	jQuery("#myTab").jqGrid({
		datatype: "json", //将这里改为使用JSON数据
		url:'DataServlet', //这是Action的请求地址
		mtype: 'POST',
		height: 250,
		width: 400,
	   	colNames:['编号','姓名', '电话'],
	   	colModel:[
	   		{name:'id',index:'id', width:60, sorttype:"int"},
	   		{name:'name',index:'name', width:90},
	   		{name:'phone',index:'phone', width:100}		
	   	],
	   	pager: 'pager', //分页工具栏
	   	imgpath: 'js/themes/basic/images', //图片存放路径
	   	rowNum:10, //每页显示记录数
	   	viewrecords: true, //是否显示行数
	   	rowList:[10,20,30], //可调整每页显示的记录数
	   	multiselect: false, //是否支持多选
	   	caption: "信息显示"
	});
});
</script>
</head>
<body>
<table id="myTab" class="scroll" cellpadding="0" cellspacing="0"></table>
<div id="pager" class="scroll"></div>
</body>
</html>

 (2)后台的servlet,里面的数据是模拟的

Java代码   收藏代码
  1. /** 
  2.  * Servlet implementation class DataServlet 
  3.  */  
  4. public class DataServlet extends HttpServlet {  
  5.     private static final long serialVersionUID = 1L;  
  6.   
  7.     /** 
  8.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 
  9.      *      response) 
  10.      */  
  11.     protected void doGet(HttpServletRequest request,  
  12.             HttpServletResponse response) throws ServletException, IOException {  
  13.         // TODO Auto-generated method stub  
  14.     }  
  15.   
  16.     /** 
  17.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 
  18.      *      response) 
  19.      */  
  20.     protected void doPost(HttpServletRequest request,  
  21.             HttpServletResponse response) throws ServletException, IOException {  
  22.         String page = request.getParameter("page"); // 取得当前页数,注意这是jqgrid自身的参数  
  23.         String rows = request.getParameter("rows"); // 取得每页显示行数,,注意这是jqgrid自身的参数  
  24.         int totalRecord = 80// 总记录数(应根据数据库取得,在此只是模拟)  
  25.         int totalPage = totalRecord % Integer.parseInt(rows) == 0 ? totalRecord  
  26.                 / Integer.parseInt(rows) : totalRecord / Integer.parseInt(rows)  
  27.                 + 1// 计算总页数  
  28.         try {  
  29.             int index = (Integer.parseInt(page) - 1) * Integer.parseInt(rows); // 开始记录数  
  30.             int pageSize = Integer.parseInt(rows);  
  31.             // 以下模拟构造JSON数据对象  
  32.             String json = "{total: " + totalPage + ", page: " + page  
  33.                     + ", records: " + totalRecord + ", rows: [";  
  34.             for (int i = index; i < pageSize + index && i < totalRecord; i++) {  
  35.                 json += "{cell:['ID " + i + "','NAME " + i + "','PHONE " + i  
  36.                         + "']}";  
  37.                 if (i != pageSize + index - 1 && i != totalRecord - 1) {  
  38.                     json += ",";  
  39.                 }  
  40.             }  
  41.             json += "]}";  
  42.             response.getWriter().write(json); // 将JSON数据返回页面  
  43.         } catch (Exception ex) {  
  44.         }  
  45.     }  
  46. }  
/**
 * Servlet implementation class DataServlet
 */
public class DataServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String page = request.getParameter("page"); // 取得当前页数,注意这是jqgrid自身的参数
		String rows = request.getParameter("rows"); // 取得每页显示行数,,注意这是jqgrid自身的参数
		int totalRecord = 80; // 总记录数(应根据数据库取得,在此只是模拟)
		int totalPage = totalRecord % Integer.parseInt(rows) == 0 ? totalRecord
				/ Integer.parseInt(rows) : totalRecord / Integer.parseInt(rows)
				+ 1; // 计算总页数
		try {
			int index = (Integer.parseInt(page) - 1) * Integer.parseInt(rows); // 开始记录数
			int pageSize = Integer.parseInt(rows);
			// 以下模拟构造JSON数据对象
			String json = "{total: " + totalPage + ", page: " + page
					+ ", records: " + totalRecord + ", rows: [";
			for (int i = index; i < pageSize + index && i < totalRecord; i++) {
				json += "{cell:['ID " + i + "','NAME " + i + "','PHONE " + i
						+ "']}";
				if (i != pageSize + index - 1 && i != totalRecord - 1) {
					json += ",";
				}
			}
			json += "]}";
			response.getWriter().write(json); // 将JSON数据返回页面
		} catch (Exception ex) {
		}
	}
}

 

目录结构:



 展现的效果:



 http://blog.youkuaiyun.com/polaris1119/archive/2010/01/04/5130974.aspx

http://d.download.youkuaiyun.com/down/1142295/ctfzh

http://hi.baidu.com/fangle_life/blog/item/1076b6fa85a9ba1c6d22eb1e.html

http://blog.youkuaiyun.com/polaris1119/archive/2010/01/12/5183327.aspx

转载于:https://www.cnblogs.com/sailormoon/archive/2012/12/25/2831867.html

<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> <link type="text/css" rel="stylesheet" href="css/jquery-ui-1.9.2.custom.min.css"> <link type="text/css" rel="stylesheet" href="css/ui.jqgrid.css"> <link rel="stylesheet" type="text/css" media="screen" href="css/ui.multiselect.css"> <script type="text/javascript" src="js/jquery-1.11.0.min.js" ></script> <script type="text/javascript" src="js/jquery-ui-1.9.2.custom.min.js"></script> <script type="text/javascript" src="js/jquery.layout-1.2.0.js"></script> <script type="text/javascript" src="js/i18n/grid.locale-cn.js"></script> <script type="text/javascript" src="js/jquery.jqGrid.min.js"></script> <script type="text/javascript" src="js/jquery.tablednd.js"></script> <script type="text/javascript" src="js/jquery.contextmenu.js"></script> <script type="text/javascript" src="js/ui.multiselect.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#list").jqGrid({ url:"happyEvent/jsonlist", datatype: "json", height: "auto", width:"auto", colNames:['id','时间', '用户名', '标题','内容'], colModel:[ {name:'id',index:'id', width:60, sorttype:"int"}, {name:'time',index:'time', width:90, sorttype:"date"}, {name:'name',index:'name', width:100}, {name:'title',index:'title', width:300, align:"left",sorttype:"float"}, {name:'content',index:'content', width:80, align:"left",sorttype:"float"} ], rowNum:2, rowList:[2,4,6], sortname: 'id', pager:"#pager", multiselect: true, caption: "喜事列表" }); $("#list").jqGrid('navGrid','#pager',{edit:false,add:false,del:false}); }); </script> </head> <body> <table id="list"></table> <div id="pager"></div> </body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值