0

你好 StackOverFlow 国家。我正在尝试向 jqGrid 添加信息,该信息是从 MySQL 数据库中检索的。我有两个文件 => index.html 和 data.php (都在同一个目录中)

index.html 来源 =>

<script type="text/javascript">
$(function(){
    $("#jqGrid_tb").jqGrid({
        url: "data.php",
        datatype: "json",
        sortable: true,
        height: "auto",
        colNames: ["Name","Surname","Birth Year","Famous Film"],
        colModel: [
            {name: "name", index: "name", width: "150"},
            {name: "surname", index: "surname", width: "150"},
            {name: "b_year", index: "year", width: "150"},
            {name: "film", index: "film", width: "200"}
        ],
        rowNum: 5,
        rowList: [5,10,15],
        viewrecords: true,
        pager: $("#pager"),
        caption: "Famous Actors",
    }).navGrid("#pager");
});
</script>

<div id="grid">
    <table id="jqGrid_tb"></table>
    <div id="pager"></div>
</div>

data.php 源 =>

include ("JSON.php");

$json = new Services_JSON();

$con = new mysqli("host","user","pswd","db");

if (!$con->connect_errno){
    if ($r = $con->query("SELECT * FROM actors")){
        while ($row = $r->fetch_assoc()){
            $info[] = array(
                "name" => $row[name],
                "surname" => $row[surname],
                "b_year" => $row[b_year],
                "film" => $row[film],
            );
        }
        $r->free();
    }
}

echo $json->encode($info);

if (isset($con)){
    $con->close();
}

jqGrid在index.html文件中显示没有任何信息,打开data.php文件信息时也成功编码成JSON格式,有什么问题我不明白。请帮忙,谢谢...

4

2 回答 2

1

你应该包括

jsonReader: {
    repeatitems: false,
    root: function (obj) { return obj; },
    page: function (obj) { return 1; },
    total: function (obj) { return 1; },
    records: function (obj) { return obj.length; }
}

作为 jqGrid 的附加选项,您不想更改输入数据的格式。此外,您应该指定哪些值应该将 jqGrid 分配为id行的属性。您可以包含附加id属性作为每个返回项目的附加属性,也可以将属性添加到包含唯一值key: true的列 (in )。colModel例如,如果您可以保证来自的值"name"已经是唯一的,那么您可以在列key: true的定义中包含属性"name"

此外,您可以考虑使用loadonce: truejqGrid 选项。在这种情况下,网格的全部数据将一次加载,数据的排序、分页和搜索(过滤)将由客户端的 jqGrid 实现,而无需在服务器端实现一些额外的代码。如果网格中有大量行(数百或数千行),则不应使用该选项。

于 2012-10-22T11:57:18.357 回答
1

您的回复格式错误。您可以转到jqGrid Demos页面,在展开Loading Data然后选择JSON Data后,您将在其中找到 PHP/MySQL 的示例。

正确的数据格式应如下所示:

{
    "total": "1",
    "page": "1",
    "records": "2",
    "rows": [
        { "name": "Robert", "surname": "De Niro", "b_year": "1943", "film": "Once Upon A Time In America" },
        { "name": "Al", "surname": "Pacino", "b_year":"1971", "film": "Scent Of A Woman"}
    ]
}

在哪里:

  • total--> 总页数
  • page--> 当前页码
  • records--> 记录总数
  • rows--> 数据行

此外,如果您希望行成为对象,则需要repeatitems在 jqGridjsonReader选项中禁用:

$("#jqGrid_tb").jqGrid({
    ...
    jsonReader: { repeatitems: false }
});

还建议行具有唯一性id以供以后参考。

于 2012-10-22T11:32:43.773 回答