Add filter to server side Datatable

Add filter to server side Datatable

zwokaroszwokaros Posts: 1Questions: 1Answers: 0

Hello

I have a wonderful datatable

I would like to add more searching parameters (the same as the search text area or the row limit display)

For example a select, that will send a new field in the ajax request for "category" or a checkbox to only display the items added yesterday in my database

I haven't found a solution so far to add filters to my server sided datatable

Have a good day

Answers

  • culterculter Posts: 102Questions: 24Answers: 0

    I'm interested in this question too. I'll solve adding new filters in the near future.

  • RedPandaRedPanda Posts: 3Questions: 1Answers: 0
    edited October 2018

    Hi,

    Here is how i did my DataTable to add filters.

    $j('#mytable').DataTable( {
                serverSide: true,
                processing:true,
                ajax: {
                    url: 'myurl.php',
                    data: function(data){
                            var filter1= $j('#filter1').val();
                            var filter2= $j('#filter2').val();
                            var filter3= $j('#filter3').val();
    
                            if(filter1)
                                data.filter1= filter1;
    
                            if(filter2)
                                data.filter2= filter2;
    
                            if(filter3)
                                data.filter3= filter3;
    
                            if($j('#filter4').is(':checked'))
                                data.filter4= 1;
                            else
                                data.filter4= 0;
    
                    },
                    dataSrc: function(json){
                        if(json.data)
                            return json.data;
                        else
                            return [];
                    },
                    type:'POST'
                },
    ......
    

    In dataSrc it's json because in my case, my .php file return json encoded data.
    Retrieve the filters value in your server side and do what you want with it.

  • culterculter Posts: 102Questions: 24Answers: 0

    Thank you for your code, RedPanda. Did you use server_processing.php script by DataTables or you've created your own? In my case I'm using server_processing.php from this web site and it is returning JSON data.

    Which type of filtering did you use? I need date picker and checkboxes. Could you please share also the rest of your code to see how it's working?

This discussion has been closed.