2014-01-29 75 views
2

我正在使用數據表,並試圖根據隱藏列中ID的存在來過濾我的表。隱藏的列包含多個ID並且它們由「@」符號分隔,例如, @ 2311 @ 11 @ 3546 @(注意:分隔符可以是任何東西;我們只是選擇「@」)。jQuery DataTables fnFilter:在連接字符串中查找字符串的完全匹配

當我將categoryId作爲var(filterValue)傳遞給DataTables fnFilter時,我得到了部分匹配。例如,如果我的categoryId = 1,它將匹配「1」,「11」和「2311」。

我希望categoryId與列之間的任何數字(由(@分隔))完全匹配。我很不熟悉fnFilter API支持的RegEx,我認爲這是最好的方法。不幸的是,我沒有太多有效的代碼可供分享。

這裏的功能我到目前爲止:

var oTable = $('#fundTable').dataTable(); //the dataTable object 
var filterCol = $("#table th.colCats").index(); //the index of the column to search in 
$('.product-filter').click(function() { //click a link to filter the data table 
    var filterValue = $(this).attr('href').match(/categoryId=([0-9]+)/)[1]; //grab the cat ID from the link 
    oTable.fnFilter(filterValue, filterCol, true, false); //use fnFilter API to pass in the value, the col index, regex true/false, smart filtering true/false 
    oTable.fnDraw(); //redraw the table using filter result 
    return false; //don't activate anchor link 
}); 

,這裏是我使用的表格的編輯版本:

<table id="fundTable"> 
    <thead> 
     <tr role="row"> 
      <th> 
       Fund 
      </th> 
      <th> 
       Categories 
      </th class="colCats"> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td> 
       Fund 1 
      </td> 
      <td> 
       @[email protected]@ 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Fund 2 
      </td> 
      <td> 
       @[email protected]@[email protected] 
      </td> 
     </tr> 
    </tbody> 
</table> 

相信數據表API說,你可以使用正則表達式表達代替值(filterValue)。我只是不知道該怎麼寫。

+0

你能說清楚你在問什麼嗎?因爲如果沒有你的代碼的例子,就不可能弄清楚你在問什麼。 – brandonscript

+0

當然。我相應地更新了問題。 – dylanmac

回答

1

這很簡單 - 我做一些我的代碼非常類似的東西(雖然我用「=」我的ID的兩邊 - 相應調整):

oTable.fnFilter('=' + filterValue + '=', filterCol); 
    oTable.fnDraw(); 

由於默認過濾查找字符串中任何位置的字符串,不需要進一步的正則表達式嚮導。

+0

這對我不起作用。它似乎適用於單個值,例如「@ 123 @」,但當具有多個值的列內的模式匹配時,過濾器不會檢索到記錄,例如, 「@ 123 @ 345 @ 678 @」 – dylanmac

+0

我說得太快了。它適用於兩個或更多整數,而不是單個整數。不知道爲什麼,但它可能是我的結局。謝謝! – dylanmac

+0

對於fnFilter調用中的第三個和第四個參數,你還在使用'true,false'嗎?如果是這樣,嘗試刪除或反轉它們 - 這些是從我使用的默認值翻轉。 –

1

我有類似的問題,但我只需要按順序匹配輸入。對我來說,這工作,在值前加^,並將第三參數(正則表達式)添加到true

注意:這是在配合使用輸入的是有附加一個jQuery KEYUP功能,因此複雜的第二參數。

oTable.fnFilter("^" + this.value, 
oTable.oApi._fnVisibleToColumnIndex(binding.gridId.fnSettings(), 
$("thead input").index(this)), true); 
+0

,這個添加'^'的想法爲我工作 – illusionist