"Search for columns" there i...">

How can I find jqGrid toolbar work?

In http://trirand.com/blog/jqgrid/jqgrid.html in the section "New in version 3.7"> "Search for columns" there is a method that is explained for searching, and it does not work for me yet. I added:

jQuery("#toolbar").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : false}); 

and less important code from the example. My server saw slightly different JSON requests, but not _search=true and not a single search request.

http://trirand.com/blog/jqgrid/jqgrid.html also gives an incomplete server-side code sample. The SQL statement is provided in a PHP example:

 $SQL = "SELECT item_id, item, item_cd FROM items ".$where." ORDER BY $sidx $sord LIMIT $start , $limit"; 

but $sidx , $sord , $start and $limit all have code to determine if $where not defined (or not mentioned) anywhere on the page.

How can I get a column search, for example, documents on a page where relevant requests get to my server?

+4
source share
4 answers

The filterToolbar method should be called on the same element that you use to define the grid. See a working example .

I cannot help you with the PHP part of your question, because I myself do not use PHP. However, the demo files from the jqGrid download page seem to contain some sample PHP code that may be useful to you.

+7
source

Thanks to the previous author for the starting point to solve the problem. Here you can use the server-side PHP code snippet that implements the search query (from 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()); 
+3
source

You can try this code for the 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 it in production, make sure that you handle cases where $ _GET ['filters'] contains garbage instead of json and the field names / values ​​are correctly escaped. Otherwise, there is enough space for SLQ injections.

+2
source

Thanks for submitting the code!

The only change was that I had to unescape double quotes in the "filters" parameter to make it work:

$ filters = str_replace ('\ "', '"', $ _ POST ['filters']);

0
source

All Articles