<!DOCTYPE html>
<html>
  <head>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.25/css/dataTables.bootstrap4.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/fixedheader/3.1.9/css/fixedHeader.bootstrap4.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css">
    
    
    </script><script  src="https://code.jquery.com/jquery-3.5.1.js"></script>
    </script><script  src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
    </script><script  src="https://cdn.datatables.net/1.10.25/js/dataTables.bootstrap4.min.js"></script>
    </script><script  src="https://cdn.datatables.net/fixedheader/3.1.9/js/dataTables.fixedHeader.min.js"></script> 
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
    <meta charset=utf-8 />
    <title>DataTables - JS Bin</title>
  </head>
  <body>
    <table id="example" class="table table-bordered nowrap" style="width:100%">
        <thead>
          <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Tiger Nixon</td>
            <td>System Architect</td>
            <td>Edinburgh</td>
            <td>61</td>
            <td>2011/04/25</td>
            <td>$3,120</td>
          </tr>
          <tr>
            <td>Garrett Winters</td>
            <td>Director</td>
            <td>Edinburgh</td>
            <td>63</td>
            <td>2011/07/25</td>
            <td>$5,300</td>
          </tr>
        </tbody>
      </table>
  </body>
</html>
 
$(document).ready( function () {
    
    // Search1
    $('#example thead tr').clone(true).appendTo( '#example thead' );
    $('#example thead tr:eq(1) th').each( function (i) {
        var title = $(this).text(); 
        $(this).html( '<input type="text" placeholder="Search" style="width: 100%" />' );
        $( 'input', this ).on( 'keyup change', function () {
            if ( table.column(i).search() !== this.value ) {
                table
                    .column(i)
                    .search( this.value )
                    .draw();
            }
        } );
    } );
    
    // Search2
     $('#example thead tr:eq(0)').clone(true).appendTo( '#example thead' );
    $('#example thead tr:eq(2) th').each( function (i) {
        var title = $(this).text();
        $(this).html( '<input type="text" placeholder="Search" style="width: 100%" />' );
        $( 'input', this ).on( 'keyup change', function () {
            if ( table.column(i).search() !== this.value ) {
                table
                    .column(i)
                    .search( this.value )
                    .draw();
            }
        } );
    } );
  
  var table = $('#example').DataTable({
        orderCellsTop: true,
        scrollX: true,
          
          
    
            initComplete: function () {
            $('body').find('.dataTables_scrollBody').addClass("scrollbar");
            count = 0;
            this.api().columns().every( function () {
                var title = this.header();
                //replace spaces with dashes
                title = $(title).html().replace(/[\W]/g, '-');
                var column = this;
                var TEST = $(column.header()).closest('tr').next().children().eq( column.index() );
                var select = $('<select class="select2" style="width: 100%"  ></select>')
                    .appendTo( $(column.header()).closest('tr').next().children().eq( column.index() ).empty() )                    
                    .on( 'change', function () {
                      //Get the "text" property from each selected data 
                      //regex escape the value and store in array
                      var data = $.map( $(this).select2('data'), function( value, key ) {
                        return value.text ? '^' + $.fn.dataTable.util.escapeRegex(value.text) + '$' : null;
                                 });
                      
                      //if no data selected use ""
                      if (data.length === 0) {
                        data = [""];
                      }
                      
                      //join array into string with regex or (|)
                      var val = data.join('|');
                      
                      //search for the option(s) selected
                      column
                            .search( val ? val : '', true, false )
                            .draw();
                    } );
 
                column.data().unique().sort().each( function ( d, j ) {
                    select.append( '<option value="'+d+'">'+d+'</option>' );
                } );
              
              //use column title as selector and placeholder
              select.select2({
                multiple: true,
                closeOnSelect: true,
                placeholder: "Choose"
              });
               
              //initially clear select otherwise first option is selected
              select.val(null).trigger('change');
            } );
        } 
  });
} );
Output

This bin was created anonymously and its free preview time has expired. Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers