我是jqGrid的新手。我正在尝试从我的网格中的数据库中获取数据,但是我收到错误未定义索引:页面,未定义索引:行,未定义索引:sidx,未定义索引:我的server.php文件中的sord。我在grid.html上获得了网格,但没有来自数据库的数据。
我有grid.html
...
<script type="text/javascript">
$(function () {
$("#list").jqGrid({
url: "server.php",
datatype: "xml",
mtype: "GET",
colNames: ["Zaposleni ID", "Ime", "Prezime", "Godine staza", "Mesecna plata", "Godisnja plata", "Broj odradjenih projekata", "Kod zaposlenog", "Odeljenje"],
colModel: [
{ name: "zap_id", width: 55 },
{ name: "ime", width: 90 },
{ name: "prezime", width: 90 },
{ name: "godine_staza", width: 80, align: "right" },
{ name: "mesecna_plata", width: 80, align: "right" },
{ name: "godisnja_plata", width: 80, align: "right" },
{ name: "broj_odradjenih_projekata", width: 50 },
{ name: "kod_zaposlenog", width: 50 },
{ name: "odeljenje", width: 150 }
],
autowidth: true,
pager: "#pager",
rowNum: 10,
rowList: [10, 20, 30],
sortname: "invid",
sortorder: "desc",
viewrecords: true,
gridview: true,
autoencode: true,
caption: "Zaposleni"
});
});
</script>
...
dbconfig.php
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$database="kompanija";
?>
server.php
<?php
//include the information needed for the connection to MySQL data base server.
// we store here username, database and password
include("dbconfig.php"); //
// to the url parameter are added 4 parameters as described in colModel
// we should get these parameters to construct the needed query
// Since we specify in the options of the grid that we will use a GET method
// we should use the appropriate command to obtain the parameters.
// In our case this is $_GET. If we specify that we want to use post
// we should use $_POST. Maybe the better way is to use $_REQUEST, which
// contain both the GET and POST variables. For more information refer to php documentation.
// Get the requested page. By default grid sets this to 1.
$page = $_GET['page'];
// get how many rows we want to have into the grid - rowNum parameter in the grid
$limit = $_GET['rows'];
// get index row - i.e. user click to sort. At first time sortname parameter -
// after that the index from colModel
$sidx = $_GET['sidx'];
// sorting order - at first time sortorder
$sord = $_GET['sord'];
// if we not pass at first time index use the first column for the index or what you want
if(!$sidx) $sidx =1;
// connect to the MySQL database server
$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());
// select the database
mysql_select_db($database) or die("Error connecting to db.");
// calculate the number of rows for the query. We need this for paging the result
$result = mysql_query("SELECT COUNT(*) AS count FROM zaposleni");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];
// calculate the total pages for the query
if( $count > 0 && $limit > 0) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
// if for some reasons the requested page is greater than the total
// set the requested page to total page
if ($page > $total_pages) $page=$total_pages;
// calculate the starting position of the rows
$start = $limit*$page - $limit;
// if for some reasons start position is negative set it to 0
// typical case is that the user type 0 for the requested page
if($start <0) $start = 0;
// the actual query for the grid data
$SQL = "SELECT zaposleni.zap_id, zaposleni.ime, zaposleni.prezime, zaposleni.godine_staza, zaposleni.mesecna_plata, zaposleni.godisnja_plata, zaposleni.broj_odradjenih_projekata, zaposleni.kod_zaposlenog, odeljenje.od_id FROM zaposleni INNER JOIN odeljenje ON zaposleni.od_id=odeljenje.od_id ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error());
// we should set the appropriate header information. Do not forget this.
header("Content-type: text/xml;charset=utf-8");
$s = "<?xml version='1.0' encoding='utf-8'?>";
$s .= "<rows>";
$s .= "<page>".$page."</page>";
$s .= "<total>".$total_pages."</total>";
$s .= "<records>".$count."</records>";
// be sure to put text data in CDATA
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$s .= "<row id='". $row['zap_id']."'>";
$s .= "<cell>". $row['zap_id']."</cell>";
$s .= "<cell>". $row['ime']."</cell>";
$s .= "<cell>". $row['prezime']."</cell>";
$s .= "<cell>". $row['godine_staza']."</cell>";
$s .= "<cell>". $row['mesecna_plata']."</cell>";
$s .= "<cell>". $row['godisnja_plata']."</cell>";
$s .= "<cell>". $row['broj_odradjenih_projekata']."</cell>";
$s .= "<cell>". $row['kod_zaposlenog']."</cell>";
$s .= "<cell>". $row['od_id']."</cell>";
$s .= "</row>";
}
$s .= "</rows>";
echo $s;
?>
是的,有人能帮帮我吗?我找不到问题。
答案 0 :(得分:0)
sortname:“invid”
你的网格colmodel中没有这样的列。
sortname:“zap_id
并为所有列设置索引,例如
{ name: "zap_id",index:"zap_id" , width: 55 }