问题描述
jqGrid的采用下列JSON格式:
jqGrid takes the following JSON format:
{
"total": "5",
"page": "2",
"records": "55",
"rows" : [
{"id" :"21", "cell" :["cell11", "cell12", "cell13"]},
{"id" :"22", "cell" :["cell21", "cell22", "cell23"]},
...
{"id" :"30", "cell" :["cell31", "cell32", "cell33"]},
]
}
我想做一个方法,可重复使用尽可能通过AJAX来传递数据回jqGrid的。
I'm trying to make a method as reusable as possible to pass data back through AJAX to the jqGrid.
var result = new
{
total = (int) Math.Ceiling((double) totalCount/PageSize),
page = PageIndex,
records = totalCount,
rows = data.Select((d, id) => new {id, cell = d.SerializeGridParameters()}).ToArray()
};
正如你所看到的,目前我设法添加索引不用额外的努力,但我有与现场数据的麻烦。
As you can see, currently I managed to add the index without extra effort, but I'm having trouble with the field data.
到目前为止,我设法解决它通过一个接口:
So far, I managed to deal with it by using an interface:
public interface IGridParameterListable
{
List<string> SerializeGridParameters();
}
有关我的数据(这是一个的IEnumerable&LT; T&GT;其中T:IGridParameterListable
)。
事情是我宁愿有一个只是一味地将对象转换属性值的通用方法名单,其中,串&GT;
..
For my data (which is an IEnumerable<T> where T : IGridParameterListable
).
Thing is I'd much rather have a generic method that just blindly converts objects property values to a List<string>
..
听起来不是很可爱,我知道,我是开放给其他的想法。
我想避免尽可能重复的数据结构上的客户机和服务器侧二者的网格。
Doesn't sound too cute, I know, I'm open to other ideas.
I want to avoid as much as possible repeating the data structure for a grid on both the client and server sides.
推荐答案
这可能是一个更好的选择。
This might be a better option.
public string SerializeQuery<T>(IQueryable<T> query, Func<T, List<string>> select)
{
// stuff ...
var result = new
{
total = (int)Math.Ceiling((double)totalCount / PageSize),
page = PageIndex,
records = totalCount,
rows = data.Select((d, id) => new { id, cell = select(d) }).ToArray()
};
// stuff ...
}
予消除的接口的需要,并移动转换到查询针对每个特定网格的实现。在这个例子中:
I eliminate the need of an interface, and move the conversion to the implementation of the query for each particular grid. In this example:
[WebMethod]
[ScriptMethod]
public string GetData(GridSettings grid)
{
var query = new FakeComputersRepository().Computers();
var response = grid.SerializeQuery(query, d => new List<string>
{
d.ID.ToString(),
d.IsOnline.ToString(),
d.Name,
d.IP,
d.User
});
return response;
}
防抖,我想。任何其他的想法,以进一步扩大在该?
A tad better, I think.Any other ideas, to further expand on this?
这篇关于jqGrid的LINQ和匿名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!