<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String basepath = request.getContextPath();
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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"
href="<%=basepath%>/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="<%=basepath%>/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css"
href="<%=basepath%>/easyui/demo.css">
<script type="text/javascript" src="<%=basepath%>/easyui/jquery.min.js"></script>
<script type="text/javascript"
src="<%=basepath%>/easyui/jquery.easyui.min.js"></script>
</head>
<body>
<br>
<h2>Client Side Pagination in DataGrid</h2>
<p>This sample shows how to implement client side pagination in
DataGrid.</p>
<div style="margin: 20px 0;"></div>
<table id="dg" title="Client Side Pagination"
style=" 700px; height: 300px"
data-options="
rownumbers:true,
singleSelect:true,
autoRowHeight:false,
pagination:true,
pageSize:10">
<thead>
<tr>
<th field="id" width="30%">id</th>
<th field="userName" width="30%">userName</th>
<th field="age" width="30%">age</th>
</tr>
</thead>
</table>
<script>
(function($) {
function pagerFilter(data) {
if ($.isArray(data)) { // is array
data = {
total : data.length,
rows : data
}
}
var dg = $(this);
var state = dg.data('datagrid');
var opts = dg.datagrid('options');
if (!state.allRows) {
state.allRows = (data.rows);
}
var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
var end = start + parseInt(opts.pageSize);
data.rows = $.extend(true, [], state.allRows.slice(start, end));
return data;
}
var loadDataMethod = $.fn.datagrid.methods.loadData;
$.extend($.fn.datagrid.methods,
{
clientPaging : function(jq) {
return jq.each(function() {
var dg = $(this);
var state = dg.data('datagrid');
var opts = state.options;
opts.loadFilter = pagerFilter;
var onBeforeLoad = opts.onBeforeLoad;
opts.onBeforeLoad = function(param) {
state.allRows = null;
return onBeforeLoad.call(this, param);
}
dg.datagrid('getPager').pagination({
onSelectPage : function(pageNum, pageSize) {
opts.pageNumber = pageNum;
opts.pageSize = pageSize;
$(this).pagination('refresh', {
pageNumber : pageNum,
pageSize : pageSize
});
dg.datagrid('loadData', state.allRows);
}
});
$(this).datagrid('loadData', state.data);
if (opts.url) {
$(this).datagrid('reload');
}
});
},
loadData : function(jq, data) {
jq.each(function() {
$(this).data('datagrid').allRows = null;
});
return loadDataMethod.call($.fn.datagrid.methods,
jq, data);
},
getAllRows : function(jq) {
return jq.data('datagrid').allRows;
}
})
})(jQuery);
function getData() {
var rows = "";
/* for (var i = 1; i <= 800; i++) {
var amount = Math.floor(Math.random() * 1000);
var price = Math.floor(Math.random() * 1000);
rows.push({
inv : 'Inv No ' + i,
date : $.fn.datebox.defaults.formatter(new Date()),
name : 'Name ' + i,
amount : amount,
price : price,
cost : amount * price,
note : 'Note ' + i
});
} */
$.ajax({
method : 'POST',
url : '/egoTest/user/getAjaxUser2.do',
async : false,
dataType : 'json',
success : function(data) {
rows = data;
},
error : function() {
alert('error');
}
});
return rows;
}
$(function() {
$('#dg').datagrid({
data : getData()
}).datagrid('clientPaging');
});
</script>
<br><br><br><br><br>
a index jsp page ! ${list}
<br>
<table border="1" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th width="100"><input type="checkBox" name="checkBox" /></th>
<th field="userId" width="100">用户ID</th>
<th field="userName" width="100">名称</th>
<th field="bigDepartId" width="100">年龄</th>
<th field="operation" width="100">操作</th>
</tr>
</thead>
<c:forEach items="${uList }" var="u">
<tr class="${status.index%2==1?'even':'odd' }">
<td><input type="checkBox" name="checkBox" /></td>
<td>${u.id}</td>
<td>${u.userName }</td>
<td>${u.age }</td>
<td><a href="/springMVC11/user/getUser.do?id=${u.id }">修改</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
@RequestMapping("/getAjaxUser2")
public void getAjaxUser2(HttpServletRequest request,
HttpServletResponse response, ModelMap modelMap) {
try {
response.setContentType( "text/html;charset=UTF-8");
List<User> uList = userService.getAllUser();
Map<String, Object> map = new HashMap<String, Object>();
// map.put( "total",total);
map.put("allRows", 57);
map.put("rows", uList);
JSONObject json = JSONObject.fromObject(map);
// String str = "aabbcc";
PrintWriter pw = response.getWriter();
pw.write(json.toString());
pw.flush();
pw.close();
} catch (Exception e) {
e.printStackTrace();
}
}