Я изо всех сил пытался заставить эту сетку заполняться.
Это возвращает мои 2 строки, но ячейки пусты?
Я бы предпочел сделать все это с помощью jqGrid (поэтому сотдельный вызов Ajax, но я не мог заставить его работать).Любая помощь будет высоко ценится.
JSON ASMX возвращает:
{"__type":"myproject.jq_grid","page":1,"total":1,"records":2,"rows":[{"id":1,"cell":["1","donald","duck"]},{"id":2,"cell":["2","daffy","duck"]}]}
Клиентская сторона: скрипты в том же порядке,
- jquery-1.5.min.js
- jquery-ui-1.8.9.custom.min.js
- grid.locale-en.js
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