简体   繁体   中英

Apply column search to current jQuery DataTable

I have a current jQuery DataTable in place that is functioning properly:

 var $dataTable = $('#example1').DataTable({
   "ajax": 'api/tableSearch.php',
   "iDisplayLength": 25,
   "order": [[ 6, "desc" ]],
   "scrollY": 600,
   "scrollX": true,
   "bDestroy": true,
   "columnDefs": [{
     "targets": 0,
     "render": function (data, type, full, meta){
       return '<a class="editLink" href="#">Edit</a><a class="deleteLink" href="#">Del</a>':
     }
   }]
 });

As stated, the above code works accordingly...the search filter works, the sorting works, everything works.

What I would like to do is add a column search to this datatable, as shown here:
https://www.datatables.net/release-datatables/examples/api/multi_filter_select.html

I attempted to add the code from the link above to my current code, as follows:

 var $dataTable = $('#example1').DataTable({
   "ajax": 'api/tableSearch.php',
   "iDisplayLength": 25,
   "order": [[ 6, "desc" ]],
   "scrollY": 600,
   "scrollX": true,
   "bDestroy": true,
   "columnDefs": [{
     "targets": 0,
     "render": function (data, type, full, meta){
       return '<a class="editLink" href="#">Edit</a><a class="deleteLink" href="#">Del</a>':
     }
   }],  // begin here
   initComplete: function(){
     this.api().columns().every(function(){
       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 ? '^'+val+'$' : '', true, false )
           .draw();
         } );
         column.data().unique().sort().each( function ( d, j ) {
           select.append( '<option value="'+d+'">'+d+'</option>' )
         } );
        } );
      }
    } );        
 });     

I have not received any errors, and the DataTable still loads, but the column search is not there.

I am using jQuery-2.1.3.min, so it should be up to date.

Does anyone see what I am doing wrong and what I can do to correct this issue?

Add a <tfoot> to your table. The expression referencing column.footer() expects it to exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM