hun*_*unt 7 json jqgrid jqgrid-php subgrid
我使用jqGrid 3.6.4和jquery 1.4.2.在我的示例中,我正在遵循json数据格式,我想将这些json数据映射到jqgrid的行中
{
"page": "1",
"total": 1,
"records": "6",
"rows": [
{
"head": {
"student_name": "Mr S. Jack ",
"year": 2007
},
"sub": [
{
"course_description": "Math ",
"date": "22-04-2010",
"number": 1,
"time_of_add": "2:00",
"day": "today"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我的jqgrid代码如下
jQuery("#"+subgrid_table_id).jqGrid({
url:"http://localhost/stud/beta/web/GetStud.php?sid="+sid,
dtatype: "json",
colNames: ['Stud Name','Year','Date'.'Number'],
colModel: [ {name:'Stud Name',index:'student_name', width:100, jsonmap:"student_name"},
{name:'Year',index:'year', width:100, jsonmap:"year"},
{name:'Date',index:'date', width:100, jsonmap:"date"},
{name:'Number',index:'number', width:100, jsonmap:"number"}
],
height:'100%',
jsonReader: { repeatitems : false, root:"head" },
});
Run Code Online (Sandbox Code Playgroud)
所以现在问题是我的数据,即student_name和year在"head"下,jqgrid可以找到这两个字段.同时其他两个列值,即日期和数字位于"子"下,甚至那些列我无法用jqgrid映射它
请帮助我如何在JQGrid中找到这些属性.
谢谢
Ole*_*leg 15
首先发布的代码有一些错误,dtatype: "json"
而不是datatype: "json"
.代码末尾的" },});
"而不是" }});
" colNames: ['Stud Name','Year','Date'.'Number']
代替colNames: ['Stud Name','Year','Date','Number']
.修复这个明确的错误后,您需要更改jsonmap
值.这是你的主要问题.固定代码如下所示:
jQuery("#"+subgrid_table_id).jqGrid({
...
datatype: 'json',
colNames: ['Stud Name','Year','Date'.'Number'],
colModel: [
{name:'student_name', width:100, jsonmap:"head.student_name"},
{name:'year', width:100, jsonmap:"head.year"},
{name:'date', width:100, jsonmap:"sub.0.date"},
{name:'number', width:100, jsonmap:"sub.0.number"}
],
jsonReader: { repeatitems:false, root:"rows" }
});
Run Code Online (Sandbox Code Playgroud)
你要修复root
到" rows
",并使用jsonmap
在JSON点号(见http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_dot_notation).我使用像" sub.0.number
" 这样的一个奇怪的符号,因为sub.0.number
在JavaScript中是相同的sub[0].number
.它现在有效.
我建议您再考虑一下您收到的JSON数据的结构.(请参阅我之前对你的评论问题):"sub"元素是否真的是一个总是有一个元素的数组,或者你想使用subgrids?也许数据应该从改变sub:[{"":"", ...}]
到sub:{"":"", ...}
?你想用什么作为rowid?student_name
?然后添加id: "head.student_name"
到jsonReader
定义或添加key: true
属性到列的定义student_name
.或者您忘记将其包含在JSON数据中?
最后一个建议.如果您打开http://trirand.com/blog/jqgrid/jqgrid.html并在树的左侧打开分支"数据映射"\"数据优化",您将看到一个示例,其中仅使用数组而不是命名JSON中的元素.这些数据的大小最小,可以更快地从服务器传输到客户端.你的数据有一些你根本不使用的字段(比如" course_description ").因此,如果您可以对服务器代码进行任何更改,请尝试优化数据传输速率.