我正在使用數據表,並試圖根據隱藏列中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)。我只是不知道該怎麼寫。
你能說清楚你在問什麼嗎?因爲如果沒有你的代碼的例子,就不可能弄清楚你在問什麼。 – brandonscript
當然。我相應地更新了問題。 – dylanmac