2011-05-09 105 views
4

http://trirand.com/blog/jqgrid/jqgrid.html,在「3.7新增功能」>「列搜索」下,有一種方法用於搜索,但它尚未適用於我。我已經添加了:如何讓jqGrid工具欄搜索工作?

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

你可以發佈你使用的jqGrid代碼嗎?有'

'元素,你使用'id =「工具欄」'?您使用哪個版本的jqGrid? – Oleg2011-05-09 19:24:48

+0

表中有另一個我在代碼中使用的ID。我正在使用jqGrid 4.0。 – JonathanHayward 2011-05-09 21:36:08

回答

7

應該在用於定義網格的相同元素上調用filterToolbar方法。看看the working example用過它。

我不能幫你解決你的問題的PHP部分,因爲我自己不使用PHP。儘管如此,從the jqGrid download pagedemo files似乎包含一些PHP代碼示例,這可能會對您有所幫助。

+0

是的演示頁面上的jqgrid的PHP代碼不正確,因爲它沒有包含任何搜索功能。引用sql的一個未定義的'$ where'子句變量...: - / – 2012-08-26 08:48:30

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 [「過濾器」]裝着垃圾,而不是JSON和字段名稱/值正確逃脫。否則SLQ注射有足夠的空間。

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()); 
0

感謝張貼你的代碼!

唯一的變化是,我曾在「過濾器」參數來取消轉義雙引號來得到它的工作:

$過濾器= str_replace函數(「\」「「」」,$ _ POST ['過濾器「]);

相關問題