javascript jqGrid 没有 addJSONData 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10253938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jqGrid has no addJSONData method
提问by Mike Christensen
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.
我今天下午只是在玩 jqGrid,让它与本地数组数据源一起工作得很好。但是,现在我试图让它加载本地 JSON 数据。
My code is as follows:
我的代码如下:
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"
});
I then try to load JSON data using the following:
然后我尝试使用以下方法加载 JSON 数据:
jQuery("#list4").jqGrid.addJSONData(json);
The issue is that jQuery("#list4").jqGrid.addJSONData
is undefined. I've also tried:
问题jQuery("#list4").jqGrid.addJSONData
是未定义。我也试过:
jQuery("#list4").jqGrid('addJSONData', json);
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.
这会引发一个异常,表示该方法addJSONData
未定义。我可以在 上看到其他记录的方法jQuery("#list4").jqGrid
,但不是这个。 addXMLData
也不见了。但是,我可以验证这些方法是否在jquery.jqGrid.min.js
源代码中。
I just downloaded jqGrid today, so I know I have the latest versions of everything.
我今天刚刚下载了 jqGrid,所以我知道我拥有所有东西的最新版本。
I must be doing something wrong, but I'm not sure what it could be. I've put the entirepage here:
我一定是做错了什么,但我不确定它可能是什么。我把整个页面放在这里:
回答by Oleg
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
这addJSONData
是一个非常古老的方法,它仍然对网格的 DOM 元素(<table>
元素)使用 expandos 。所以要addJSONData
正确使用,应该使用
jQuery("#list4")[0].addJSONData(json);
See the documentation. More beter way will be to create jqGrid and fill the data directly. You can use
请参阅文档。更好的方法是创建 jqGrid 并直接填充数据。您可以使用
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
});
The format of mydata
can be like
的格式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 notbe sorted.
允许使用本地分页、过滤和数据排序。输入数据需要不进行排序。
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:
或者,您可以使用datatype: 'jsonstring'
和datastr
。的值datastr
可以是 JSON 字符串或已经解析的对象。来自的数据datastr
必须正确排序(如果您使用 somesortname
和sortorder
参数)并且具有与 for 相同的格式datatype: 'json'
(请参阅文档)。可以使用jsonReader
和jsonmap
来指定数据格式:
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"}
]
];
What is the most strange for me is whyyou need to load "local JSON data"? Where is the difference to the "local array data source"? You can use $.parseJSONto 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.ajaxwhich 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 anyformat 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.
对我来说最奇怪的是为什么需要加载“本地 JSON 数据”?与“本地数组数据源”的区别在哪里?您可以使用$.parseJSON将输入数据转换为对象或datatype: 'jsonstring'
直接使用。在大多数情况下的使用addJSONData
是因为加载每jQuery.ajax手动服务器中的数据这实在是床的想法(见一个从我在计算器第一帖在这里)。jqGrid 有很多自定义选项和回调,例如ajaxGridOptions
,serializeGridData
和beforeProcessing
,您可以使用jsonReader
(参见此处)和 中的函数jsonmap
,这使您几乎可以加载任何格式的输入数据。使用prmNames,serializeGridData
和postData
(见这里)你可以实际发送到服务器的参数的任何定制。所以addJSONData
在极少数情况下确实需要使用low-level 。
回答by Joe Doyle
For the most part, you are close. I don't think the addJSONData method is the way to go. Here's how we deal with local JSON data:
在大多数情况下,你很接近。我不认为 addJSONData 方法是要走的路。以下是我们处理本地 JSON 数据的方式:
The grid:
网格:
$("#list4").jqGrid({
datatype: "local", //<-- "local" tells jqGrid not to try and get the data itself
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"
});
Give data to the grid:
向网格提供数据:
// Clear the grid if you only want the new data
$('#list4').clearGridData(true);
// Set the data the tell the grid to refresh
$('#list4').setGridParam({ data: jsonData, rowNum: jsonData.length }).trigger('reloadGrid');
You should also change your jsonData to:
您还应该将 jsonData 更改为:
var jsonData = [
{id: 1, name: 'Apple'},
{id: 2, name: 'Banana'},
{id: 3, name: 'Pear'},
{id: 4, name: 'Orange'}
];
jqGrid is going to look to match up the columns specified to the objects passed into the array.
jqGrid 将寻找匹配指定给传入数组的对象的列。
回答by Mauricio
I'm working with version jqGrid 4.1.2
Having initialized the grid with a JSONReader and datatype:'jsonstring'
, when adding jsonstring data I've to include the datatype:'jsonstring'
parameter.
我正在使用 jqGrid 4.1.2 版本,使用 JSONReader 和 初始化网格datatype:'jsonstring'
,在添加 jsonstring 数据时,我必须包含datatype:'jsonstring'
参数。
$('#list4').setGridParam({ datastr: jsonData, datatype:'jsonstring', rowNum: jsonData.length }).trigger('reloadGrid');
As far as I know that is because after initialize the datatype:'jsonstring' is turned to datatype:'local', so when adding jsonstring it tries to load data from "data" param instead of "datastr" but because is empty no data is loaded.
据我所知,这是因为在初始化数据类型:'jsonstring' 后变成了数据类型:'local',所以当添加 jsonstring 时,它会尝试从“data”参数而不是“datastr”加载数据,但因为是空的没有数据已加载。
I hope to contribute to this ...
我希望为此做出贡献......