我创建了一个简单的网格,通过php从服务器检索数据。 MYSQL数据库中的行数为9。
我决定将rowNum选项从rowNum:10更改为rowNum:7,看看会发生什么。正如我预计网格上出现了7行。问题是我看不到其余的2.寻呼机栏没有第二页(它说的第1页)。
然后我添加了recordtext选项并将其设置为此记录文本:“{0} - {1}的{1}”。
刷新页面并在网格的右下角这个文本apeard“1 - 7 of 9”。这意味着从服务器返回的所有数据都会出现分页错误。
让我发布代码。
的index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My First Grid</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/smoothness/jquery-ui-1.8.16.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
<script src="js/jquery-1.5.2.min.js" type="text/javascript"></script>
<script src="js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.jqGrid.min.js"></script>
<style>
html, body {
margin: 0;
padding: 0;
font-size: 75%;
}
</style>
<script type="text/javascript">
$(function(){
mygrid = $("#list");
mygrid.jqGrid({
url:'example1.php',
datatype: 'xml',
mtype: 'GET',
colNames:['Inv No','Date', 'Amount','Tax','Total','Notes'],
colModel :[
{name:'invid', index:'invid', width:55},
{name:'invdate', index:'invdate', width:90},
{name:'amount', index:'amount', width:80, align:'right', search:true , stype:'select', searchoptions:{value:':All;8:8.00;6:6.00'}},
{name:'tax', index:'tax', width:80, align:'right'},
{name:'total', index:'total', width:80, align:'right', sortable:true},
{name:'note', index:'note', width:150, search:true , align:'center'}
],
pager: '#pager',
emptyrecords: "Nothing to display",
recordtext: '{0} - {1} of {2}',
rowNum:7,
rowList:[7,9,11],
viewrecords: true,
caption: 'My first grid'
});
//Search button
$("#bsdata").click(function(){ mygrid.jqGrid('searchGrid', {sopt:['eq'],top:300,caption:"test searching"} ); });
// Search toolbar.
mygrid.jqGrid('filterToolbar', {stringResult: true, searchOnEnter: false, defaultSearch : "eq"});
//NavBar
mygrid.jqGrid('navGrid','#pager',{edit:false,add:false,del:false});
});
</script>
</head>
<body>
<table id="list"><tr><td/></tr></table>
<div id="pager"></div>
<input type="BUTTON" id="bsdata" value="Search" />
</body>
</html>
和example1.php
<?php
$page = 1; // $_GET['page']; // get the requested page
$limit = 9; //$_GET['rows']; // get how many rows we want to have into the grid
$sidx = 'invid';//$_GET['sidx']; // get index row - i.e. user click to sort
$sord = 'invid';//$_GET['sord']; // get the direction
if(!$sidx) $sidx =1;
//array to translate the search type
$ops = array(
'eq'=>'=', //equal
'ne'=>'<>',//not equal
'lt'=>'<', //less than
'le'=>'<=',//less than or equal
'gt'=>'>', //greater than
'ge'=>'>=',//greater than or equal
'bw'=>'LIKE', //begins with
'bn'=>'NOT LIKE', //doesn't begin with
'in'=>'LIKE', //is in
'ni'=>'NOT LIKE', //is not in
'ew'=>'LIKE', //ends with
'en'=>'NOT LIKE', //doesn't end with
'cn'=>'LIKE', // contains
'nc'=>'NOT LIKE' //doesn't contain
);
function getWhereClause($col, $oper, $val){
global $ops;
if($oper == 'bw' || $oper == 'bn') $val .= '%';
if($oper == 'ew' || $oper == 'en' ) $val = '%'.$val;
if($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') $val = '%'.$val.'%';
return " WHERE $col {$ops[$oper]} '$val' ";
}
$where = ""; //if there is no search request sent by jqgrid, $where should be empty
$searchField = isset($_GET['searchField']) ? $_GET['searchField'] : false;
$searchOper = isset($_GET['searchOper']) ? $_GET['searchOper']: false;
$searchString = isset($_GET['searchString']) ? $_GET['searchString'] : false;
if ($_GET['_search'] == 'true') {
$where = getWhereClause($searchField,$searchOper,$searchString);
}
// connect to the database
$dbhost = "localhost";
$dbuser = "user";
$dbpassword = "user123";
$database = "test";
$tablename = "invheader";
$db = mysql_connect($dbhost, $dbuser, $dbpassword)
or die("Connection Error: " . mysql_error());
mysql_select_db($database) or die("Error conecting to db.");
//mysql_set_charset('utf8',$database);
mysql_query("SET NAMES 'utf8'");
$result = mysql_query("SELECT COUNT(*) AS count FROM $tablename");
$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)
$SQL = "SELECT invid, invdate, amount, tax, total, note FROM $tablename ".$where." ORDER BY $sidx, $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldn?t execute query.".mysql_error());
if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
header("Content-type: application/xhtml+xml;charset=utf-8"); } else {
header("Content-type: text/xml;charset=utf-8");
}
$et = ">";
echo "<?xml version='1.0' encoding='utf-8'?$et\n";
echo "<rows>";
echo "<page>".$page."</page>";
echo "<total>".$total_pages."</total>";
echo "<records>".$count."</records>";
// be sure to put text data in CDATA
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
echo "<row id='". $row['invid']."'>";
echo "<cell>". $row['invid']."</cell>";
echo "<cell>". $row['invdate']."</cell>";
echo "<cell>". $row['amount']."</cell>";
echo "<cell>". $row['tax']."</cell>";
echo "<cell>". $row['total']."</cell>";
echo "<cell><![CDATA[". $row['note']."]]></cell>";
echo "</row>";
}
echo "</rows>";
?>
另外尝试使用this方法,但也没有任何改变。 任何的想法 ?你能给我一些提示吗?
提前致谢。
答案 0 :(得分:1)
我认为您的服务器代码放置了total
页面的错误值。如果rowNum: 7
并且数据库中有9个项目,则服务器响应应为:<page>1</page>
,<total>2<total>
和<records>2<records>
。所以页面总数应该是2而不是1。
问题似乎是代码如何计算$total_pages
。你用
$total_pages = ceil($count/$limit);
我建议你把它改成
$total_pages = floor(($count + $limit - 1)/$limit);
$total_pages
的{{1}}值应为1,$count <= $limit
的值为2。
此外,您忘记在$count = $limit + 1
之前使用var
。
答案 1 :(得分:0)
因此。这个错误是由于一个noobish编码。
正如你在php文件的顶部所见,我有这个
$ page = 1; // $ _GET ['page']; //获取请求的页面
$ limit = 9; // $ _ GET [ '行']; //获取我们想要进入网格的行数
当html文件在php中发送rowNum:7时,这个数字变为9,这就是为什么它说1页的第1页。
要解决这个问题,我只需要删除注释并使用$ _GET方法。
我也改变了奥列格所说的话。