Serverside-Processing & AJAX - Select filter not works

Serverside-Processing & AJAX - Select filter not works

vertisanvertisan Posts: 7Questions: 2Answers: 0

Hi!

I need to implement filter by select input to my table. I using serverside-processing to get data from DB and AJAX for dynamic actions.
Pagination, entries and search works fine but only this filter not, why?
Here is my code:

$(document).ready(function() {
    table = $('#VolunteersList').DataTable({ 
        searching: true,
        "processing": true,
        "serverSide": true,
        "order": [], 
        "ajax": {
            "url": "<?php echo site_url('wolontariat/przegladaj')?>",
            "type": "POST"
        },
        "language": {
            "url": "<?php echo site_url('assets/js/dataTables.Polish-lang.json') ?>"
        },
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "Wszystko"]],

        "columnDefs": [
        { 
            "targets": [ -1, 0, -2 ],
            "orderable": false,
        },
        ],

        "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
            nRow.className = aData[0 /* or whatever */] == "94" ? "HighlightUser" : "";
            // USTAWIĆ ZWRACANIE HIGHLIGHT Z BAZY
            return nRow;
        },

    });

});

    setInterval( function () {
        reload_table();
    }, 5000 );

    function reload_table() {
        table.ajax.reload(null,false);
    }

}

Answers

  • allanallan Posts: 61,920Questions: 1Answers: 10,153 Site admin

    Thanks for your question - however, per the forum rules can you link to a test case showing the issue please. This will allow the issue to be debugged.

    Information on how to create a test page], if you can't provide a link to your own page can be found here.

    Thanks,
    Allan

  • vertisanvertisan Posts: 7Questions: 2Answers: 0

    Ofc, here is website, where is implemented DataTables and where is problem.
    http://demo.vrs-factory.pl/DT/
    If is needed PHP Code - tell me.

  • allanallan Posts: 61,920Questions: 1Answers: 10,153 Site admin

    The client is sending the following to the server for the column search:

    columns[1][search][value]:^Florence Figueroa$
    

    Is your server-side processing script configured to accept regular expressions? I suspect not and that is the issue.

    I would suggest, at least in the first instance, that you drop the regular expressions - I doubt you need it with server-side processing.

    Allan

  • vertisanvertisan Posts: 7Questions: 2Answers: 0

    Here is my PHP code (I using CodeIgniter)
    Controller (creating array for DataTables)
    http://pastebin.com/wq7s9XqH

    Model (getting data from DB)
    http://pastebin.com/SU32GmFF

  • vertisanvertisan Posts: 7Questions: 2Answers: 0

    REF

  • allanallan Posts: 61,920Questions: 1Answers: 10,153 Site admin

    I'm afraid I don't support custom server-side code, but on a quick scan it looks like it isn't configured to support regular expressions. Either you need to change your server-side code to accept regular expressions (and the performance penalty that comes with that in SQL) or you need to not send a regular expression to the server-side.

    Allan

  • alderhernandezalderhernandez Posts: 33Questions: 11Answers: 1
    edited April 2016

    hey @vertisan, you solve the problem??? i want do the same thing in my proyect

  • equinoxequinox Posts: 1Questions: 0Answers: 0

    i found the way how to do this on
    http://coderexample.com/datatable-custom-column-search/

    but i must specify what column i want to have custom search one by one in model and view :x
    and this still show first page data only

    on my view im using initcomplete

    $(document).ready(function() { 
                          lengthMenu: [[5, 10, 25, -1], [5, 10, 25, "All"]],
                          processing: true, //Feature control the processing indicator.
                          serverSide: true, //Feature control DataTables' server-side processing mode.
                  ajax: {
                                     "url": "<?php echo site_url('Barang/ajax_list');?>",
                                     "type": "POST"
                           },
                           language: {
                                    "url": "<?php echo base_url();?>assets/lang/Indonesian.json"
                            },
                     initComplete: function () {
    
                this.api().columns([1]).every( function () {// i tried this on one column only
                    var column = this;
                    var select = $('<select><option value=""></option></select>')
                                        .appendTo( $(column.footer()).empty() )
                                         .on( 'change', function () {
                                         var val = $.fn.dataTable.util.escapeRegex(
                                $(this).val()
                            );
      
                            column
                               .search(val)
                                .draw();
                        } );
      
                    column.data().unique().each( function ( d, j ) {
                        //console.log(d);
                        //console.log(j);
                        select.append( '<option value="'+d+'">'+d+'</option>' )
                    } );
                } );
            },
    etcetc
    

    my controller
    http://pastebin.com/LRuNHYbv
    my model
    http://pastebin.com/XtaKDTMG

    just add this code above if($_POST['search']['value']) to get request from datatable based on selected column at "this.api().columns([1])"

    if($_POST['columns']['1']['search']['value'])
                {
                    if($i===0) // first loop
                    {
                        
                        $this->db->group_start(); 
                        $this->db->like($item, $_POST['columns']['1']['search']['value']);
                    }
                    else
                    {
                        $this->db->or_like($item, $_POST['columns']['1']['search']['value']);
                    }
    
                        if(count($this->column) - 1 == $i) //last loop
                        $this->db->group_end(); //close bracket 
                    
                }
    

    the example result
    https://goo.gl/photos/EMN3NhWNiYHSQNRz7

This discussion has been closed.