Dynamic Checkbox Filtering
Dynamic Checkbox Filtering

Hello--I feel this question may have already been asked, but I was unable to find a workaround with information given in other threads as this situation is unique. I have a dynamic checkbox filter that is generated using php and distinct values from my database. When using the datatable search feature, I am able to filter only one of the fields. When a second field is checked, no results are returned. The code example is as follows--
CHECKBOX FILTER
</h6>Category</h6>
<ul class="list-group">
<?php $query = "SELECT DISTINCT(Category) FROM `Table` WHERE Status='1' ORDER BY `Category`";
$statement=$dbcon->prepare($query) or die('error getting data');
$statement->execute();
$result=$statement->fetchAll();
foreach($result as $row)
{
?>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input name='category' type=checkbox class='categories' value="<?php echo $row['Category']; ?>"></input>
<?php echo $row['Category'];?>
</label>
</div>
</li>
<?php
}
?>
</ul>
SCRIPT
$('input:checkbox').on('change', function(){
var category=[
$('input:checkbox[name="category"]:checked').map(function(){
return this.value;}).get().join('|')];
dataTable.column(2).search(category, true,true,false).draw(false);
});
Essentially, I am looking for an AND string to be created within the query. According to the developer console on Chrome, the multiple values are being passed, just not displayed.
Any help is appreciated!
Answers
Your code looks similar to this working example:
http://live.datatables.net/vipifute/1/edit
Except it looks like you are creating an array. Try this instead:
The use of
|
is an OR operator. For example,alpha|beta
will display the row if the column has eitheralpha
orbeta
. If you want an AND you will need to create a different regex expression. I don't believe regex has an AND operator.Kevin
Sorry for the confusion, I was trying to relate to a SQL query so when I wrote AND, I actually meant IN---but anyway, so I did some research on the forums and found out that regex search doesn't work when serverSide processing is enabled. Once I disabled serverSide, the search worked as expected! Next question is, in order to enhance processing speed, how would I be able to have both a regex function along with serverSide processing??
Ended up using the
deferRender
option set totrue
and processing speed decreased tremendously! So to reiterate the issues, this filter method will not work withserverSide
processing enabled, usedeferRender
to speed up clientSide processing! Hope this helps someone in the future!