jQuery, Tutorials

Jquery datatable select filter dropdown for search multiple value in column

06 March 2019

In this post, we are going to show you how add filter dropdown in column of jquery datatable.First you need to add jquery datatable cdn file and then apply datatable script in html table using script.
This example is almost identical to text based individual column example and provides the same functionality, but in this case using select input controls.
Add a dropDown element for selected columns to a Datatable allowing the user to filter the table to only show rows containing a certain value.

Table design and datatable jquery with filter search

<table id="example" class="display" cellspacing="0" width="100%">
      <th>Start date</th>
      <th>Start date</th>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Garrett Winters</td>
      <td>Ashton Cox</td>
      <td>Junior Technical Author</td>
      <td>San Francisco</td>

/*Add jquery for datatable search multiple value filtering select dropdown in each column*/

<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/bs/dt-1.10.12/b-1.2.2/fh-3.1.2/datatables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.2/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" src="//cdn.datatables.net/buttons/1.2.2/js/buttons.flash.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs/dt-1.10.12/b-1.2.2/fh-3.1.2/datatables.min.css"/>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.2.2/css/buttons.dataTables.min.css"/>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css">
<script type="text/javascript">
$(document).ready(function() {
    initComplete: function() {
      this.api().columns().every(function() {
        var column = this;
        /*added class "select"*/
        var select = $('<select class="select" multiple="multiple"><option value=""></option></select>')
          .on('change', function() {
            var vals = $('option:selected', this).map(function(index, element) {
              return $.fn.dataTable.util.escapeRegex($(element).val());

              .search(vals.length > 0 ? '^(' + vals + ')$' : '', true, false)
        column.data().unique().sort().each(function(d, j) {
          select.append('<option value="' + d + '">' + d + '</option>')
      /*select2 init for .select class */

Also note this example demonstrates the use of initComplete, a callback function triggered when the table has fully loaded. Use of this callback isn’t required in this example since the data is available in the table on load, but in the case of Ajax loaded data, initComplete is useful to execute code after the data has been loaded.

Leave a Comments