我一直在努力让这个网格填满。
这会返回我的2行,但是单元格是空的吗?
我更愿意使用jqGrid完成所有操作(因此使用单独的Ajax调用,但我无法使其工作)。任何帮助将不胜感激。
ASMX返回的JSON:
{"__type":"myproject.jq_grid","page":1,"total":1,"records":2,"rows":[{"id":1,"cell":["1","donald","duck"]},{"id":2,"cell":["2","daffy","duck"]}]}
客户端:脚本顺序相同,
jquery.jqGrid.min.js
$(document).ready(function () {
$("#list1").jqGrid({
datatype: function (postdata) {
var params = new Object();
params.page_index = postdata.page;
params.page_size = postdata.rows;
params.sort_index = postdata.sidx;
params.sort_direction = postdata.sord;
$.ajax({
url: "../service/contact_service.asmx/contact_jq_grid",
type: "POST",
data: JSON.stringify(params),
contentType: "application/json; charset=utf-8",
error: function (data, textStatus) {
alert("Error loading json");
},
success: function (data, st) {
if (st == "success") {
var results = (typeof data.d) == 'string' ? eval('(' + data.d + ')') : data.d;
$("#list1")[0].addJSONData(eval("(" + JSON.stringify(results) + ")"));
}
}
});
},
colNames: ["contact_id", "first_name", "last_name"],
colModel: [{ name: "contact_id", index: "contact_id", width: 100 },
{ name: "first_name", index: "first_name", width: 150 },
{ name: "last_name", index: "last_name", width: 150}],
pager: "#pager1",
rowNum: 10,
rowList: [10, 20, 30],
sortname: "contact_id",
sortorder: "asc",
viewRecords: true,
jsonReader: {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
id: "id",
cell: "cell"
}
});
});
服务器端:
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function contact_jq_grid( _
ByVal page_index As Integer, ByVal page_size As Integer, _
ByVal sort_index As String, ByVal sort_direction As String)
As jq_grid
Dim jq_grid As New jq_grid
Dim contacts As List(Of contact) = _
contact_functions.get_contacts_aw(1, page_index - 1, page_size, sort_index, sort_direction)
For i As Integer = 0 To contacts.Count - 1
Dim row As New jq_grid.row()
row.id = contacts(i).contact_id
row.cell.Add(contacts(i).contact_id)
row.cell.Add(contacts(i).first_name)
row.cell.Add(contacts(i).last_name)
jq_grid.rows.Add(row)
Next
jq_grid.page = page_index
jq_grid.records = contacts.Count
jq_grid.total = Math.Ceiling(contacts.Count / page_size)
Return jq_grid
End Function
Public Class jq_grid
Private _page As Integer
Private _total As Integer
Private _records As Integer
Private _rows As List(Of row)
Public Property page() As Integer
Get
Return _page
End Get
Set(ByVal value As Integer)
_page = value
End Set
End Property
Public Property total() As Integer
Get
Return _total
End Get
Set(ByVal value As Integer)
_total = value
End Set
End Property
Public Property records() As Integer
Get
Return _records
End Get
Set(ByVal value As Integer)
_records = value
End Set
End Property
Public Property rows() As List(Of row)
Get
Return _rows
End Get
Set(ByVal value As List(Of row))
_rows = value
End Set
End Property
Public Sub New()
rows = New List(Of row)
End Sub
Public Class row
Private _id As Integer
Private _cell As List(Of String)
Public Property id() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
Public Property cell() As List(Of String)
Get
Return _cell
End Get
Set(ByVal value As List(Of String))
_cell = value
End Set
End Property
Public Sub New()
cell = New List(Of String)
End Sub
End Class
End Class
答案 0 :(得分:1)
您的主要错误是您在repeatitems: false
中使用jsonReader
,但是您构建了repeatitems: true
的数据。
另一个错误:如果从服务器返回的JSON数据确实是您发布的格式(我不确定是这样),那么您不应该在data.d
中使用success
处理。我想,你作为JSON而不是服务器响应,但results
变量的值包括data.d
。
您使用非常旧的代码模板datatype
作为函数。您可以轻松地重写代码(例如,请参阅this old answer)。此外,如果您确实需要使用重命名的参数,例如page_index
而不是page
,则可以更好地使用prmNames
来执行此操作。