How to show Jquery Datatable Individual column searching (select inputs) at the top of the table header

How to show Jquery Datatable Individual column searching (select inputs) at the top of the table header

Dear All,

I had the below code where filters are working fine for html table. But I required it to show on top.
I had replaced the    .appendTo( $(column.footer ()).empty() ) to   .appendTo( $(column.header()).empty() ) .But my table headers are going down . I required dropdowns on top my table headers

can any one please help me how can I do it.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
  5. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  6. <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  7. <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
  8. <script>
  9. $(document).ready(function() {
  10.     $('#example').DataTable( {
  11.         initComplete: function () {
  12.             this.api().columns().every( function () {
  13.                 var column = this;
  14.                 var select = $('<select><option value=""></option></select>')
  15.                     .appendTo( $(column.header()).empty() )
  16.                     .on( 'change', function () {
  17.                         var val = $.fn.dataTable.util.escapeRegex(
  18.                             $(this).val()
  19.                         );
  20.                         column
  21.                             .search( val ? '^'+val+'$' : '', true, false )
  22.                             .draw();
  23.                     } );
  24.                  column.data().unique().sort().each( function ( d, j ) {
  25.                     select.append( '<option value="'+d+'">'+d+'</option>' )
  26.                 } );
  27.             } );
  28.         }
  29.     } );
  30. } );
  31. </script>
  32. </head>
  33. <body>
  34. <table id="example" class="display" style="width:100%">
  35.         <thead>
  36.             <tr>
  37.                 <th>Name</th>
  38.                 <th>Position</th>
  39.                 <th>Office</th>
  40.                 <th>Age</th>
  41.                 <th>Start date</th>
  42.                 <th>Salary</th>
  43.             </tr>
  44.         </thead>
  45.         <tbody>
  46.             <tr>
  47.                 <td>Tiger Nixon</td>
  48.                 <td>System Architect</td>
  49.                 <td>Edinburgh</td>
  50.                 <td>61</td>
  51.                 <td>2011/04/25</td>
  52.                 <td>$320,800</td>
  53.             </tr>
  54.             <tr>
  55.                 <td>Garrett Winters</td>
  56.                 <td>Accountant</td>
  57.                 <td>Tokyo</td>
  58.                 <td>63</td>
  59.                 <td>2011/07/25</td>
  60.                 <td>$170,750</td>
  61.             </tr>
  62.             <tr>
  63.                 <td>Ashton Cox</td>
  64.                 <td>Junior Technical Author</td>
  65.                 <td>San Francisco</td>
  66.                 <td>66</td>
  67.                 <td>2009/01/12</td>
  68.                 <td>$86,000</td>
  69.             </tr>
  70.             <tr>
  71.                 <td>Cedric Kelly</td>
  72.                 <td>Senior Javascript Developer</td>
  73.                 <td>Edinburgh</td>
  74.                 <td>22</td>
  75.                 <td>2012/03/29</td>
  76.                 <td>$433,060</td>
  77.             </tr>
  78.             <tr>
  79.                 <td>Airi Satou</td>
  80.                 <td>Accountant</td>
  81.                 <td>Tokyo</td>
  82.                 <td>33</td>
  83.                 <td>2008/11/28</td>
  84.                 <td>$162,700</td>
  85.             </tr>
  86.             <tr>
  87.                 <td>Brielle Williamson</td>
  88.                 <td>Integration Specialist</td>
  89.                 <td>New York</td>
  90.                 <td>61</td>
  91.                 <td>2012/12/02</td>
  92.                 <td>$372,000</td>
  93.             </tr>
  94.              
  95.         </tbody>
  96.         <tfoot>
  97.             <tr>
  98.                 <th>Name</th>
  99.                 <th>Position</th>
  100.                 <th>Office</th>
  101.                 <th>Age</th>
  102.                 <th>Start date</th>
  103.                 <th>Salary</th>
  104.             </tr>
  105.         </tfoot>
  106.     </table>
  107. </body>
  108. </html>


Srk