【问题标题】:Populate jqGrid from ASMX从 ASMX 填充 jqGrid
【发布时间】:2011-02-20 09:58:50
【问题描述】:

我一直在努力填充这个网格。

这会返回我的 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"]}]}

客户端:脚本顺序相同,

  1. jquery-1.5.min.js
  2. jquery-ui-1.8.9.custom.min.js
  3. grid.locale-en.js
  4. 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

【问题讨论】:

    标签: jquery jqgrid asmx


    【解决方案1】:

    您的主要错误是您在jsonReader 中使用了repeatitems: false,但您为repeatitems: true 构造了数据。

    另一个错误:如果从服务器返回的 JSON 数据确实是您发布的格式(我不确定是不是这样),那么您不应该在 success 句柄中使用 data.d。我想,您作为 JSON 包含的不是服务器响应,而是使用 data.dresults 变量的值。

    您使用带有 datatype 的非常旧的代码模板作为函数。没有它,您可以轻松地重写代码(例如,参见this old answer)。此外,如果您确实需要使用重命名的参数,例如page_index 而不是page,您可以更好地使用prmNames 来执行此操作。

    【讨论】:

    • 您好奥列格,感谢您的回复。你是对的,repeatitems: true 修复了它。您对 JSON 的看法也是正确的。我找到了 prmNames 的文档,但是有没有任何示例,因为这似乎完全符合我的要求。
    • 嗨 Oleg,我已经在下面发布了我更新的客户端代码,但它不会触发我的服务。请你看一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 2011-02-24
    • 2013-09-23
    相关资源
    最近更新 更多