在http://trirand.com/blog/jqgrid/jqgrid.html,“版本3.7中的新功能”> “列搜索”,有一种方法被解释为搜索,但它还没有为我工作。我添加了:
jQuery("#toolbar").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : false});
来自示例的不太重要的代码。我的服务器看到了稍微不同的JSON请求,但没有_search=true
,也没有搜索词。
http://trirand.com/blog/jqgrid/jqgrid.html也提供了服务器端代码的不完整示例。 SQL语句在PHP示例中给出:
$SQL = "SELECT item_id, item, item_cd FROM items ".$where." ORDER BY $sidx $sord LIMIT $start , $limit";
但是,虽然$sidx
,$sord
,$start
和$limit
都有代码来定义它们,但$where
未在任何地方定义(或引用)或者在页面上。
如何获取列表搜索,例如页面文档,我的服务器受到相应请求的影响?
答案 0 :(得分:8)
应该在用于定义网格的同一元素上调用filterToolbar方法。看the working example使用它。
我无法帮助您处理问题的PHP部分,因为我自己不使用PHP。然而,来自demo files的the jqGrid download page似乎包含一些可能对您有帮助的PHP代码示例。
答案 1 :(得分:3)
感谢前一位作者提出问题解决方案的起点。这里准备使用server-side PHP
代码实现搜索请求(来自jqGrid
)处理:
$filters = $_POST['filters'];
$search = $_POST['_search'];
$where = "";
if(($search==true) &&($filters != "")) {
$filters = json_decode($filters);
$where = " where ";
$whereArray = array();
$rules = $filters->rules;
$groupOperation = $filters->groupOp;
foreach($rules as $rule) {
$fieldName = $rule->field;
$fieldData = mysql_real_escape_string($rule->data);
switch ($rule->op) {
case "eq":
$fieldOperation = " = '".$fieldData."'";
break;
case "ne":
$fieldOperation = " != '".$fieldData."'";
break;
case "lt":
$fieldOperation = " < '".$fieldData."'";
break;
case "gt":
$fieldOperation = " > '".$fieldData."'";
break;
case "le":
$fieldOperation = " <= '".$fieldData."'";
break;
case "ge":
$fieldOperation = " >= '".$fieldData."'";
break;
case "nu":
$fieldOperation = " = ''";
break;
case "nn":
$fieldOperation = " != ''";
break;
case "in":
$fieldOperation = " IN (".$fieldData.")";
break;
case "ni":
$fieldOperation = " NOT IN '".$fieldData."'";
break;
case "bw":
$fieldOperation = " LIKE '".$fieldData."%'";
break;
case "bn":
$fieldOperation = " NOT LIKE '".$fieldData."%'";
break;
case "ew":
$fieldOperation = " LIKE '%".$fieldData."'";
break;
case "en":
$fieldOperation = " NOT LIKE '%".$fieldData."'";
break;
case "cn":
$fieldOperation = " LIKE '%".$fieldData."%'";
break;
case "nc":
$fieldOperation = " NOT LIKE '%".$fieldData."%'";
break;
default:
$fieldOperation = "";
break;
}
if($fieldOperation != "") $whereArray[] = $fieldName.$fieldOperation;
}
if (count($whereArray)>0) {
$where .= join(" ".$groupOperation." ", $whereArray);
} else {
$where = "";
}
}
// evaluating $sidx, $sord, $start, $limit
$SQL = "SELECT id, brandName, name, description FROM products".$where." ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error());
答案 2 :(得分:2)
您可能希望在最简单的情况下尝试此代码:
$filters = $_GET['filters'];
$where = "";
if (isset($filters)) {
$filters = json_decode($filters);
$where = " where ";
$whereArray = array();
$rules = $filters->rules;
foreach($rules as $rule) {
$whereArray[] = $rule->field." like '%".$rule->data."%'";
}
if (count($whereArray)>0) {
$where .= join(" and ", $whereArray);
} else {
$where = "";
}
}
在生产中使用之前,请确保在$ _GET ['filters']包含垃圾而不是json时处理案例,并且正确转义字段名称/值。否则SLQ注射有足够的空间。
答案 3 :(得分:0)
感谢发布您的代码!
唯一的变化是我必须在'filters'参数中使用双引号才能使其正常工作:
$ filters = str_replace('\“','”',$ _ POST ['filters']);