I can't make individual column search work on table.ajax.reload(). Please help.

I can't make individual column search work on table.ajax.reload(). Please help.

whatacarwhatacar Posts: 7Questions: 3Answers: 1
var myCallback = function () {  this.api().columns().every(function () {....}

$('#example').DataTable({

initComplete: myCallback

 }); 

````
but the code below doesn't work:

table.ajax.reload(myCallback);
```
Thank you

This question has an accepted answers - jump to answer

Answers

  • whatacarwhatacar Posts: 7Questions: 3Answers: 1
    Answer ✓

    I am sorry for the ugly formatted question. Here is a ready to use solution, from Allan, that I've found, for those who might be interested:

    $('#example').DataTable({                    
                         "initComplete": function () {
                             this.api().columns([1, 10]).every(function () {
                                 var column = this;
                                 var select = $('<select><option value="">Select All</option></select>')
                                   .appendTo($(column.footer()).empty())
                                   .on('change', function () {
                                       var val = $.fn.dataTable.util.escapeRegex(
                                         $(this).val()
                                       );
    
                                       column
                                         .search(val ? '^' + val + '$' : '', true, false)
                                         .draw();
                                   });
    
                                 column.data().unique().sort().each(function (d, j) {
                                     select.append('<option value="' + d + '">' + d + '</option>');
                                 });
                             });
                         }
     });
    
    var filterIndexes = [1, 10];
                     table.on('draw', function () {                    
                         table.columns().indexes(filterIndexes).each(function (idx) {
                             var select = $(table.column(idx).footer()).find('select');
                             
                             if (select.val() === '') {
                                 select
                                   .empty()
                                   .append('<option value="">Select All</option>');
    
                                 table.column(idx, { search: 'applied' }).data().unique().sort().each(function (d, j) {
                                     select.append('<option value="' + d + '">' + d + '</option>');
                                 });
                               
                             }
    

    Please consider that question answered and close the post.

This discussion has been closed.