jqgrid工具栏搜索没有获取mysql的过滤器

时间:2015-02-25 22:34:33

标签: php jquery json search jqgrid

之前可能已经回答过但我没有找到适合我的东西。

我正在尝试使用php / json数据为jqgrid实现工具栏搜索选项。

到目前为止,我有以下

<script src="js/jquery-1.11.0.min.js"></script>
<script type='text/javascript' src='js/jquery-ui-1.8.4.custom.min.js'></script>        
<script type='text/javascript' src='js/i18n/grid.locale-es.js'></script>
<script type='text/javascript' src='js/jquery.jqGrid.min.js'></script>

<script>
$(document).ready(function () {
$("#list_records").jqGrid({
url: "grid.php",
datatype: "json",
mtype: "GET",
colNames: ["ID", "Encuesta", "Fecha", "Quien"],
colModel: [
{ name: "id_consulta"},
{ name: "texto"},
{ name: "fecha"},
{ name: "quien"}
],
pager: "#perpage",
rowNum: 10,
rowList: [10,20],
sortname: "id_consulta",
sortorder: "asc",
height: 'auto',
viewrecords: true,
gridview: true,
caption: ""
});

$('#list_records').jqGrid('filterToolbar', {stringResult: true, searchOnEnter: false, defaultSearch : "bw"});

});
</script>

我设置了stringresult:true并尝试获取过滤器。

相关的php文件是

$db = mysql_connect($server,$user,$pass);
mysql_select_db($dbname,$db);

    $page = $_REQUEST['page']; // get the requested page
    $limit = $_REQUEST['rows']; // get how many rows we want to have into the grid
    $sidx = $_REQUEST['sidx']; // get index row - i.e. user click to sort
    $sord = $_REQUEST['sord']; // get the direction
    if(!$sidx) $sidx =1;


$filterResultsJSON = json_decode($_REQUEST['filters']);

$filterArray = get_object_vars($filterResultsJSON);

$counter = 0;
$sql='';
while($counter < count($filterArray['rules']))
{
$filterRules = get_object_vars($filterArray['rules'][$counter]);

if($counter == 0){
$sql .= ' WHERE ' . $filterRules['field'] . ' LIKE "%' . $filterRules['data'] . '%"';
}
else {
$sql .= ' AND ' . $filterRules['field'] . ' LIKE "%' . $filterRules['data'] . '%"';
}
$counter++;
}

$totalrows = isset($_REQUEST['totalrows']) ? $_REQUEST['totalrows']: false;
if($totalrows) {$limit = $totalrows;}

$result = mysql_query("SELECT COUNT(*) AS count FROM consulta $sql"); 
$row = mysql_fetch_array($result,MYSQL_ASSOC); 

$count = $row['count']; 
if( $count > 0 && $limit > 0) { 
$total_pages = ceil($count/$limit); 
} else { 
$total_pages = 0; 
} 
if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit;
if($start <0) $start = 0; 

$SQL = "SELECT * FROM consulta $sql ORDER BY $sidx $sord LIMIT $start , $limit"; 
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error()); 

$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$responce->rows[$i]['id']=$row['id_consulta'];
$responce->rows[$i]['cell']=array($row['id_consulta'],utf8_encode($row['texto']),$row['fecha'],$row['quien']);
$i++;
}

echo json_encode($responce);

我直接调用php时收到以下错误消息 警告:get_object_vars()期望参数1为object,在第16行的/home/www2w/public_html/tuvoto/grid.php中给出null 无法执行查询。您的SQL语法中有错误;查看与您的MySQL服务器版本相对应的手册,以获得在&#39;&#39;附近使用的正确语法。在第1行

任何帮助apreciated

2 个答案:

答案 0 :(得分:0)

要使jqgrid工具栏搜索过滤器使用php提供的数据,您必须捕获过滤器和规则并构建WHERE子句。

可以通过以下方式完成。

$filterResultsJSON = json_decode($_REQUEST['filters']);
$counter = 0;
$sql='';
if($filterResultsJSON != "") {

$filterArray = get_object_vars($filterResultsJSON);

while($counter < count($filterArray['rules']))
{
$filterRules = get_object_vars($filterArray['rules'][$counter]);

if($counter == 0){
$sql .= ' WHERE ' . $filterRules['field'] . ' LIKE "%' . $filterRules['data'] . '%"';
}
else {
$sql .= ' AND ' . $filterRules['field'] . ' LIKE "%' . $filterRules['data'] . '%"';
}
$counter++;
}

}

感谢您的评论。

答案 1 :(得分:0)

在第一次加载网格时你没有搜索任何东西.. 所以过滤器设置为null .. 在get_object_vars()中,我们无法传递空值。所以把条件放在那里,

if(filterResultsJSON!="")
{
$filterArray = get_object_vars($filterResultsJSON);
}