2013-03-22 30 views
0

的jqGrid不能设置jsonReader,repeatitems工作:假的jqGrid不能设置jsonReader,repeatitems工作:假

首先我的jqGrid是通过的getData阵列

$(listvar).jqGrid({ 

} 
$responce = new stdClass(); 

    $responce -> page = $page; 

    $responce -> total = $total_pages; 

    $responce -> records = $count; 

    $responce -> rows[$num]['id'] = $row["id"]; 

$responce -> rows[$num]['cell']= array("fid" => $row['fid'], "fname" => $row['fname']); 

echo json_encode($responce); 

正常工作; ;但我想通过关键的价值; 然后我改变代码,所以jqgrid可以通过键值方式获取数据; 我引用

Using key/value pairs for jqGrid cell data

http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_data


$(listvar).jqGrid({ 

enter code here`jsonReader : { 

repeatitems : false, 
    //others default value; 

    }, 

} 

mycode的:

$responce = new stdClass(); 

    $responce -> page = $page; 

    $responce -> total = $total_pages; 

    $responce -> records = $count; 

$responce -> rows[$num]['id'] = $row["id"]; 

$responce -> rows[$num]['cell']= array("fid" => $row['fid'], "fname" => $row['fname']); 

echo json_encode($responce); 

:网络repons e;

{"page":"1","total":1,"records":"1","rows":{"id":1,"cell": [{"fid":"153","fname":"\u624b\u673a"}]}} 

但jqgrid无法显示数据;问题是什么 ?

回答

0
rows an array that contains the actual data 
    id the unique id of the row 
cell an array that contains the data for a row 

http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_data

所以如果你选择关键价值方式; cell不应该在内容json字符串中;

$responce -> rows[$num]['id'] = $row["id"]; 

$responce -> rows[$num]['cell']= array("fid" => $row['fid'], "fname" => $row['fname']); 
**result:** "rows":{"id":1,"cell": [{"fid":"153","fname":"\u624b\u673a"}]}} 

改变为从0

$responce -> rows[$num]= array("id"=>"id","fid" => $row['fid'], "fname" => $row['fname']); 

reponse json:** "rows":[{"id":null,"fid":"153","fname":"\u624b\u673a" 

和num必须因为阵列在PHP必须设置为0开始,否则结果jsonString将是

-

result:"rows":{"1":{"id":null,"fid":"153","fname":"\u624b\u673a" 
1

您发布的JSON数据包含rows对象而不是数组。这是你的问题。你应该改变你的服务器代码,以便它产生

{ 
    "page": "1", 
    "total": 1, 
    "records": "1", 
    "rows": [ 
     { 
      "id": 1, 
      "cell": [ 
       { 
        "fid": "153", 
        "fname": "手机" 
       } 
      ] 
     } 
    ] 
} 

,而不是

{ 
    "page": "1", 
    "total": 1, 
    "records": "1", 
    "rows": { 
     "id": 1, 
     "cell": [ 
      { 
       "fid": "153", 
       "fname": "手机" 
      } 
     ] 
    } 
} 

(见更换{}[{}, ...,{}]rows值)