【问题标题】:Problem using jqGrid Search Function使用 jqGrid 搜索功能的问题
【发布时间】:2011-02-09 15:48:14
【问题描述】:

我是 jqgrid 的新手,但我真的很想掌握它。我很难让搜索功能正常工作。这是我正在使用的 XML:

<?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)并单击“查找”,没有任何反应。搜索框仍然存在,列表未被过滤。

我一整天都没有成功,所以任何帮助都将非常感激!

我正在使用 jquery v1.4.4 和 jqgrid v3.8.2

谢谢大家

【问题讨论】:

  • jqgrid 会给php server 三个参数searchField、searchString 和searchOper 单条搜索SQL 是什么样子的?如何使用这三个参数?你能给我一个示例代码吗?更何况,之后php服务器会回馈给jqgrid什么?谢谢!

标签: php javascript jquery jqgrid


【解决方案1】:

取决于你使用哪种searching服务器接收不同的附加参数。

如果是single field searching,它将是searchFieldsearchStringsearchOper 参数。在advance searching 的情况下,一个参数filtersthe JSON encoded form 中包含有关搜索请求的所有参数的信息。在toolbar searching 和附加参数stringResult:true 的情况下,参数格式与advance searching 的情况相同。

所以你应该在你的代码中附加搜索请求参数的分析。您可以从the jqGrid download page 下载the demo files,例如查看search_adv.php 文件的代码。

【讨论】:

  • 我现在感觉有点厚。我查看了使用 XML 数据和 PHP 脚本可以找到的所有示例,除了默认 URL 参数(页面、行、sidx、sord)之外,我看不到它们在任何地方处理任何内容。你能指出一个我可以参考的具体例子吗?
  • @Simon S:在来自trirand.com/blog/jqgrid/downloads/jqgrid_demo38.zip 的search_adv.php 文件中,您将看到带有$_REQUEST['_search']$_REQUEST['filters'] 的行用作高级搜索的参数。然后会调用函数constructWhere
  • 非常感谢。您指出我下载的示例似乎与 Triand 网站上演示页面中的示例完全不同。对我有很大帮助的另一件事是在 suddendevelopment.com/?p=38 找到的出色的 jqGrid CRUD 模板