JQGrid“未定义"错误 [英] JQGrid 'undefined' error

查看:192
本文介绍了JQGrid“未定义"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用JQGrid时出现JavaScript错误:

消息:'undefined'为null或不是对象"

"Message: 'undefined' is null or not an object"

在服务器上进行调试时,我看到JSON输出如下所示:("id"值不在双引号内是否重要?)

{
"page":"1",
"total":"20",
"records":"5",
"rows":[
  {"id":1,"name":"Sam","phone":"732-333-2222"},
  {"id":2,"name":"Dan","phone":"000-222-1111"},
  {"id":6,"name":"George","phone":"333333"},
  {"id":4,"name":"Jerry","phone":"332-333-4444"},
  {"id":7,"name":"John","phone":"666666"},
  {"id":8,"name":"Tom","phone":"3333"}]
}

..和我的页面如下:

<script type="text/javascript">
  jQuery(document).ready(function(){

  jQuery("#list").jqGrid({
  url:'/myUrlPage',
  datatype: 'json',
  mtype: 'GET',
  colNames:['Id', 'Name', 'Phone'],
  colModel :[
    {name:'id', index:'id', width:55},
    {name:'name', index:'name', width:90},
    {name:'phone', index:'phone', width:150, sortable:false} ],
  pager: jQuery('#pager'),
  rowNum:10, rowList:[10,20,30],
  sortname: 'id',
  sortorder: "desc",
  viewrecords: true,
  imgpath: 'themes/basic/images',
  caption: 'My first grid' }); });
</script>

推荐答案

如果包含,您的主要问题将得到解决

You main problem will be solved if you include

jsonReader: { repeatitems: false }

jpGrid中的

参数.请参见 jqGrid文档.

parameter in your jqGrid. See details in the jqGrid documentation.

此外,我还对您的演示进行了一些修改.您可以在此处看到.我建议您删除弃用的 imgpath参数.取而代之的是,我建议您使用height: 'auto',在大多数情况下,该效果会很好.最好使用'#pager'代替jQuery('#pager').如果使用了分页器和viewrecords: true,则还应该为某些列增加width的值.我在演示中包含了jQuery("#pager_left").hide();语句,该语句隐藏了您现在不使用的寻呼机的某些块.如果您将开始使用导航器按钮,则应删除线.

Moreover I modified a little your demo. You can see it here. I recommend you remove deprecated imgpath parameter. Instead of that I recommend you to use height: 'auto' which gives you good results in the most cases. Instead of jQuery('#pager') is better to use just '#pager'. You should additionally increase the value of the width for some columns in case of the usage of pager and viewrecords: true. I included in my demo the jQuery("#pager_left").hide(); statement which hide some block of the pager which you not use now. If you will start to use navigator buttons you should remove the line.

关于您使用的JSON数据的另一条评论. idpagetotalrecords属性的值可以是字符串或整数,因此"id":1将为您提供与"id":"1"相同的结果.

One more remarks about the JSON data which you use. The values of id, page, total and records properties can be either strings or integers, so "id":1 will gives you the same results as "id":"1".

重要的是要了解如何填充pagetotalrecords.您当前的值是page = 1,total = 20,records = 5,并且您的数据包含6行.所有数据都没有意义. jqgrid向服务器询问有关附加参数的信息,该参数附加到URL,以给出一页数据,每页10行(rowNum:10).您从服务器得到的答案意味着您的数据总共包含5个项目(记录= 5).如果按页面顺序(每页10个项目)订购数据(5个项目),则将有20页(总计= 20),从那里开始的第一页(页面= 1)将填充数据(6个项目). JSON数据中pagetotalrecords的奇异值跟随

It's important to understand how you should fill page, total and records. You current values are page=1, total=20, records=5 and you data contain 6 rows. All the data has no sense. The jqgrid asked the server with respect of additional parameters which it appended to the URL to gives one page of the data with 10 rows per page (rowNum:10). Your answer from the server means that your data contain 5 items (records=5) totally. If you order the data (the 5 items) in pages (10 items per page) you will have 20 pages (total=20) and the first one from there (page=1) you are filled with the data (6 items). The strange values of page, total and records from your JSON data follows to strange values in the pager on the demo:

我建议您阅读答案,在这里我试图描述为什么jqGrid需要如此奇怪的JSON数据格式.

I would recommend you to read the answer where I tried to describe why jqGrid need so strange format of JSON data.

这篇关于JQGrid“未定义"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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