我是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)并单击FIND,没有任何反应。搜索框仍然存在且列表尚未过滤。
我一整天都没有成功,所以任何帮助都会非常感激!
我正在使用jquery v1.4.4和jqgrid v3.8.2
非常感谢所有人
的Si
答案 0 :(得分:1)
取决于您使用哪种searching服务器接收不同的附加参数。
如果是single field searching,则会searchField
,searchString
和searchOper
参数。如果是advance searching,则其中一个参数filters
在the JSON encoded form中有关于搜索请求的所有参数的信息。对于toolbar searching和其他参数stringResult:true
,参数的格式与advance searching的格式相同。
因此,您只需将代码附加到搜索请求的参数分析中即可。您可以从the demo files下载the jqGrid download page并查看search_adv.php文件的代码。