Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
830 views
in Technique[技术] by (71.8m points)

asp.net - jqgrid jsonReader configuration

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.

Here is my grid:

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
        });

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

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

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

look for comments... many thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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;
    }
}

or (if you use loadonce: true) just

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

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

enter image description here

after the alert message.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...