jqgrid addJSONData不起作用 [英] jqgrid addJSONData doesn't work

查看:152
本文介绍了jqgrid addJSONData不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jqGrid,返回的json是

{"total":1,"page":1,"records":2,"rows":[{"projectId":"1022","name":"john"}]}

然后在我调用的函数中

 var jsongrid = eval("("+data+")");                  
 var thegrid = jQuery("#projectList")[0];                       
 thegrid.addJSONData(jsongrid);

但是它给我错误:object为null或未定义.不知道为什么.我没有使用json阅读器.

顺便说一句,您知道如何使用set方法设置总计",页面",记录"吗?

解决方案

我想您尝试在 之前调用addJSONData方法 >

几乎不需要使用addJSONData(请参阅我的第一篇有关主题的文章此处).同样,您永远都不要使用邪恶的eval方法.一个使用 jQuery.parseJSON

var $grid = $("#projectList");

$grid.jqGrid({
    url: 'user595234.json',
    datatype: "json",
    serializeGridData: function (data) {
        return $.param(data) + '&' + $("#project_search_form").serialize();
    },
    jsonReader: {id: "projectId", repeatitems: false},
    colNames: ['ID', 'Name'],
    colModel: [
        {name: 'projectId', width: 255},
        {name: 'name', width: 255}
    ],
    rowNum: 10,
    rowList: [10,20,30],
    pager: '#projectPager',
    sortname: 'projectId',
    viewrecords: true,
    sortorder: "desc",
    caption: "Simple data manipulation",
    height: "auto"
}).jqGrid("navGrid", "#projectPager", {edit: false, add: false, del: false});

$("#search").click(function () {
    $grid.trigger("reloadGrid", [{page: 1}]);
});

演示中,我仅从示例中获得了表格用法 jQuery.serialize 并对其进行一些修改.它显示您需要的数据

此外,就像您可以轻松验证Fiddler或Firebug一样,URL还会附加以下附加参数

...?_search=false&nd=1336057299806&rows=10&page=1&sidx=projectId&sord=desc&a=1&b=2&c=3&d=4&e=7&f=8 

标准参数

_search=false&nd=1336057299806&rows=10&page=1&sidx=projectId&sord=desc

将附加表格中的参数

a=1&b=2&c=3&d=4&e=7&f=8

在某些情况下,可以选择使用 jQuery.serializeArray .它允许以另一种格式(例如JSON)序列化数据或以另一种格式转换数据(请参见此处),可以使用 $ .extend 与标准jqGrid参数轻松合并.

I am working on jqGrid, the return json is

{"total":1,"page":1,"records":2,"rows":[{"projectId":"1022","name":"john"}]}

then in the function I call

 var jsongrid = eval("("+data+")");                  
 var thegrid = jQuery("#projectList")[0];                       
 thegrid.addJSONData(jsongrid);

but it give me error: object is null or undefined. don't know why . I didn't use json reader.

BTW, do you know how to use set method to set "total", "page", "records" ?

解决方案

I suppose that you try to call addJSONData method before the grid will be created with respect of jQuery("#projectList").jqGrid({...});

The usage of addJSONData is practically always unneeded (see one from my first posts about the subject here). In the same way you should never use eval method which is evil. One uses jQuery.parseJSON or JSON.parse instead.

I suppose that you should use datatype: 'json' to solve your problem. You should post more code to show you how you should use other jqGrid options in your case.

UPDATED: From your previous question it seems that you want just send additional data to the server from the form on clicking of the "Search" button. In the case I would suggest to modify the code to the following

var $grid = $("#projectList");

$grid.jqGrid({
    url: 'user595234.json',
    datatype: "json",
    serializeGridData: function (data) {
        return $.param(data) + '&' + $("#project_search_form").serialize();
    },
    jsonReader: {id: "projectId", repeatitems: false},
    colNames: ['ID', 'Name'],
    colModel: [
        {name: 'projectId', width: 255},
        {name: 'name', width: 255}
    ],
    rowNum: 10,
    rowList: [10,20,30],
    pager: '#projectPager',
    sortname: 'projectId',
    viewrecords: true,
    sortorder: "desc",
    caption: "Simple data manipulation",
    height: "auto"
}).jqGrid("navGrid", "#projectPager", {edit: false, add: false, del: false});

$("#search").click(function () {
    $grid.trigger("reloadGrid", [{page: 1}]);
});

In the demo I get just the form from the example of the usage jQuery.serialize and modify it a little. It display the data which you need

Additionally, like you can easy verify with respect of Fiddler or Firebug, the URL will ba appended with additional parameters like below

...?_search=false&nd=1336057299806&rows=10&page=1&sidx=projectId&sord=desc&a=1&b=2&c=3&d=4&e=7&f=8 

The standard parameters

_search=false&nd=1336057299806&rows=10&page=1&sidx=projectId&sord=desc

will be appended with parameters from the form

a=1&b=2&c=3&d=4&e=7&f=8

In some scenarios one can use jQuery.serializeArray alternatively. It allows to serialize the data in another format (like JSON) or convert the data in some another format (see here) which can be easy merged with the standard jqGrid parameters using $.extend.

这篇关于jqgrid addJSONData不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆