我有一个jgrid设置并正确显示数据。我添加了一个filtertoolbox但是当我输入内容时,网格没有显示任何数据。如果我使用放大镜进行搜索,一切都按预期工作。
这是代码: 的index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>blablabla</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/jquery-ui-1.10.2.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
<?php
include("dbconfig.php");
?>
<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:'example.php',
datatype: 'json',
colNames:['Edit','ID','Alliance Name','Player', 'Level','Might','City 1','City 2','City 3','City 4','Notes'],
colModel :[
{name: 'myac', width:80, fixed:true, sortable:false, resize:false,search:false, formatter:'actions',
formatoptions:{keys:true}},
{name:'id', index:'id', width:30,searchoptions: { sopt: ['eq', 'cn']} },
{name:'alliance_name', index:'alliance_name', editable:true, sorttype: 'int' ,sortable:true, search:true,searchoptions: { sopt: ['eq', 'cn']}, stype:'text', width:110},
{name:'player_name', index:'player_name', editable:true, sortable:true,search:true,searchoptions: { sopt: ['eq', 'cn']}, stype:'text', width:80, align:'center'},
{name:'player_lvl', index:'player_lvl', editable:true, sortable:true, search:true,searchoptions: { sopt: ['eq', 'cn']}, stype:'text', width:80, align:'center'},
{name:'player_might', index:'player_might', editable:true, sortable:true,search:true, searchoptions: { sopt: ['eq', 'cn']},stype:'text', width:80, align:'center'},
{name:'city1_coords', index:'city1_coords', editable:true, width:80,search:true,searchoptions: { sopt: ['eq', 'cn']}, stype:'text' ,align:'center'},
{name:'city2_coords', index:'city2_coords',editable:true, width:80,search:true,searchoptions: { sopt: ['eq', 'cn']}, stype:'text', align:'center'},
{name:'city3_coords', index:'city3_coords',editable:true, width:80,search:true,searchoptions: { sopt: ['eq', 'cn']}, stype:'text', align:'center'},
{name:'city4_coords', index:'city4_coords',editable:true, width:80,search:true,searchoptions: { sopt: ['eq', 'cn']}, stype:'text', align:'center'},
{name:'notes', index:'notes', width:150, editable:true,search:true, searchoptions: { sopt: ['eq', 'cn']}, stype:'text',sortable:false}
],
pager: '#pager',
rowNum:30,
rowList:[10,20,30,40,50,60,70,100],
sortname: 'id',
mtype: "GET",
sortorder: 'asc',
viewrecords: true,
ignoreCase:true,
gridview: true,
subGrid : true,
height: "100%",
editurl: 'dummy.php',
autowidth: true,
subGridUrl: 'subgrid.php',
subgridtype: "json",
autoencode: true,
subGridModel: [{ name : ['Wild 1','Wild 2','Wild 3','Wild 4','Wild 5','Wild 6','Wild 7','Wild 8','Wild 9','Wild 10'],
width : [120,120,120,120,120,120,120,120,120,120]}
],
caption: 'Enemy Coordrinates',
repeatitems: false,
loadComplete: function(){ $('#list').setGridParam({datatype: 'json'});
jQuery("#list").trigger("reloadGrid");
}
});
jQuery("#list").showCol('subgrid');
jQuery("#list").jqGrid('navGrid',"#pager",{edit:false,add:false,del:false,search:true});
jQuery("#list").jqGrid('inlineNav',"#pager");
jQuery("#list").jqGrid('filterToolbar',{stringResult: false, searchOnEnter : false, defaultSearch: 'cn', ignoreCase: true});
});
</script>
</head>
<body>
<table id="list"><tr><td/></tr></table>
<div id="pager"></div>
</body>
</html>
使用example.php
<?php
//include the information needed for the connection to MySQL data base server.
// we store here username, database and password
include("dbconfig.php");
//ini_set("display_errors",1);
// 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 = $_REQUEST['page'];
$limit = $_REQUEST['rows'];
$sidx = $_REQUEST['sidx'];
$sord = $_REQUEST['sord'];
// if we not pass at first time index use the first column for the index or what you want
if(!$sidx) $sidx =1;
//array to translate the search type
$ops = array(
'eq'=>'=', //equal
'ne'=>'<>',//not equal
'lt'=>'<', //less than
'le'=>'<=',//less than or equal
'gt'=>'>', //greater than
'ge'=>'>=',//greater than or equal
'bw'=>'LIKE', //begins with
'bn'=>'NOT LIKE', //doesn't begin with
'in'=>'LIKE', //is in
'ni'=>'NOT LIKE', //is not in
'ew'=>'LIKE', //ends with
'en'=>'NOT LIKE', //doesn't end with
'cn'=>'LIKE', // contains
'nc'=>'NOT LIKE' //doesn't contain
);
function getWhereClause($col, $oper, $val){
global $ops;
if($oper == 'bw' || $oper == 'bn') $val .= '%';
if($oper == 'ew' || $oper == 'en' ) $val = '%'.$val;
if($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') $val = '%'.$val.'%';
return " WHERE $col {$ops[$oper]} '$val' ";
}
$where = ""; //if there is no search request sent by jqgrid, $where should be empty
$searchField = isset($_GET['searchField']) ? $_GET['searchField'] : false;
$searchOper = isset($_GET['searchOper']) ? $_GET['searchOper']: false;
$searchString = isset($_GET['searchString']) ? $_GET['searchString'] : false;
if ($_GET['_search'] == 'true') {
$where = getWhereClause($searchField,$searchOper,$searchString);
// var_dump($where);
}
$totalrows = isset($_REQUEST['totalrows']) ? $_REQUEST['totalrows']: false;
if($totalrows) {
$limit = $totalrows;
}
// 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.");
if ($limit<0) $limit = 0;
// calculate the number of rows for the query. We need this for paging the result
$result = mysql_query("SELECT COUNT(*) AS count FROM enemy_coords");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];
if( $count >0 ) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
if ($start<0) $start = 0;
// if for some reasons start position is negative set it to 0
// typical case is that the user type 0 for the requested page
// the actual query for the grid data
$SQL = "SELECT * FROM enemy_coords ".$where." ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error());
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$responce->rows[$i]['id']=$row[ID];
$responce->rows[$i]['cell']=array($row[ID],$row[ID],$row[alliance_name],$row[player_name],$row[player_lvl],$row[player_might],$row[city1_coords],$row[city2_coords],$row[city3_coords],$row[city4_coords],$row[notes]);
$i++;
}
echo json_encode($responce);
// var_dump($SQL);
?>
subgrid.php
<?php
include("dbconfig.php");
$id = $_GET['id'];
// connect to the database
$db = mysql_connect($dbhost, $dbuser, $dbpassword)
or die("Connection Error: " . mysql_error());
mysql_select_db($database) or die("Error conecting to db.");
$SQL = "SELECT wild1,wild2,wild3,wild4,wild5,wild6,wild7,wild8,wild9,wild10 FROM enemy_coords WHERE id=".$id."";
$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$responce->rows[$i]['id']=$row[num];
$responce->rows[$i]['cell']=array($row[wild1],$row[wild2],$row[wild3],$row[wild4],$row[wild5],$row[wild6],$row[wild7],$row[wild8],$row[wild9],$row[wild10],);
$i++;
}
echo json_encode($responce);
?>
我做错了什么?我不是一个PHP专家!谢谢!
答案 0 :(得分:1)
您使用stringResult: false
选项。它表示另一种格式数据,作为您当前使用的single field searching格式。您可以尝试使用Fiddler或Firebug或IE或Chrome的开发人员工具来捕获HTTP流量。您会看到,在stringResult: false
选项的情况下,jqGrid会以
key=value
key
是应用过滤器的列的index
或name
属性的值。例如,如果用户在notes
列之上的输入字段中键入文本“bla”而不是参数
notes=bla
将在网址中找到。您可以使用$_REQUEST['notes']
来获取它。
由于单字段搜索的数据格式与stringResult: false
的格式不同,我个人更喜欢使用与高级搜索格式相同的stringResult: true
格式,请参阅the documentation。