Ext分页及远程排序

xclin163 2009-12-29 07:46:03
小弟刚开始接触Ext,试着写了个分页以及远程排序,写得不好,希望大家不要笑话~~~
这量asp.net版本,jsp和php的兄弟自己改一改~~~

脚本

/// <reference path="../ext-2.0.2/vswd-ext_2.0.2.js" />
Ext.onReady(function() {
var data = new Ext.data.JsonStore({
url: "GetData.aspx",
//启用远程排序
remoteSort: true,
//记录总条数
totalProperty: "totalCount",
//读取数据的根
root: "users",
fields: ["id", "name", "pwd"]
});
//加载数据,start:从第几条开始,limit:返回几条数据
data.load({ params: { start: 0, limit: 10} });
var cmd = new Ext.grid.ColumnModel({
align: "center",
columns: [{
header: "用户编号",
sortable: true,
dataIndex: "id"
}, {
header: "用户名称",
sortable: true,
dataIndex: "name"
}, {
header: "用户密码",
sortable: true,
dataIndex: "pwd"
}]
});
new Ext.QuickTip();
var grid = new Ext.grid.GridPanel({
width: 400,
height: 400,
title: "用户展示",
renderTo: Ext.get("list"),
store: data,
cm: cmd,
frame: true,
//在bbar上定义一个分页工具
bbar: new Ext.PagingToolbar({
//每页条数
pageSize: 10,
//是否显示数据信息,默认为false
displayInfo: true,
displayMsg: "第{0}条--第{1}条,总共{2}条",
emptyMsg: "没有数据",
//这里的store与GridPanel的store是同一个
store: data
})
});
});


后台代码:

protected void Page_Load(object sender, EventArgs e)
{
//总条数
int count = 0;
//开始条数
int start = Convert.ToInt32(Request["start"]);
//最大数据
int limit = Convert.ToInt32(Request["limit"]);
//因为这里是用Linq查询的IList里的数据,用数据库的话就不用加了
limit = limit + start;
//定义默认的排序列
string orderBy = "id";
//定义默认的排序方式
string dir = "ASC";
//判断页面是否进行了排序
if (Request["sort"] != null && Request["dir"] != null)
{
orderBy = Request["sort"] as string;
dir = Request["dir"] as string;
}
//获取处理后的数据
IList<Users> list = GetUsers(start, limit, orderBy, dir, out count);
//拼接json字符串
string json = string.Format("'totalCount':'{0}','users':[", count);
foreach (var item in list)
{
json += "{" + string.Format("'id':'{0}','name':'{1}','pwd':'{2}'", item.Id, item.Name, item.Pwd) + "},";
}
json = "{" + json.Remove(json.Length - 1, 1) + "]}";
Response.Write(json);
}

private IList<Users> GetUsers(int start, int limit, string orderBy, string dir, out int count)
{

IList<Users> data = BuildUsers();
count = data.Count;
//这里用的Linq的查找与排序
IEnumerable<Users> results = null;
switch (dir)
{
case "ASC":
switch (orderBy)
{
case "id": results = from user in data.Where((u, index) => index >= start && index < limit) orderby user.Id select user;
break;
case "name": results = from user in data.Where((u, index) => index >= start && index < limit) orderby user.Name select user;
break;
case "pwd": results = from user in data.Where((u, index) => index >= start && index < limit) orderby user.Pwd select user;
break;
default: results = from user in data.Where((u, index) => index >= start && index < limit) orderby user.Id select user;
break;
}
break;
case "DESC": switch (orderBy)
{
case "id": results = from user in data.Where((u, index) => index >= start && index < limit) orderby user.Id descending select user;
break;
case "name": results = from user in data.Where((u, index) => index >= start && index < limit) orderby user.Name descending select user;
break;
case "pwd": results = from user in data.Where((u, index) => index >= start && index < limit) orderby user.Pwd descending select user;
break;
default: results = from user in data.Where((u, index) => index >= start && index < limit) orderby user.Id descending select user;
break;
}
break;
default:
results = from user in data.Where((u, index) => index >= start && index < limit) orderby user.Id select user;
break;
}
return results.ToList<Users>();
}
private IList<Users> BuildUsers()
{
IList<Users> list = new List<Users>();
for (int i = 0; i < 40; i++)
{
list.Add(new Users(i, "用户" + i, "admin" + i));
}
return list;
}


其中Users中就id,name,pwd和Id,Name,Pwd字段和属性

本来想着写个案例就不用数据库了,直接用Linq查询模拟下,写了才发现Linq结果写起来很麻烦,直接郁闷了~~~~,希望大家不要介意,也希望大家给出意见,大家一起学习进步
...全文
228 3 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
happy002 2009-12-30
  • 打赏
  • 举报
回复
up一下~
s000rd 2009-12-30
  • 打赏
  • 举报
回复
json记得转义,如单引号,否则会出错
woshifou 2009-12-29
  • 打赏
  • 举报
回复
不错。