2011-02-09 120 views
1

我是jqgrid的新手,但真的很想掌握它。我很難讓搜索功能正常工作。這裏是我正在使用的XML:使用jqGrid的問題搜索功能

<?xml version='1.0' encoding='utf-8'?> 
<rows> 
    <page>1</page> 
    <total>1</total> 
    <records>3</records> 
     <row id='1'> 
      <cell>1</cell> 
      <cell>5 Little Roodee</cell> 
      <cell>Hawarden</cell><cell>CH5 3PU</cell> 
      <cell>895.00</cell> 
      <cell>1</cell> 
     </row> 
     <row id='2'> 
      <cell>2</cell> 
      <cell>28 Pant-y-Fawnog</cell> 
      <cell>Buckley</cell> 
      <cell>CH7 2PD</cell> 
      <cell>610.00</cell> 
      <cell>0</cell> 
     </row> 
     <row id='3'> 
      <cell>3</cell> 
      <cell>60 Langford Crescent</cell> 
      <cell>Buckley</cell> 
      <cell>CH7 2PR</cell> 
      <cell>625.00</cell> 
      <cell>1</cell> 
     </row> 
</rows> 

正如你可以看到它的相當簡單。現在,這裏是我使用的jqGrid聲明:

$(function(){ 
      $("#list").jqGrid({ 
      url:'xml_properties_jq.php', 
      datatype: 'xml', 
      mtype: 'GET', 
      colNames:['ID','Address', 'Town','Postcode','Rent. Val','Active'], 
      colModel :[ 
       {name:'property_id', index:'property_id', width:40}, 
       {name:'property_add_1', index:'property_add_1', width:150}, 
       {name:'property_add_town', index:'property_add_town', width:80, align:'right', searchoptions: { sopt: ['eq', 'ne']}}, 
       {name:'property_add_postcode', index:'property_add_postcode', width:80, align:'right'}, 
       {name:'property_rentable_value', index:'property_rentable_value', width:80, align:'right'}, 
       {name:'property_active', index:'property_active', width:60} 
      ], 
      pager: '#pager', 
      rowNum:10, 
      rowList:[10,20,30], 
      sortname: 'property_id', 
      sortorder: 'desc', 
      viewrecords: true, 
      caption: 'Properties' 
      }) 
      //jQuery("#list").jqGrid('navGrid','#pager',{edit:false,add:false,del:false}); 
      .navGrid('#pager',{view:true, del:false}, 
{}, // use default settings for edit 
{}, // use default settings for add 
{}, // delete instead that del:false we need this 
{multipleSearch : false}, // enable the advanced searching 
{closeOnEscape:true} /* allow the view dialog to be closed when user press ESC key*/ 
); 
}); 

最後這裏是一個構造我的XML的PHP​​:

<?php 
//include the information needed for the connection to MySQL data base server. 
// we store here username, database and password 
include("Connections/db.php"); 

// to the url parameter are added 4 parameters as described in colModel 
// we should get these parameters to construct the needed query 
// Since we specify in the options of the grid that we will use a GET method 
// we should use the appropriate command to obtain the parameters. 
// In our case this is $_GET. If we specify that we want to use post 
// we should use $_POST. Maybe the better way is to use $_REQUEST, which 
// contain both the GET and POST variables. For more information refer to php documentation. 
// 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($db) or die("Error connecting to db."); 
mysql_select_db($database_eazylet, $eazylet); 

// calculate the number of rows for the query. We need this for paging the result 
$result = mysql_query("SELECT COUNT(*) AS count FROM p_properties", $eazylet); 
//$row = mysql_fetch_array($result,MYSQL_ASSOC); 
$row = mysql_fetch_assoc($result); 
$count = $row['count']; 

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

// 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 * FROM p_properties ORDER BY $sidx $sord LIMIT $start , $limit"; 
$result = mysql_query($SQL, $eazylet) 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)) { 
//while($row = mysql_fetch_assoc($result)) { 
    $s .= "<row id='". $row['property_id']."'>";    
    $s .= "<cell>". $row['property_id']."</cell>"; 
    $s .= "<cell>". $row['property_add_1']."</cell>"; 
    $s .= "<cell>". $row['property_add_town']."</cell>"; 
    $s .= "<cell>". $row['property_add_postcode']."</cell>"; 
    $s .= "<cell>". $row['property_rentable_value']."</cell>"; 
    $s .= "<cell>". $row['property_active']."</cell>"; 
    $s .= "</row>"; 
} 
$s .= "</rows>"; 

echo $s; 
?> 

所有的排序工作在網格精細,所以我假定數據確定。

我的問題是,當我點擊搜索按鈕時,輸入任何有效的標準(即ID = 1),然後單擊FIND,沒有任何反應。搜索框將保留,列表尚未過濾。

我一直在這一整天沒有成功的工作,所以任何幫助將非常感激地收到!

我使用jQuery V1.4.4和的jqGrid v3.8.2

非常感謝所有

+0

jqgrid會給php服務器三個參數searchField,searchString和searchOper Whar單個搜索SQL的樣子?如何使用這三個參數?你能給我一個示例代碼嗎?更重要的是,在那之後,php服務器將返回給jqgrid?謝謝! – 2011-11-08 16:12:34

回答

1

依靠哪種searching您所使用的服務器接收不同的附加參數。

single field searching的情況下,將是searchField,searchStringsearchOper參數。在advance searching的情況下,有一個參數filters,它在the JSON encoded form中有關於搜索請求的所有參數的信息。在toolbar searching和附加參數stringResult:true的情況下,參數的格式與advance searching的情況相同。

因此,您應該將您的代碼附加到搜索請求參數的分析中。例如,您可以從the jqGrid download page下載the demo files並檢查search_adv.php文件的代碼。

+0

現在我感覺有點厚了。我查看了使用XML數據和PHP腳本可以找到的所有示例,並且除了默認URL參數(頁面,行,sidx,sord)之外,我無法看到它們正在處理的任何地方。你能指出我可以用作參考的具體例子嗎? – 2011-02-09 16:28:18