Javascript 如何让 jqGrid 工具栏搜索工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5941434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get jqGrid toolbar search working?
提问by Christos Hayward
At http://trirand.com/blog/jqgrid/jqgrid.html, under "New in Version 3.7" > "Column Search", there is a method explained to search, and it hasn't worked yet for me. I've added:
在http://trirand.com/blog/jqgrid/jqgrid.html 的“3.7 版新功能”>“列搜索”下,有一种搜索方法说明,但对我来说还没有奏效。我已经添加:
jQuery("#toolbar").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : false});
and less essential code from the example. My server saw slightly different JSON requests, but no _search=true
and no search term, ever.
以及示例中不太重要的代码。我的服务器看到略有不同的 JSON 请求,但_search=true
从来没有,也没有搜索词。
http://trirand.com/blog/jqgrid/jqgrid.htmlalso gives an incomplete example of server-side code. The SQL statement is given in the example PHP:
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";
but, while $sidx
, $sord
, $start
, and $limit
all have code to define them, $where
is not defined (or referenced) anywhere else on the page.
但是, while $sidx
、$sord
、$start
、 和$limit
所有代码都有定义它们的代码,但$where
没有在页面上的其他任何地方定义(或引用)。
How can I get a column search like the page documents, where my server is being hit by the appropriate requests?
如何获得像页面文档这样的列搜索,其中我的服务器被适当的请求击中?
回答by Oleg
The filterToolbarmethod should be called on the same element which you use to define the grid. Look at the working exampleused it.
该filterToolbar方法应您可以使用它定义网格在相同的元素被调用。看看使用它的工作示例。
I can't help you with the PHP part of your question, because I don't use PHP myself. Nevertheless the demo filesfrom the jqGrid download pageseems to contain some PHP code examples which could be helpful for you.
我无法帮助您解决问题的 PHP 部分,因为我自己不使用 PHP。尽管如此,演示文件从该jqGrid的下载页面似乎包含一些PHP代码示例这可能对你有所帮助。
回答by Serhij Blotskyy
Thanks to the previous author for the starting point for the problem solution. Here is ready to use piece of the server-side PHP
code implementing search request (from jqGrid
) processing:
感谢之前的作者提供了解决问题的起点。这是准备使用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());
回答by d0rc
You may want to try this code for simplest case:
对于最简单的情况,您可能想尝试使用此代码:
$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 = "";
}
}
Before using in production make sure you are handling cases when $_GET['filters'] contains garbage instead of json, and field names/values are properly escaped. Otherwise there are plenty of space for SLQ injections.
在生产中使用之前,请确保您正在处理 $_GET['filters'] 包含垃圾而不是 json 的情况,并且字段名称/值已正确转义。否则有足够的空间用于 SLQ 注射。
回答by Robert Imhoff
Thanks for posting your code!
感谢您发布您的代码!
The only change was that I had to unescape double-quotes in the 'filters' parameter to get it working:
唯一的变化是我必须在 'filters' 参数中取消双引号以使其正常工作:
$filters = str_replace('\"','"' ,$_POST['filters']);
$filters = str_replace('\"','"' ,$_POST['filters']);