jqGrid没有addJSONData方法 [英] jqGrid has no addJSONData method

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

问题描述

今天下午我正在玩jqGrid,并且使用本地数据源工作得很好。但是,现在我正试图让它加载本地JSON数据。

I'm just playing around with jqGrid this afternoon, and have it working fairly well with a local array data source. However, now I'm trying to get it to load local JSON data.

我的代码如下:

jQuery("#list4").jqGrid({
   datatype: "json", //<-- Also tried "local" here
   height: 'auto',
   autowidth: true,
   forceFit: true,
   colNames:['ID','Name'],
   colModel:[
      {name:'id',index:'id', width:60, sorttype:"int", jsonmap:"id"},
      {name:'name',index:'name', width:90, jsonmap: "name"}
   ],
   multiselect: false,
   caption: "Test"
});

然后尝试使用以下内容加载JSON数据:

I then try to load JSON data using the following:

jQuery("#list4").jqGrid.addJSONData(json);

问题是 jQuery(#list4)。jqGrid.addJSONData 未定义。我也尝试过:

The issue is that jQuery("#list4").jqGrid.addJSONData is undefined. I've also tried:

jQuery("#list4").jqGrid('addJSONData', json);

抛出异常,说方法 addJSONData 未定义。我可以在 jQuery(#list4)。jqGrid 上看到其他记录的方法,而不是这一个。 addXMLData 也缺失了。但是,我可以验证这些方法是否在 jquery.jqGrid.min.js 源代码中。

Which throws an exception saying that the method addJSONData is not defined. I can see other documented methods on jQuery("#list4").jqGrid, just not this one. addXMLData is also missing. However, I can verify that these methods are in the jquery.jqGrid.min.js source code.

I今天刚刚下载了jqGrid,所以我知道我有最新版本的一切。

I just downloaded jqGrid today, so I know I have the latest versions of everything.

我一定做错了什么,但我不确定它是什么。我把整个页面放在这里:

I must be doing something wrong, but I'm not sure what it could be. I've put the entire page here:

http://pastie.org/3825067

推荐答案

addJSONData 是一种非常古老的方法,它使用仍然expandos到网格的DOM元素(< table> 元素)。所以要正确使用 addJSONData ,应该使用

The addJSONData is very old method which uses still expandos to the DOM element of the grid (<table> element). So to use addJSONData correctly one should use

jQuery("#list4")[0].addJSONData(json);

参见文档。更多beter方式将创建jqGrid并直接填充数据。您可以使用

See the documentation. More beter way will be to create jqGrid and fill the data directly. You can use

jQuery("#list4").jqGrid({
    datatype: "local",
    data: mydata,
    height: 'auto',
    autowidth: true,
    colNames: ['ID', 'Name'],
    colModel: [
        {name: 'id', index: 'id', width: 60, sorttype: "int", key: true},
        {name: 'name', index:'name', width: 90}
    ],
    caption: "Test",
    gridview: true // !!! improve the performance
});

mydata 的格式可以像

var mydata = [
        {id: 10, name: "Oleg"},
        {id: 20, name: "Mike"}
    ];

允许使用本地分页,过滤和数据排序。输入数据需要进行排序。

It's allow to use local paging, filtering and sorting of data. The input data need not be sorted.

或者您可以使用数据类型:'jsonstring' datastr datastr 的值可以是JSON字符串或已解析的对象。来自 datastr 的数据必须正确排序(如果您使用某些 sortname sortorder 参数)并具有与数据类型相同的格式:'json'(参见文档)。可以使用 jsonReader jsonmap 来指定数据格式:

Alternatively you can use datatype: 'jsonstring' and datastr. The value of datastr can be either JSON string or already parsed object. The data from datastr have to be correctly sorted (if you use some sortname and sortorder parameters) and have the same format as for datatype: 'json' (see the documentation). One can use jsonReader and jsonmap to specify the data format:

var mydata = {
        //total: 1,  // will be ignored
        //page: 1,   // will be ignored
        //records: 2 // will be ignored
        rows: [
            {id: 10, name: "Oleg"},
            {id: 20, name: "Mike"}
        ]
    ];

对我来说最奇怪的是为什么你需要加载本地JSON数据? 本地阵列数据源的区别在哪里?您可以使用 $ .parseJSON 将输入数据转换为对象或使用数据类型:'jsonstring'直接。在大多数情况下, addJSONData 的使用是因为每个jQuery.ajax手动从服务器加载数据这是真正的想法(参见一个)来自我在stackoverflow上的第一篇帖子这里)。 jqGrid有很多自定义选项和回调,如 ajaxGridOptions serializeGridData beforeProcessing ,你可以使用 jsonReader 中的函数(参见这里)和 jsonmap ,它允许您实际加载任何格式输入数据。使用 prmNames serializeGridData postData (参见此处)你可以几乎任何自定义发送到服务器的参数。因此,在非常非常少的情况下,确实需要使用低级 addJSONData

What is the most strange for me is why you need to load "local JSON data"? Where is the difference to the "local array data source"? You can use $.parseJSON to convert the input data to object or use datatype: 'jsonstring' directly. In the most cases the usage of addJSONData is because of loading the data from the server manually per jQuery.ajax which is really bed idea (see one from my first posts on the stackoverflow here). jqGrid has a lot of customization options and callbackes like ajaxGridOptions, serializeGridData and beforeProcessing, you can use functions in jsonReader (see here) and jsonmap, which allows you to load practically any format of input data. Using prmNames, serializeGridData and postData (see here) you can make practically any customization of the parameters sent to the server. So the usage of low-level addJSONData are needed really in extremely very seldom scenarios.

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

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