2013-05-08 94 views
0

我第一次使用jqgrid插件。除了網格從這裏開始載入數據之外,一切都很好:http://imageshack.us/photo/my-images/16/capturadepantalla201305r.png「grid」。數據從數據庫中獲取,但只有當我點擊任何標題/分類器時,它纔會加載所有數據。我怎樣才能修復它從一開始就加載?我是一個總noob,並希望任何幫助亞勒。
這裏是我的代碼:jqgrid not在開始時加載。如何在網格加載時加載數據?

我的PHP負荷:

// 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 demo"); 
$row = mysql_fetch_array($result,MYSQL_ASSOC); 
$count = $row['count']; 

// calculate the total pages for the query 
if($count > 0 && $limit >5) { 
       $total_pages = ceil($count/$limit); 
} else { 
       $total_pages = 5; 
} 

// 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 id, name, id_continent, lastvisit,cdate, ddate,email FROM demo 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['id']."'>"; 
     $s .= "<cell>". $row['id']."</cell>"; 
     $s .= "<cell><![CDATA[". $row['name']."]]></cell>"; 
    $s .= "<cell><![CDATA[". $row['id_continent']."]]></cell>"; 
    $s .= "<cell>". $row['lastvisit']."</cell>"; 
    $s .= "<cell>". $row['cdate']."</cell>"; 
    $s .= "<cell>". $row['ddate']."</cell>"; 
    $s .= "<cell><![CDATA[". $row['email']."]]></cell>"; 
    $s .= "</row>"; 
} 
$s .= "</rows>"; 

echo $s; 
?> 

HTML:

<link rel="stylesheet" type="text/css" media="screen" href="css/jquery-ui-1.10.3.custom.min.css" /> 
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" /> 
<link rel="stylesheet" type="text/css" media="screen" href="css/nhf.css" /> 


<style type="text/css"> 
html, body { 
    margin: 0; 
    padding: 0; 
    font-size: 90%; 
} 
</style> 

<script src="js/jquery-1.9.0.min.js" type="text/javascript"></script> 
<script src="js/i18n/grid.locale-en.js" type="text/javascript"></script> 
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script> 

<script type="text/javascript"> 
$(function(){ 
    $("#list").jqGrid({ 
    url:'request.php', 
    datatype: 'xml', 
    mtype: 'GET', 
    height: 'auto', 
    colNames:['id','Project', 'Assigned To','Assign Date','Check Date','Due Date','Attachments'], 
    colModel :[ 
     {name:'id', index:'id', width:20}, 
     {name:'name', index:'name', width:200, align:'left'}, 
     {name:'id_continent', index:'id_continent', width:80, align:'right'}, 
     {name:'lastvisit', index:'lastvisit', width:70, align:'right'}, 
     {name:'cdate', index:'cdate', width:70, align:'right'}, 
     {name:'ddate', index:'ddate', width:70, align:'right',datefmt:'(mm/d/YY)',date:'true'}, 
     {name:'email', index:'email', width:70,align:'center',sortable:false} 
    ], 
    pager: '#pager', 
    rowNum:10, 
    rowList:[10,20,30], 
    sortname: 'invid', 
    sortorder: 'desc', 
    viewrecords: true, 
    gridview: true, 
    caption: 'Pending Assignements' 
    }); 
}); 
</script> 

</head> 
+0

我明白了。我的排序名稱錯誤。 sortname:'invid',它應該是「id」。謝謝 – NewHistoricForm 2013-05-08 20:46:58

+1

添加此作爲答案和馬克作爲解決問題的答案 – Mark 2013-05-09 12:47:46

回答

0

我知道了。我的排序名稱錯誤。 sortname:'invid',它應該是「id」。謝謝