jqGrid未加载数据 [英] jqGrid Not Loading Data

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

问题描述

我确定这很简单,我很想念,但似乎找不到.我在这里指定了一个简单的jqGrid:

I'm sure this is something simple that I'm missing, but I just can't seem to find it. I have a simple jqGrid specified here:

$('#mainGrid').jqGrid({
    datatype: 'local',
    colNames: ['id', 'name'],
    colModel: [
        { name: 'id', index: 'id', width: 100 },
        { name: 'name', index: 'name', width: 300 }
    ],
    rowNum: 9999,
    sortname: 'name',
    viewrecords: true,
    sortorder: 'asc',
    data: [{"id":"924c18a4-cad6-4b6a-97ef-f9ca61614530","name":"Pathway 1"},{"id":"54897f40-49ab-4abd-acac-6047006c7cc7","name":"Pathway 2"},{"id":"61542c48-102f-4d8e-ba9f-c24c64a20d28","name":"Pathway 3"},{"id":"c4ca9575-7353-4c18-b38a-33b383fcd8b2","name":"Pathway 4"}]
});

这将正确加载.简单的概念证明.现在,我尝试用对服务器资源的调用来替换本地数据:

This loads correctly. Simple proof of concept. Now I try to replace the local data with a call to a server resource:

$('#mainGrid').jqGrid({
    url: 'AJAXHandler.aspx',
    datatype: 'json',
    colNames: ['id', 'name'],
    colModel: [
        { name: 'id', index: 'id', width: 100 },
        { name: 'name', index: 'name', width: 300 }
    ],
    rowNum: 9999,
    sortname: 'name',
    viewrecords: true,
    sortorder: 'asc'
});

服务器资源返回相同的数据.但是网格没有加载数据. (至少,它没有显示任何记录.)我已经用FireBug确认该资源确实在被调用,并且正在返回预期的数据.

The server resource returns the same data. But the grid isn't loading the data. (At least, it's not showing any records.) I've confirmed with FireBug that the resource is indeed being called and is returning the expected data.

起初,我认为资源响应中的内容类型应更改为application/json,但这没什么区别.该响应是否还存在其他问题,导致网格无法加载数据?

At first I thought that the content type in the resource's response should be changed to application/json, but that made no difference. Is there something else wrong with that response that's preventing the grid from loading the data?

服务器资源的Firebug输出:

Firebug output for the server resource:

Response Headers
  Cache-Control     private
  Content-Length    261
  Content-Type      text/html; charset=utf-8
  Server            Microsoft-IIS/6.0
  MicrosoftSharePointTeamSe...    12.0.0.6219
  X-Powered-By      ASP.NET
  X-AspNet-Version  2.0.50727
  Set-Cookie        WSS_KeepSessionAuthenticated=80; path=/
  Date              Sat, 23 Apr 2011 14:35:43 GMT

Request Headers
  Host              cyber0ne.com
  User-Agent        Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16
  Accept            application/json, text/javascript, */*; q=0.01
  Accept-Language   en-us,en;q=0.5
  Accept-Encoding   gzip,deflate
  Accept-Charset    ISO-8859-1,utf-8;q=0.7,*;q=0.7
  Keep-Alive        115
  Connection        keep-alive
  X-Requested-With  XMLHttpRequest
  Referer           http://cyber0ne.com/dovetail_pages/Member/Pathways.aspx?MemberID=b428e0a7-dd55-de11-8e97-0016cfe25fa3
  Cookie            WSS_KeepSessionAuthenticated=80

Params
  _search   false
  nd        1303569347783
  page      1
  rows      9999
  sidx      name
  sord      asc

Response
  [{"id":"4d4b8249-b5f9-4da6-aba2-bf3af588d560","name":"Pathway 1"},{"id":"230184e6-44cc-4274-97fd-b455440cd9c0","name":"Pathway 2"},{"id":"7f938218-b963-495f-9646-f3cfb1e63ea1","name":"Pathway 3"},{"id":"2b17f23e-5500-4b01-ac1c-df2de90dc511","name":"Pathway 4"}]

更新:

根据@Paul Creasey的以下回答,现在的回复内容为:

Per @Paul Creasey's answer below, the response content is now:

{"total":4,"page":1,"records":4,"rows":[{"id":"55132687-b0bd-4c89-97cb-122d127429eb","name":"Pathway 1"},{"id":"123ba476-1560-4148-ae96-968bdd10e190","name":"Pathway 2"},{"id":"43f5660b-141c-4ccc-848e-6b41667b399a","name":"Pathway 3"},{"id":"b0d21316-d07d-4b46-8011-89c3cb07a8f6","name":"Pathway 4"}]}

行为已略有变化.网格现在显示正在加载",但仍然不会加载数据.

The behavior has changed slightly. The grid now says "Loading" but still doesn't load the data.

推荐答案

根据文档

According to the docs here, the expected json format is:

{ 
  "total": "xxx", 
  "page": "yyy", 
  "records": "zzz",
  "rows" : [
    {"id" :"1", "cell" :["cell11", "cell12", "cell13"]},
    {"id" :"2", "cell":["cell21", "cell22", "cell23"]},
      ...
  ]
}

因此,您的Web服务应该返回以下内容:

Therefore you web service should be returning this:

{
    "total": "4",
    "page": "1",
    "records": "4",
    "rows" : [
        {
            "id": "4d4b8249-b5f9-4da6-aba2-bf3af588d560",
            "name": "Pathway 1" 
        },
        {
            "id": "230184e6-44cc-4274-97fd-b455440cd9c0",
            "name": "Pathway 2" 
        },
        {
            "id": "7f938218-b963-495f-9646-f3cfb1e63ea1",
            "name": "Pathway 3" 
        },
        {
            "id": "2b17f23e-5500-4b01-ac1c-df2de90dc511",
            "name": "Pathway 4" 
        } 
    ]
}

目前只有行数组.

您可以实现自己的函数来读取json,但我从未做到过,请参见上面链接的"jsonReader作为函数"部分.

You could implement your own function to read the json, but i've never done this, see the "jsonReader as Function" section of the link above.

最初我错了,要么您应该将repeatitems标志设置为false:

I was wrong initially, either you should set the repeatitems flag to false:

jsonReader : {
   repeatitems: false
},

并使用上面的json(我认为!)或返回如下数据:

and use the json above (i think!) or return data like this:

{
    "total": "4",
    "page": "1",
    "records": "4",
    "rows" : [
        {
            "id": "4d4b8249-b5f9-4da6-aba2-bf3af588d560",
            "cells" : ["Pathway 1"]
        },
        {
            "id": "230184e6-44cc-4274-97fd-b455440cd9c0",
            "cells" : ["Pathway 2"]
        },
        {
            "id": "7f938218-b963-495f-9646-f3cfb1e63ea1",
            "cells" : ["Pathway 3"]
        },
        {
            "id": "2b17f23e-5500-4b01-ac1c-df2de90dc511",
            "cells" : ["Pathway 4"] 
        } 
    ]
}

我一直都做后者,但是我认为前者可能更好!

I've always done the latter, but i think the former is probably better!

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

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