Individual column searching (select inputs) with Ajax

Individual column searching (select inputs) with Ajax

pvtpepperpvtpepper Posts: 4Questions: 2Answers: 0

Trying to get this code:

<script>
   jQuery(function(){
        jQuery('#datatable').dataTable({
            "ajax": "/sites/sample.json",
 
"columns": [
                { "data": "region" },
                { "data": "state" },
                { "data": "siteId" },
                { "data": "siteName" },               
            ],
"lengthMenu": [[10, 25, 100, -1], [10, 25, 100, "All"]]
 
    } );
} );
  
 </script>

To work with this:
https://datatables.net/examples/api/multi_filter_select.html

but getting stuck on where to insert it.

Thanks!

Answers

  • aaronwaaronw Posts: 89Questions: 3Answers: 4

    You would insert the "ajax", "columns", and "lengthMenu" options (everything in "" or [] after those options) right before initComplete

  • pvtpepperpvtpepper Posts: 4Questions: 2Answers: 0

    Like this?

    $(document).ready(function() {
        $('#dataTable').DataTable( {
            "ajax": "/sites/sample.json",
      
    "columns": [
                    { "data": "region" },
                    { "data": "state" },
                    { "data": "siteId" },
                    { "data": "siteName" },              
                ],
    "lengthMenu": [[10, 25, 100, -1], [10, 25, 100, "All"]],
    
            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>' )
                    } );
                } );
            }
        } );
    } );
    
This discussion has been closed.