jqGrid JSON - 为什么 jqGrid 不喜欢这个 JSON?有人可以提供适用于该字符串的脚本和标记吗?

发布于 2024-11-06 15:18:01 字数 3543 浏览 0 评论 0原文

这是 JSON:

{
    "page": "1",
    "total": "1",
    "records": "2",
    "rows": [
        {
            "Id": 3,
            "FirstName": "Test",
            "LastName": "Testerson",
            "CustomerNumber": "1234",
            "EmailAddress": "[email protected]",
            "Subject": "Test1",
            "OrderNumber": "4321",
            "Comments": "This is a test"
        },
        {
            "Id": 4,
            "FirstName": "Test2",
            "LastName": "Testerson2",
            "CustomerNumber": "2222",
            "EmailAddress": "[email protected]",
            "Subject": "Test1",
            "OrderNumber": "3333",
            "Comments": "This is a test"
        }
    ]
}

这是我当前的脚本和标记(为什么这不起作用?):

<table id="list1"></table>
<div id="pager1"></div>
<script type="text/javascript">
    function getData(pdata) {
        var params = new Object();
        params.page = pdata.page;
        params.pageSize = pdata.rows;
        params.sortIndex = pdata.sidx;
        params.sortDirection = pdata.sord;


        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "ScriptServices/PNScriptService.asmx/GetContactRequests",
            data: JSON.stringify(params),
            dataType: "json",
            success: function (data, textStatus) {
                if (textStatus == "success") {
                    var thegrid = $("#list1")[0];
                    thegrid.addJSONData(data.d);
                }
            },
            error: function (data, textStatus) {
                alert('An error has occured retrieving the data.');
            }
        });
    }

    $(document).ready(function () {
        $("#list1").jqGrid({
            datatype: function (pdata) {
                getData(pdata);
            },
            jsonReader: {
                page: "page",
                total: "total",
                records: "records",
                root: "rows",
                id: "Id",
                repeatitems: false
            },
            colNames: ['Id', 'FirstName', 'LastName', 'CustomerNumber', 'EmailAddress', 'Subject', 'OrderNumber', 'Comments'],
            colModel: [
                { name: 'Id', index: 'Id', width: 90 },
                { name: 'FirstName', index: 'FirstName', width: 100 },
                { name: 'LastName', index: 'LastName', width: 100 },
                { name: 'CustomerNumber', index: 'CustomerNumber', width: 80, align: "right" },
                { name: 'EmailAddress', index: 'EmailAddress', width: 100 },
                { name: 'Subject', index: 'Subject', width: 80 },
                { name: 'OrderNumber', index: 'OrderNumber', width: 80, align: "right" },
                { name: 'Comments', index: 'Comments', width: 250, sortable: false }
            ],
            rowNum: 10,
            rowList: [10, 20, 30],
            pager: $('#pager1'),
            sortname: 'Id',
            viewrecords: true, 
            sortorder: "desc",
            caption: "Contact Requests"
        });
    });
</script>

顺便说一句:上面的 JSON 字符串是脚本服务返回的确切字符串。我可以在脚本服务中打断点并获取该字符串,并且可以在成功时提醒调用 ajax 脚本中的字符串,并且这些字符串是相同的。所以我知道这不是脚本服务,而且我知道 json 字符串正被传递到 jqGrid,如图所示。

Here's the JSON:

{
    "page": "1",
    "total": "1",
    "records": "2",
    "rows": [
        {
            "Id": 3,
            "FirstName": "Test",
            "LastName": "Testerson",
            "CustomerNumber": "1234",
            "EmailAddress": "[email protected]",
            "Subject": "Test1",
            "OrderNumber": "4321",
            "Comments": "This is a test"
        },
        {
            "Id": 4,
            "FirstName": "Test2",
            "LastName": "Testerson2",
            "CustomerNumber": "2222",
            "EmailAddress": "[email protected]",
            "Subject": "Test1",
            "OrderNumber": "3333",
            "Comments": "This is a test"
        }
    ]
}

Here's my current script and markup (why isn't this working?):

<table id="list1"></table>
<div id="pager1"></div>
<script type="text/javascript">
    function getData(pdata) {
        var params = new Object();
        params.page = pdata.page;
        params.pageSize = pdata.rows;
        params.sortIndex = pdata.sidx;
        params.sortDirection = pdata.sord;


        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "ScriptServices/PNScriptService.asmx/GetContactRequests",
            data: JSON.stringify(params),
            dataType: "json",
            success: function (data, textStatus) {
                if (textStatus == "success") {
                    var thegrid = $("#list1")[0];
                    thegrid.addJSONData(data.d);
                }
            },
            error: function (data, textStatus) {
                alert('An error has occured retrieving the data.');
            }
        });
    }

    $(document).ready(function () {
        $("#list1").jqGrid({
            datatype: function (pdata) {
                getData(pdata);
            },
            jsonReader: {
                page: "page",
                total: "total",
                records: "records",
                root: "rows",
                id: "Id",
                repeatitems: false
            },
            colNames: ['Id', 'FirstName', 'LastName', 'CustomerNumber', 'EmailAddress', 'Subject', 'OrderNumber', 'Comments'],
            colModel: [
                { name: 'Id', index: 'Id', width: 90 },
                { name: 'FirstName', index: 'FirstName', width: 100 },
                { name: 'LastName', index: 'LastName', width: 100 },
                { name: 'CustomerNumber', index: 'CustomerNumber', width: 80, align: "right" },
                { name: 'EmailAddress', index: 'EmailAddress', width: 100 },
                { name: 'Subject', index: 'Subject', width: 80 },
                { name: 'OrderNumber', index: 'OrderNumber', width: 80, align: "right" },
                { name: 'Comments', index: 'Comments', width: 250, sortable: false }
            ],
            rowNum: 10,
            rowList: [10, 20, 30],
            pager: $('#pager1'),
            sortname: 'Id',
            viewrecords: true, 
            sortorder: "desc",
            caption: "Contact Requests"
        });
    });
</script>

btw: The JSON string above is the exact string returned by the script service. I can hit a break point in my script service and grab that string, and I can alert the string in the calling ajax script on success, and the strings are identical. So I know it's not the script service, and I know that that json string is being passed, as shown, into the jqGrid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水中月 2024-11-13 15:18:01

您的问题是您使用非常非常旧的模板代码来加载 JSON 数据。如果您的服务器生成您发布的 JSON 数据,您可以将代码简化为 以下内容

其他答案中,您会找到更多代码示例,以防万一使用 ASMX,它会生成您发布的一些其他 JSON 代码。

Your problem is that you user very very old template code for loading JSON data. If your server produce the JSON data which you posted you can simplify for code to the following.

In another answers you will find more code examples in case you use ASMX, which produce a litle other JSON code as you posted.

蓦然回首 2024-11-13 15:18:01

感谢 Oleg 提供的信息,但结果如下:

thegrid.addJSONData(JSON.parse(data.d));

我需要在将数据传递到网格之前对数据进行 JSON.parse。

Thanks Oleg for the info, but it turned out to be the following:

thegrid.addJSONData(JSON.parse(data.d));

I needed to JSON.parse the data before passing it into the grid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文