Dynamic Checkbox Filtering

Dynamic Checkbox Filtering

horanj07horanj07 Posts: 3Questions: 1Answers: 0

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

  • kthorngrenkthorngren Posts: 20,301Questions: 26Answers: 4,769

    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:

                var category=[$('input:checkbox[name="category"]:checked').map(function(){
                        return this.value;}).get().join('|');
    

    I am looking for an AND string to be created within the query.

    The use of | is an OR operator. For example, alpha|beta will display the row if the column has either alpha or beta. If you want an AND you will need to create a different regex expression. I don't believe regex has an AND operator.

    Kevin

  • horanj07horanj07 Posts: 3Questions: 1Answers: 0

    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??

  • horanj07horanj07 Posts: 3Questions: 1Answers: 0

    Ended up using the deferRender option set to true and processing speed decreased tremendously! So to reiterate the issues, this filter method will not work with serverSide processing enabled, use deferRender to speed up clientSide processing! Hope this helps someone in the future!

This discussion has been closed.