2011-11-04 111 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