无法在json格式内从php向jqGrid添加数据 [英] can't add data to jqGrid from php within json format
问题描述
Hello StackOverFlow国家/地区。我正在尝试向jqGrid添加信息,这是从MySQL数据库中检索的。我有两个文件=> index.html和data.php(都在同一目录中)
Hello StackOverFlow nation . I'm trying to add information to jqGrid , which is retrieved from MySQL database. I've two files => index.html and data.php (both in the same directory)
index.html source =>
index.html source =>
<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 source =>
data.php source =>
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文件中显示没有任何信息,打开数据时也是如此.php文件信息成功编码成JSON格式,哪个错误我无法理解。请帮助,谢谢...
jqGrid is shown without any information in index.html file , also when opening data.php file information is successfully encoded into JSON format , whats wrong I can't understand . Please help , thanks ...
推荐答案
您的回复格式错误。您可以访问 jqGrid演示页面,在这里您可以找到PHP / MySQL的示例扩展加载数据,然后选择 JSON数据。
Your response format is wrong. You can go to jqGrid Demos page where you will find a sample for PHP/MySQL after expanding Loading Data and then choosing JSON Data.
正确的数据格式应如下所示:
The proper format of data should look like this:
{
"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"}
]
}
其中:
-
总计
- >总页数 -
页面
- >当前页码 -
记录
- >记录总数 -
rows
- >数据行
total
--> total count of pagespage
--> current page numberrecords
--> total count of recordsrows
--> the rows of data
此外,如果您希望行为ob jects,你需要在jqGrid中禁用 repeatitems
jsonReader
选项:
Also if you want rows to be objects, you need to disable repeatitems
in jqGrid jsonReader
options:
$("#jqGrid_tb").jqGrid({
...
jsonReader: { repeatitems: false }
});
还建议行具有唯一 id
供以后参考。
It is also adviced for rows to have unique id
for later reference.
这篇关于无法在json格式内从php向jqGrid添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!