selectpicker with container reference

by Sascha

HTML

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css" rel="stylesheet">
<link href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap4.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.17/css/bootstrap-select.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.17/js/bootstrap-select.min.js"></script>

<table id="example" class="table table-bordered table-hover nowrap" style="width:100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Ashton Cox</td>
      <td>Junior Technical Author</td>
      <td>San Francisco</td>
    </tr>
    <tr>
      <td>Cedric Kelly</td>
      <td>Senior Javascript Developer</td>
      <td>Edinburgh</td>
    </tr>
    <tr>
      <td>Airi Satou</td>
      <td>Accountant</td>
      <td>Tokyo</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
    </tr>
  </tfoot>
</table>
<div id="test" style="background:red;">

</div>

CSS

body {
  background: white;
  padding: 20px;
  font-family: Helvetica, sans-serif;
}

.highlight {
  background: green;
  color: white;
}
select {
  position: absolute;
}

JavaScript

$(document).ready(function() {

  window.currentSelect = false;

  var table = $('#example').DataTable({
    searching: false,
    info: false,
    paging: false,

    initComplete: function() {
      let ci = 0;
      this.api().columns().every(function() {
        //add select to each column footer
        var column = this;
        let cf = $(column.footer()).empty();
        var select = $('<select class="form-control show-tick" data-container="body" data-header="Select option(s)" data-actions-box="true" data-live-search="true" title="All" data-selected-text-format="count > 0" multiple></select>')
          .appendTo(cf).selectpicker({container: '#ci'+ci});
          cf.attr('id',  'ci' + ci).data('col', ci);
        // populate the select options with unique values of current column          
        column.data().unique().sort().each(function(d, j) {
          select.append('<option value="' + d + '">' + d + '</option>');
        });
        ci++;

      });

      //add 'clear filter' button below the search box in the dropdown select      
      var buttonPosition = $('.bs-searchbox');
      $('<div class="text-center"><button type="button" class="btn btn-sm btn-light clearButton">Clear filter</button></div>').appendTo(buttonPosition);
      //        
      $(".clearButton").on("click", function() {
        var col = $(this).closest('th').data('col');
        alert('Column:' + $('table thead tr th').eq(col).html().trim());
      });
    }
  });
});