Mvcjqgrid数据类型:json用于url请求,json响应无法填充



我想向服务器发出一个请求(同步)来填充jqGrid。我这样做了:

$table.jqGrid({ 
                    datatype: "json",
                    url:'takepage/page=1',
                    mtype: 'GET',
                    ajaxSubgridOptions: { async: false },
                    colNames:['Result','Test'], 
                    colModel:[ {name:'result',index:'result',width:120}, 
                               {name:'test',index:'test', width:120}
                              ], 
                     pager: "#"+pager,
                     caption: "TESTjqGrid sync request to server",
                     jsonReader: {
                            repeatitems: false,
                            page: function(obj) { 
                                return obj.page; 
                            },
                            total: function(obj) { 
                                return obj.total; 
                            },
                            root: function (obj) { 
                                console.log(obj);
                                return obj; 
                            },
                            records: function (obj) { 
                                console.log(obj.rows.length);
                                return obj.rows.length; 
                            }
                        }
                }).jqGrid('navGrid', "#"+pager, {
                    add:    false,
                    edit:   false,
                    del:    false,
                    search: false,
                    refresh:false
                });

服务器的json响应如下:

{"total":1,"page":1,"rows":[{"result":null,"test":"val"}],"records":1}

错误在哪里?谢谢!

您应该使用与您发布的数据相对应的jsonReader。在JSON数据的情况下,你发布你应该使用jsonReader: {repeatitems: false}jsonReader的许多其他选项是正确的,但root是错误的。您必须jsonReader中删除 root属性或将其更改为root: "rows"root: function (obj) { return obj.rows; }(使用return obj;为假)。

另外,如果您的服务器没有实现服务器端数据分页,我建议您使用loadonce: true。无论如何,建议使用gridview: trueheight: "auto"选项

最新更新