jQuery DataTables: Multiple checkbox filtering

jQuery DataTables: Multiple checkbox filtering

umair_malikumair_malik Posts: 7Questions: 3Answers: 0

i'm fetching records in datatable using server side scripting which fetch record perfectly. Now i want to apply filtering record using checkboxes. these checkboxes are outside datatable. previously i was using this method to filter datatable record on client side

$(':checkbox[name="Backplane_Connector[]"]').click(function() {
var filter = '',
regexFilter = true,
smartFilter = false;

filter = $('[name="' + this.name + '"]:checked').map(function() {
    return this.value;
}).toArray().join('|');
if (filter != '')
    filter = '^(' + filter + ')$';
$('#example').dataTable().fnFilter(filter, 0, regexFilter, smartFilter);

});

This question has accepted answers - jump to:

Answers

  • umair_malikumair_malik Posts: 7Questions: 3Answers: 0
    edited February 2015

    record is coming via this code

    $(document).ready(function() {

    var oTable = $('#example').DataTable({
    
        "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
    
        "iDisplayLength": 100,
    
        "bProcessing": true,
    
        "bServerSide": true,
    
        "sAjaxSource": "server_processing.php",
    
    });
    

    });

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39
    Answer ✓

    The best solution is to modify your query on the server-side portion based on your filters. Here is a way to dynamically send values to your server-side call.

    Then every time a filter is changed, you can call oTable.ajax.reload() and your table will be updated based on the current filters selected.

  • umair_malikumair_malik Posts: 7Questions: 3Answers: 0

    the link you provided is not displaying record of php. I'm new in using datatables and i didnot found any solution to apply filters via checkboxes :(

  • umair_malikumair_malik Posts: 7Questions: 3Answers: 0

    let me explain you briefly,

    suppose i've a table with two columns name and class.
    -datatable will display all the record fetched from the DB when page load.
    - there are checkboxs on left side of datatable which display distinct names and classes in their values(names- jhon,joe,umair classes-1,2,3,4,....10)
    requirement:-

    whenever a user check a checkbox (e.g jhon), datatable must display all record where name= jhon,
    he can check multiple checkboxes at a time...

    some people says i've to write my own sql queries for this but i don't think its the write solution because how can i write queries if there were thousands of names and classes.

    i've no idea how to do this in server side processing of datatable.. please help i need it badly!

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39
    edited February 2015 Answer ✓

    You could make an array of checked values for your filter, the values being the name of the person you'd need to find. Something like this would handle as many names being checked as you wanted. Just build the array in JS as people check names and pass it to your server-side script.

    $inString = "";
    $i = 0;
    if(isset($_POST['filter_names']) && is_array($_POST['filter_names'])){
        foreach($_POST['filter_names'] as $name){
            if($i > 0){
                $inString.= ",'$name'";
            }else{
                $inString.= "'$name'";
                $i++;
            }
        }
    }
    
    if($i >0){
        $query = "SELECT * FROM my_table WHERE name_column IN ($inString)";
    }else{
        $query = "SELECT * FROM my_table";
    }
    
This discussion has been closed.