我仍然难以使用 jqGrid 显示数据。我找到了一些可能对我有帮助的资源,比如这里的资源,甚至可以复制这里的代码。
我一直试图解决这个问题好几个小时,但我仍然没有这样做。有人可以帮帮我吗?以下是我目前拥有的代码:
HTML/Javascript 代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" media="screen" href="css/smoothness/jquery-ui- 1.10.3.custom.min.css"/>
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css"/>
<style>
html, body {
margin: 0;
padding: 0;
font-size: 75%;
}
</style>
<script type="text/javascript" src="js/jquery-1.10.0.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.10.3.custom.min.js"></script>
<script type="text/javascript" src="js/i18n/grid.locale-en.js"></script>
<script type="text/javascript" src="js/jquery.jqGrid.min.js"></script>
<script type="text/javascript">
jQuery().ready(function (){
jQuery("#adminlist").jqGrid({
url:'cnc_admin_search.php',
datatype: "json",
colNames:['Record ID','Admin ID', 'Password', 'Last Name','First Name','Type Code'],
colModel:[
{name:'id',index:'id', width:55},
{name:'adminid',index:'adminid', width:90, jsonmap:"adminid"},
{name:'adminpass',index:'adminpass asc, adminid', width:100},
{name:'lname',index:'lname', width:80, align:"right"},
{name:'fname',index:'fname', width:80, align:"right"},
{name:'admintype',index:'admintype', width:80,align:"right"}
],
rowNum:10,
rowList:[10,20,30],
pager: '#adminpager',
sortname: 'id',
viewrecords: true,
sortorder: "desc",
jsonReader: {
repeatitems: false,
id: "id",
root: function (obj) { return obj; },
page: function (obj) { return 1; },
total: function (obj) { return 1; },
records: function (obj) { return obj.length; }
},
caption: "JSON Mapping",
height: '100%'
}).navGrid('#adminpager',{edit:false,add:false,del:false});
});
</script>
<title>Administrator Panel - Super Administrator</title>
</head>
<body>
<header>
<h2>Administrator Panel - Super Administrator</h2>
</header>
<div class="content">
<table id="adminlist"><tr><td></td></tr></table>
<div id="adminpager"></div>
</div>
<footer></footer>
</body>
</html>
PHP/数据库代码:
<?php
$page = 1; // get the requested page
$limit = 10; // get how many rows we want to have into the grid
$sidx = 0; // get index row - i.e. user click to sort
$sord = 'desc'; // get the direction
if(!$sidx) $sidx =1;
// connect to the database
$db = mysql_connect('localhost', 'root', '')
or die("Connection Error: " . mysql_error());
mysql_select_db('caneco_db') or die("Error conecting to db.");
$result = mysql_query("SELECT COUNT(*) AS count FROM caneco_admintable");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];
if( $count >0 ) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
if ($start<0) $start = 0;
$SQL = "SELECT * FROM caneco_admintable ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$responce->rows[$i]=$row;
$i++;
}
json_encode($responce); // coment if php 5
break;
?>
我希望有人能帮助我。谢谢你。
更新:
我包含了由我的 php 文件编码的 json 数据:
{"page":1,"total":1,"records":"2","rows":[{"Admin_UserNum":"751497003","Admin_UserID":"jrmthibaler","Admin_Passwrd":"44c8ac3a1f03f006febc1075b2ba567d","Admin_LastName":"Hibaler","Admin_FirstName":"Jairo","Admin_TypeCode":"1"}, {"Admin_UserNum":"704006278","Admin_UserID":"rojaihibaler","Admin_Passwrd":"8810534d4f44d557d92a2110d664dcf6","Admin_LastName":"Hibaler","Admin_FirstName":"Jairo","Admin_TypeCode":"2"}]}
这是我之前包含的 php 文件生成的 JSON 数据。但是,有一个错误说“在第 25 行从空值创建对象”,即以下代码行:
$responce->page = $page;
那行代码可能有什么问题?其他两行类似的代码没有产生相同的错误。