duanluan8390 2011-02-09 15:48
浏览 120
已采纳

使用jqGrid搜索功能时出现问题

I am new to jqgrid, but really want to get to grips with it. I am having a hard time getting the Search function to work. Here is the XML I am working with:

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

As you can see its fairly simple. Now here is the jqgrid declaration I am using:

$(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*/
);
}); 

Finally here is the PHP which constructs my XML:

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

All the sorting works fine in the grid so I assume the data is OK.

My problem is that when I hit the search button, enter any valid criteria (i.e. ID = 1) and click FIND, nothing happens. The search box remains and the list has not been filtered.

I have been working on this all day without success so any help would be VERY gratefully received!!

I am using jquery v1.4.4 and jqgrid v3.8.2

Many thanks all

Si

  • 写回答

1条回答 默认 最新

  • duanqun7761 2011-02-09 16:08
    关注

    Depend on which kind of searching you use the server receive different additional parameters.

    In case of single field searching it will be searchField, searchString and searchOper parameter. In case of advance searching one has one parameter filters which has in the JSON encoded form the information about all parameters of the search request. In case of the toolbar searching and additional parameter stringResult:true the format of parameters are the same as in case of advance searching.

    So you should just append your code with the analyse of the parameters of searching request. You can download the demo files from the jqGrid download page and examine the code of search_adv.php file for example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛