2011-11-04 117 views
0


我已经创建了一个简单的网格,通过php从服务器检索数据。 MYSQL数据库中的行数为9.
我决定将rowNum选项从rowNum:10更改为rowNum:7,看看会发生什么。正如我预料的那样,网格上出现了7行。问题是我看不到其余的2.寻呼机栏没有第二页(它说的第1页,共1页)。
然后我添加recordtext选项并将其设置为此记录文本:{{}}的{0} - {1}。 刷新页面,并在网格的右下角这个文本apeard“1 - 7 of 9”。这意味着从服务器返回的所有数据,但分页有些不好。jqgrid分页栏不显示所有页面

让我发布代码。

的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

所以。这个错误是由于一个不好的编码。

正如你可以看到在php文件的顶部我有这个
$ page = 1; // $ _GET ['page']; //获取请求的页面
$ limit = 9; // $ _ GET [ '行']; //得到我们想要多少行有入电网

虽然HTML文件发送的rowNum:这个数字达到9 php的7,这就是为什么有人说第1页1

要解决我只需擦除注释并使用$ _GET方法的问题。

另外我改变奥列格说。

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值应为$count <= $limit是1和$count = $limit + 1为2。

此外,您忘记在mygrid = $("#list");之前使用var