2012-03-01 50 views
0

您好我正在使用jQuery的數據表插件,我想修改我的服務器端腳本,以允許負面搜索前。 !FOO。它會顯示一切,但美孚。我有一些我寫的代碼,但它似乎並沒有工作,我不知道爲什麼。我可以定期過濾,但是當我使用!foo時,它只是返回表格中的所有內容。如何對jQuery插件數據表進行負面過濾?

$sWhere = ""; 
if ($_GET['sSearch'] != "") 
{ 
    $aWords = preg_split('/\s+/', $_GET['sSearch']); 
    $sWhere = "WHERE ("; 

    for ($j=0 ; $j<count($aWords) ; $j++) { 
     if ($aWords[$j] != "") { 
      if(substr($aWords[$j], 0, 1) == "!") { 
        $notString = substr($aWords[$j], 1); 
        $sWhere .= "("; 
        for ($i=0 ; $i<count($aColumns) ; $i++) { 
          $sWhere .= $aColumns[$i]." NOT LIKE '%".mysql_real_escape_string($notString)."%' OR "; 
        } 
      } else { 
       $sWhere .= "("; 
       for ($i=0 ; $i<count($aColumns) ; $i++) { 
         $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($aWords[$j])."%' OR "; 
       } 
      } 
      $sWhere = substr_replace($sWhere, "", -3); 
      $sWhere .= ") AND "; 
     } 
    } 
    $sWhere = substr_replace($sWhere, "", -4); 
} 

感謝

回答

0

的數據表插件有一個內置的過濾功能,可使用正則表達式的作品。如果你可以使用它,你可以嘗試一個負面的前瞻斷言。

^((?!foo).)*$ 

看看這裏的例子,看看這是否是你所需要的: http://datatables.net/release-datatables/examples/api/regex.html

+0

謝謝你的回覆,但不幸的是,這不是我正在尋找 – waa1990 2012-03-01 19:45:29

0

所有你已經粘貼代碼是在PHP,所以它不使用數據表的框架在所有搜索我打算假設你已經啓用了服務器端處理。

爲了從服務器端使用SQL NOT LIKE做一個否定過濾器就好了,正如你試過的那樣,但是你所有的正負濾波器代碼都在相同的摘要循環中處理。我會添加另一個$_GET,如$_GET['sSearch_type'],以在完全獨立的循環中處理負濾波器。只要您可以在PHP MyAdmin(或您使用的任何SQL管理器)中重現查詢,使用單獨的$_GET來控制搜索類型,在單獨的循環中設置查詢將不會有任何問題。

對於那些你們誰想要實際上使用數據表進行篩選,並需要負過濾器表達式: @Nate是有關使用regex表達正確的,但執行不討論。

有兩種我知道以這種方式使用正則表達式的API方法。無論是fnFilter()search()

fnFilter()已過時,你仍然可以使用它,但它是更好地使用上面寫着更換fnFilter()search() API funcion。

文檔:

實施例:

// case insensitive filter a column for all 
// values that don't match 'closed' 
column.search('^(?!closed).*$', true, false, true).draw(); 

示例實現:

/** 
* This example creates a select box filter which 
* appends to the thead cell of the 3rd column 
* from the left (i.e. column at index 2). The values 
* of the select <option></option> are generated 
* automatically based on the column's real values 
* except for the 2 top most filters. The first is "All" 
* which passes and empty string to the regex filter, 
* and the second top most filter is "All Open Projects" 
* which triggers the negative regex filter to remove 
* all rows where the column value is set to "Closed" 
* or "closed" 
*/ 
$(document).ready(function() { 
    $('#report-table').DataTable({ 
     initComplete: function() { 
      this.api().columns().every(function() { 
       var column_index = this[0][0], 
        column = this, 
        not_closed = 'not_closed', 
        select = $('<select id="status_col_filter" style="margin-left:10px;"><option value="">All</option><option value="'+ not_closed +'">All Open Projects</option></select>'); 

       if (column_index === 2) { 
        select 
         .appendTo($(column.header())) 
         .on('change', function() { 
          var val = $.fn.dataTable.util.escapeRegex($(this).val()); 
          if (val === not_closed) { 
           // negative regex for all open status projects 
           column.search('^(?!closed).*$', true, false, true).draw(); 
          } else { 
           // regular positive regex - filter by column value 
           column.search(val ? '^'+val+'$' : '', true, false).draw(); 
          } 
         }); 

        column.data().unique().sort().each(function (d, j) { 
         select.append('<option value="'+d+'">'+d+'</option>') 
        }); 
       } 
      }); 
     } 
    }); 
});