本文介绍了jqGrid的jsonReader配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的最后的jqGrid我设置的网格。假设我需要设置jsonReader使电网知道从哪里得到的回报JSON我网格的数据。不过,我想了好几天后得到的空白单元格。

I'm new to jqgrid finally i've setup a grid. Suppose i need to setup jsonReader so that the grid knows where to get my grid-data in the json return. However i got blank cells after trying for days.

下面是我的网格:

jQuery("#list48").jqGrid({
            url: 'dbtest.aspx/get_offsite_history2',
            datatype: "json",
            mtype: 'POST',
            ajaxGridOptions: { contentType: "application/json" },
            serializeGridData: function(postData) {
                return JSON.stringify(postData);
            },
            jsonReader: {
                root: function(obj) { alert(JSON.stringify(obj.d)); return obj.d; },
                repeatitems: false
            },
            height: 'auto',
            rowNum: 30,
            rowList: [10, 20, 30],
            colNames: ['name', 'start_date', 'duration', 'offsite_cat'],
            colModel: [
                          { name: 'name', index: 'name', width: 80, align: 'left', editable: true, edittype: 'text' },
                          { name: 'start_date', index: 'start_date', width: 120, align: 'left', editable: true, edittype: 'text' },
                          { name: 'duration', index: 'duration', width: 120, align: 'left', editable: true, edittype: 'text' },
                          { name: 'offsite_cat', index: 'offsite_cat', width: 120, align: 'left', editable: true, edittype: 'text'}],
            pager: "#plist48",
            viewrecords: true,
            sortname: 'name',
            caption: "Grouping Array Data",
            gridview: true
        });

这是从URL dbtest.aspx / get_offsite_history2服务器返回:

This is the server return from url dbtest.aspx/get_offsite_history2:

{"d":"[{\"name\":\"A\",\"start_date\":\"B\",\"duration\":\"C\",\"offsite_cat\":\"D\"}]"}

我想通过设置根:'D'来获得结果,但我得到了64空行对于...

i suppose to get the result by setting "root: 'd'" but i got 64 blank rows for that...

看征求意见......非常感谢

look for comments... many thanks

推荐答案

你的问题的原因是在你的服务器code中的错误。你让序列化到JSON的两次 D 服务器响应的属性反序列化后,您仍然得到JSON字符串(!),而不是对象。典型的错误是在Web方法 JavaScriptSerializer.Serialize 手动使用。每个人都应该返回的目标本身,而不是它是序列化的结果字符串。

The reason of your problem is the bug in your server code. You make serialization to JSON twice. After deserializing of d property of the server response you get still JSON string (!!!) instead of object. Typical error is manual usage of JavaScriptSerializer.Serialize in the web method. One should return the object itself instead of the string which is the result of serializing.

如果没有当前服务器$ C $的修改C可以用法解决问题。

Without modifying of your current server code you can fix the problem by usage of

jsonReader: {
    root: function (obj) {
        alert(typeof obj.d === "string" ? obj.d : JSON.stringify(obj.d));
        return typeof obj.d === "string" ? $.parseJSON(obj.d) : obj.d;
    },
    repeatitems: false,
    page: function () { return 1; },
    total: function () { return 1; },
    records: function (obj) {
        return typeof obj.d === "string" ? $.parseJSON(obj.d).length : obj.length;
    }
}

或者(如果你使用 loadonce:真正的)刚

jsonReader: {
    root: function (obj) {
        return typeof obj.d === "string" ? $.parseJSON(obj.d) : obj.d;
    },
    repeatitems: false
}

由于当前服务器code似乎没有实现数据的分页,你应该增加的rowNum 一些足够大的值,比如的rowNum:10000 或使用 loadonce:真正的

Because your current server code seems not implemented the paging of data you should increase rowNum to some large enough value like rowNum: 10000 or to use loadonce: true.

更新时间::您可以找到这里修改演示其中工程。它显示

UPDATED: You can find here modified demo which works. It displays

警报消息。

这篇关于jqGrid的jsonReader配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:57