2011-11-04 3 views
0


PHP를 통해 서버에서 데이터를 검색하는 간단한 그리드를 만들었습니다. MYSQL 데이터베이스의 행 수는 9입니다.
rowNum 옵션을 rowNum : 10에서 rowNum : 7로 변경하고 어떤 일이 발생하는지 확인합니다. 내가 그리드에 7 개의 행이 나타날 것으로 예상했기 때문입니다. 문제는 내가 나머지를 볼 수 없다는 것입니다. 2. 페이저 바에 두 번째 페이지가 없습니다 (1 페이지 중 1 페이지).
그런 다음 recordtext 옵션을 추가하고이 레코드 텍스트에 '{0} - {1} of {2}'을 설정하십시오. 페이지를 새로 고치고 표의 오른쪽 아래 모서리에이 텍스트 apeard "1 - 7 of 9"를 입력하십시오. 이는 모든 데이터가 서버에서 반환되었지만 페이지 매김으로 인해 문제가 있음을 의미합니다.jqgrid 페이지 넘김 모음이 모든 페이지를 표시하지 않습니다.

코드를 게시 해주세요.

<?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>"; 
?> 

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은 또한 this 방법뿐만 아니라, 아무것도 변화하지 사용하려고. 아이디어가 있으십니까? 힌트 좀 줄 수있어?

미리 감사드립니다.

답변

0

So. 실수는 멍청한 코딩 때문이었습니다.

PHP 파일의 상단에서 볼 수 있듯이 내가 가지고있다.
$ page = 1; // $ _GET [ 'page']; // 요청한 페이지를 가져옵니다.
$ limit = 9; // $ _ GET [ 'rows']; // 우리는 html 파일이 ROWNUM 보내는 동안 그리드

에 갖고 싶어 얼마나 많은 행 수 :이 숫자 9가 된 PHP 7을하고 1.

의 1 페이지를 말한 이유입니다 코멘트를 지우고 $ _GET 메소드를 사용해야하는 문제를 해결하십시오.

또한 Oleg의 말을 바꿉니다.

1

서버 코드가 total 페이지의 값을 잘못 입력했다고 가정합니다. rowNum: 7이고 데이터베이스에 9 개의 항목이있는 경우 서버 응답은 <page>1</page>, <total>2<total><records>2<records>이어야합니다. 그래서 총 페이지 수는 1이 아니라 2가되어야합니다.

$total_pages을 계산하는 방법에 문제가있는 것 같습니다. 당신은 $total_pages의 값은 $count <= $limit 1하고 $count = $limit + 1 2해야 당신이

$total_pages = floor(($count + $limit - 1)/$limit); 

로 변경하는 것이 좋습니다

$total_pages = ceil($count/$limit); 

를 사용합니다.

또한 var을 사용하기 전에 mygrid = $("#list");을 사용하는 것을 잊지 마십시오.

관련 문제