我有一个类,我在我的网络服务中填充数据。它被称为 JqGridData,是在回复 Oleg 的另一个线程时受到启发而制作的。它看起来是这样的:
public class JqGridData
{
/// <summary>
/// The number of pages which should be displayed in the paging controls at the bottom of the grid.
/// </summary>
public int Total { get; set; }
/// <summary>
/// The current page number which should be highlighted in the paging controls at the bottom of the grid.
/// </summary>
public int Page { get; set; }
/// <summary>
/// The total number of records in the entire data set, not just the portion returned in Rows.
/// </summary>
public int Records { get; set; }
/// <summary>
/// The data that will actually be displayed in the grid.
/// </summary>
public IEnumerable Rows { get; set; }
/// <summary>
/// Arbitrary data to be returned to the grid along with the row data. Leave null if not using. Must be serializable to JSON!
/// </summary>
public object UserData { get; set; }
}
在我的网络服务中,我通过以下方式返回数据:
List<Order> orders = OrderBLL.LoadByCustomerIdPaging(customer.CustomerId, page, rows, sidx, sord, out totalCount);
object o = null;
JqGridData result = new JqGridData()
{
Page = page,
Total = (int)Math.Ceiling((decimal)totalCount / rows),
Records = totalCount,
Rows = (from order in orders
select new
{
Id = order.OrderId,
OrderId = order.OrderId//,
//OrderStatusName = order.OrderStatusName,
//OrderDate = order.ActionDate.Value.Date.ToShortDateString(),
//OrderTypeName = order.OrderTypeName,
//OrderPhoneNo = order.PhoneNo,
//OrderProductName = order.Product.ProductName,
//OrderTempNumber = order.TempNumber,
//OrderLines = order.FormattedOrderlines
}).ToArray(),
UserData = o
};
//return result;
//return result.ConvertToJson();
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
return jsonSerializer.Serialize(result);
我的 jqGrid 定义如下:
jQuery("#<%=orderList.ClientID %>").jqGrid({
datatype: "json",
url: "/Webservices/OrderService.svc/LoadOrderViewPaging",
contentType: 'application/json; charset=utf-8',
mtype: 'POST',
jsonReader: {
root: "d.Rows",
page: "d.Page",
total: "d.Total",
records: "d.Records",
repeatitems: false
},
height: 'auto',
rowNum: 30,
rowList: [10, 20, 30],
shrinkToFit: true,
colNames: [
'<%=GetGlobalResourceObject("CustomerOrders","OrderId") %>',
// '<%=GetGlobalResourceObject("CustomerOrders","OrderStatus") %>',
// '<%=GetGlobalResourceObject("CustomerOrders","OrderDate") %>',
// '<%=GetGlobalResourceObject("CustomerOrders","OrderType") %>',
// '<%=GetGlobalResourceObject("CustomerOrders","OrderPhoneNo") %>',
// '<%=GetGlobalResourceObject("CustomerOrders","OrderTempNumber") %>',
// '<%=GetGlobalResourceObject("CustomerOrders","OrderProduct") %>',
// '<%=GetGlobalResourceObject("CustomerOrders","Products") %>'
],
colModel: [
{ name: 'OrderId', index: 'OrderId', width: 60, sorttype: "int" },
// { name: 'OrderStatusName', index: 'OrderStatusName', width: 80 },
// { name: 'ActionDate', index: 'ActionDate', formatter: 'date', formatoptions: { srcformat: 'ISO8601Long', newformat: 'Y-m-d H:i:s' }, width: 80 },
// { name: 'OrderTypeName', index: 'OrderTypeName', width: 80 },
// { name: 'PhoneNo', index: 'PhoneNo', width: 80 },
// { name: 'TempNumber', index: 'TempNumber', width: 80 },
// { name: 'Product.ProductName', index: 'OrderProductName', width: 125 },
// { name: 'FormattedOrderlines', index: 'FormattedOrderlines', width: 125 },
],
pager: "#<%=orderListPager.ClientID %>",
viewrecords: true,
sortname: 'OrderId',
sortorder: 'desc',
viewrecords: true,
sortname: 'Id',
grouping: true,
groupingView: {
groupField: ['OrderId'],
groupOrder: ['desc']
// groupCollapse: true,
},
loadError: function (jqXHR, textStatus, errorThrown) {
alert('HTTP status code: ' + jqXHR.status + '\n' +
'textStatus: ' + textStatus + '\n' +
'errorThrown: ' + errorThrown);
alert('HTTP message body (jqXHR.responseText): ' + '\n' + jqXHR.responseText);
},
beforeProcessing: function (data, status, xhr) {
debugger;
},
caption: '<%=GetGlobalResourceObject("CustomerOrders","Customer orders") %>',
onSelectRow: function (id) {
if (id) {
var ret = jQuery("#<%=orderList.ClientID %>").jqGrid('getRowData', id);
ShowOrderStatus(ret.OrderId);
}
}
loadError 不会引发错误。如果我捕捉到 beforeProcessing 事件,我可以看到我收到了有效的 json 数据,我在下面插入了这些数据:
{"Total":73,"Page":1,"Records":2179,"Rows":[{"Id":30903,"OrderId":30903},{"Id":30852,"OrderId":30852},{"Id":30692,"OrderId":30692},{"Id":30687,"OrderId":30687},{"Id":30686,"OrderId":30686},{"Id":30684,"OrderId":30684},{"Id":30683,"OrderId":30683},{"Id":30682,"OrderId":30682},{"Id":30681,"OrderId":30681},{"Id":30680,"OrderId":30680},{"Id":30678,"OrderId":30678},{"Id":30677,"OrderId":30677},{"Id":30676,"OrderId":30676},{"Id":30675,"OrderId":30675},{"Id":30674,"OrderId":30674},{"Id":30673,"OrderId":30673},{"Id":30668,"OrderId":30668},{"Id":30667,"OrderId":30667},{"Id":30665,"OrderId":30665},{"Id":30664,"OrderId":30664},{"Id":30663,"OrderId":30663},{"Id":30647,"OrderId":30647},{"Id":30635,"OrderId":30635},{"Id":30411,"OrderId":30411},{"Id":30331,"OrderId":30331},{"Id":30330,"OrderId":30330},{"Id":30287,"OrderId":30287},{"Id":30241,"OrderId":30241},{"Id":30240,"OrderId":30240},{"Id":30239,"OrderId":30239}],"UserData":null}
但是 jqGrid 中没有显示任何行。如果有人有一些想法,他们将不胜感激。
PS。如果我将 JqGridData 的 Rows 字段更改为 JqGridRecord 之类的已定义类并返回这些所有内容的列表。但我不想被迫为我想要返回的所有特定返回数据组合定义类。
更新
只要我返回一个对象,它就可以正常工作。但我想阻止这种情况。
JqGridData result = new JqGridData()
{
page = page,
total = (int)Math.Ceiling((decimal)totalCount / rows),
records = totalCount,
rows = (from order in orders
select new jqGridRecord
{
OrderId = order.OrderId//,
//OrderId = order.OrderId//,
//OrderStatusName = order.OrderStatusName,
//OrderDate = order.ActionDate.Value.Date.ToShortDateString(),
//OrderTypeName = order.OrderTypeName,
//OrderPhoneNo = order.PhoneNo,
//OrderProductName = order.Product.ProductName,
//OrderTempNumber = order.TempNumber,
//OrderLines = order.FormattedOrderlines
}).ToList()//,
//UserData = o
};
return result;