jqgrid无法通过set jsonReader,repeatitems:false工作

时间:2013-03-22 12:04:01

标签: jquery jqgrid jqgrid-php

jqgrid无法通过set jsonReader,repeatitems:false

工作

首先我的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);

:网络响应;

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

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

2 个答案:

答案 0 :(得分: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的值

答案 1 :(得分: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

中的

所以如果你选择键值方式;单元格不应该在内容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"}]}}

改为

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

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

和num必须从0开始,因为php中的数组必须设置从0开始,否则结果jsonString将是

-

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