javascript jqgrid 加载数组数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4739535/
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 load array data
提问by Progress Programmer
I have a set of data like the following example and i would like to load it into the grid. However, i'm not sure how since the data doesn't have an name.
我有一组类似于以下示例的数据,我想将其加载到网格中。但是,我不确定如何,因为数据没有名称。
[[48803,"DSK1","","02200220","OPEN"],[48769,"APPR","","77733337","ENTERED"]]
采纳答案by Oleg
What you need is just use the following localReader
您只需要使用以下localReader
localReader: {
repeatitems: true,
cell: "",
id: 0
}
I made for you the demowhich shows live how it works.
UPDATED: How I could find out the reality is not so good as the documentation. The usage of localReadercould help you to fill the grid contain with data from dataparameter with the custom structure, but another parts of jqGrid: local sorting and searching don't work correctwith this structure of dataparameter. I interpret it as a bug. As a pragmatical solution I would recommend you to convert your custom data to array of named objects like
更新:我如何才能发现现实不如文档那么好。localReader的用法可以帮助您data使用自定义结构的参数填充包含数据的网格,但是 jqGrid 的另一部分:本地排序和搜索对于这种data参数结构 无法正常工作。我把它解释为一个错误。作为一种实用的解决方案,我建议您将自定义数据转换为命名对象数组,例如
[{id:48803,col2:"DSK1",col3:"",col4:"02200220",col5:"OPEN"},
{id:48769,col2:"APPR",col3:"",col4:"77733337",col5:"ENTERED"}]
with the names corresponds to the column names in the colModel. If you will use dataparameter in the form, everything will work perfect in jqGrid.
名称对应于colModel. 如果您将data在表单中使用参数,那么在 jqGrid 中一切都会完美运行。
UPDATED 2: Look at the source of the fixed exampleand it will be clear what I mean. In your case conversion of the data can be about the following
更新 2:查看固定示例的来源,就很清楚我的意思了。在您的情况下,数据的转换可能如下
var myNewData = [];
for (var i=0,l=mydata.length; i<l; i++) {
var d = mydata[i];
myNewData.push({id:d[0],col2:d[1],col3:d[2],col4:d[3],col5:d[4]});
}
The solution is not so elegant like with localReader, but it work without any restrictions.
该解决方案不像 with 那样优雅localReader,但它可以不受任何限制地工作。
回答by Jeff B
Well, I'm not very familiar with jqgrid, but you could simply assign your data to an associative array and then load it.
好吧,我对 jqgrid 不是很熟悉,但是您可以简单地将数据分配给关联数组,然后加载它。
Example here:
这里的例子:

