1

在 jqGrid 中显示我的 json 数据时遇到问题。

我在这个论坛上搜索了很多,并尝试了各种形式来使其工作。如果这已经被回答,我很抱歉,但我真的需要帮助。

在服务器页面上,我只使用JavaScriptSerializer发送数据和带有默认参数的jsonreader函数(这工作正常)。

我现在需要分页并更改了我的服务器页面代码以使用sidxsordpagerows参数。

来自服务器的结果字符串如下所示:

{"total":"344","page":"1","records":"8577","root":[{"Id":"1","SerialNumber":"132","Name":"ServerName"},...]}

这是我的 jQuery 代码:

$("#list").jqGrid({
        datatype: "json",
        mtype: 'GET',
        url:'https://server/handlerpage.ashx',
        colNames:['Id','SerialNumber','Name'],
        colModel :[         
                {name:'Id', index:'Id', jsonmap:"Id", width:20},
                {name:'Name', index:'Name', jsonmap:"Name", width:120},
                {name:'SerialNumber', index:'SerialNumber', jsonmap:"SerialNumber", width:100}],    
        jsonreader: {repeatitems:false,id:'Id',root:'root'},
        pager: '#pager',
        rowNum:25,
        rowList:[25,50,75,100],
        sortname: 'Id',
        viewrecords:true,
        gridview: true,
        height:"400",
        width:"700",
        caption: 'Select from existing server',
        loadtext:'Loading, please wait'
      }).navGrid("#pager", { edit: false, add: false, search: true, del: false });
4

3 回答 3

3

为了使用 json 数据作为来自查询的响应,响应的格式必须正确。很难确定什么是正确的响应以及如何从文档中获取它。由于您的服务器设置生成警告作为响应的一部分,这将导致网格无法加载,因此可能会出现更多问题。

从文档来看,这是默认的 json 阅读器,这意味着如果您正确格式化响应,则无需添加任何内容/自定义 json 阅读器。

jsonReader : { 
  root: "rows", 
  page: "page", 
  total: "total", 
  records: "records", 
  repeatitems: true, 
  cell: "cell", 
  id: "id",
  userdata: "userdata",
  subgrid: { 
     root:"rows", 
     repeatitems: true, 
     cell:"cell" 
  } 

首先确保您有正确的响应格式。 {"page":1,"total":1,"records":"2","rows":[{"id":"1","cell":["1","mydata1"]},{"id":"2","cell":["2","mydata2"]}]}

文档页面上没有给出示例,但如果您的网格中只有两列,则上述内容是正确的。您需要获取服务器查询/解析服务器查询和传递给运行服务器端脚本的页面的值以返回此格式。文档中的 example.php 页面为您提供了所需的所有值。

此代码将为您提供正确的标题,以避免错误警告并与上面的响应相匹配。另请注意,在文档中,他们没有在关联数组索引名称的索引周围添加撇号。那将失败。

header('Content-type: application/json');$responce = new stdClass();$responce->page = $page;$responce->total = $total_pages;$responce->records = $count;$i=0;while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {$responce->rows[$i]['id']=$row['ID'];$responce->rows[$i]['cell']=array($row['ID'],$row['entity_name']); $i++; } echo json_encode($responce);

重新整理文档中缺少的内容(我可能只是错过了它们,即使 jQgrid 的文档是史诗/大师作品): 1. 没有提到需要 json 的标题声明(很明显使用 XML 示例) 2. 没有示例说明返回的工作字符串应该是什么样子 3. 关联数组索引的格式不正确。4. 没有提及如何避免在响应中返回 PHP 警告

于 2013-03-13T18:57:30.133 回答
1

尝试以下

jsonReader: {
    root: 'rows',
    page: 'page',
    total: 'total',
    records: 'records',
    repeatitems: true,
    cell: 'cell',
    id: 'id',
    userdata: 'userdata'
}
于 2012-03-27T18:01:11.807 回答
0

请确保服务器端脚本返回正确的带有HEADERS的 json 字符串

进一步检查演示站点和 JSON 示例

于 2012-03-27T17:43:47.233 回答