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"]},
]
}
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();
}
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.
I eliminate the need of an interface, and move the conversion to the implementation of the query for each particular grid. In this example:
A tad better, I think.
Any other ideas, to further expand on this?