62,046
社区成员
发帖
与我相关
我的任务
分享
public JsonResult getJson(int page, int rows)
{
var users = InitialData();
return Json(new { total = users.Count, rows = users.Skip((page - 1) * rows).Take(rows) }, JsonRequestBehavior.AllowGet);
}
public List<UserModel> InitialData()
{
List<UserModel> list = new List<UserModel>();
for (int i = 1; i <= 300; i++)
{
UserModel user = new UserModel() { ID = i, UName = "名字" + i };
list.Add(user);
}
return list;
}
<div>
<table id="dg"></table>
<script type="text/javascript">
$('#dg').datagrid({
url: '/Default/getJson',
fitColumns: true,
rownumbers: true,
pagination: true, //分页属性设置
singleSelect: true,
pageNumber: 1,
pageSize: 10,
pageList: [10, 20, 30, 40],
columns: [[
{ field: 'UId', title: 'ID', width: 50, align: 'center' },
{ field: 'UName', title: '用户名称', width: 200, align: 'center' }
]],
});
</script>
</div>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link href="../Scripts/jquery-easyui-1.4/themes/default/easyui.css" rel="stylesheet" />
<link href="../Scripts/jquery-easyui-1.4/themes/icon.css" rel="stylesheet" />
</head>
<body>
<table id="tt" class="easyui-datagrid" style="width:700px;height:350px"
data-options="url:'datagrid_getdata.ashx',
title:'Load Data',
iconCls:'icon-save', pageList:[10, 20, 30],
rownumbers:'true', pagination:true">
<thead>
<tr>
<th field="productid" width="120">Product ID</th>
</tr>
</thead>
</table>
</body>
</html>
<script src="../Scripts/jquery-easyui-1.4/jquery.min.js"></script>
<script src="../Scripts/jquery-easyui-1.4/jquery.easyui.min.js"></script>
datagrid_getdata.ashx:
<%@ WebHandler Language="C#" Class="datagrid_getdata" %>
using System;
using System.Web;
public class datagrid_getdata : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int intPageSize = int.Parse(context.Request["rows"] == null ? "10" : context.Request["rows"]);
int intCurrentPage = int.Parse(context.Request["page"] == null ? "0" : context.Request["page"]);
int totalCount = 100;
string json = "";
int start = intPageSize * (intCurrentPage - 1);
for (int i = 0; i < intPageSize; i++)
{
json += string.Format("{{\"productid\":\"{0}\"}},", i + start + 1);
if (i + start + 1 == totalCount)
{
break;
}
}
json = "[" + json.TrimEnd(',') + "]";
string result = "{\"total\":" + totalCount + ", \"rows\":" + json + "}";
context.Response.Write(result);
}
public bool IsReusable
{
get
{
return false;
}
}
}