JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
<p style="text-align: center">
This Fiddle was forked by <a href="https://stackoverflow.com/users/9606459/giacomo-penuti">Giacomo Penuti</a>, for <a href="https://stackoverflow.com/q/49846701/9217760">this Stack Overflow question</a>. <small><a href="https://jsfiddle.net/41vgefnf/1/">See the original Fiddle</a>.</small>
</p>
<hr />
<table id="example" class="display" cellspacing="0" 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>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$86,000</td>
</tr>
<tr>
<td>Cedric Kelly</td>
...
CSS
/* Styles for the drop-down. Feel free to change the styles to suit your website. */
.cb-dropdown-wrap {
min-height: 23px;
max-height: 23px;
transition: max-height 0.20s ease;
overflow-y: auto;
border: 1px solid #888;
position:absolute;
z-index: 1;
background-color: #fff;
margin-top: 5px;
}
.cb-dropdown-wrap:hover {
height: auto;
max-height: 80px; /* At most, around 3/4 visible items. */
}
.cb-dropdown,
.cb-dropdown li {
margin: 0;
padding: 0;
list-style: none;
}
.cb-dropdown li label {
padding: 3px 0;
display: block;
position: relative;
cursor: pointer;
border-bottom: 1px solid #eee;
}
.cb-dropdown li label > input {
position: absolute;
right: 0;
top: 0;
width: 16px;
}
.cb-dropdown li label > span {
display: block;
margin-left: 3px;
margin-right: 20px; /* At least, width of the checkbox. */
font-family: sans-serif;
font-size: 0.8em;
font-weight: normal;
text-align: left;
}
.factive {
background-color: #c9e2c6;
}
#example th {
padding-bottom: 40px;
background-position: 85% 8px;
min-width: 50px;
}
JavaScript
// This code has been beautified via http://jsbeautifier.org/ with 2 spaces indentation.
jQuery(document).ready(function($) {
function cbDropdown(column) {
return $('<ul>', {
'class': 'cb-dropdown'
}).appendTo($('<div>', {
'class': 'cb-dropdown-wrap'
}).appendTo(column));
}
$('#example').DataTable({
initComplete: function() {
this.api().columns().every(function() {
var column = this;
var ddmenu = cbDropdown($(column.header()))
.on('change', ':checkbox', function() {
var vals = $(':checked', ddmenu).map(function(index, element) {
return $.fn.dataTable.util.escapeRegex($(element).val());
}).toArray().join('|');
column
.search(vals.length > 0 ? '^(' + vals + ')$' : '', true, false)
.draw();
//console.log(vals);
if(vals === ""){
$(this).parent().parent().parent().removeClass("factive");
}else{ $(this).parent().parent().parent().addClass("factive");
}
//change callback
});
column.data().unique().sort().each(function(d, j) {
var // wrapped
$label = $('<label>'),
$text = $('<span>', {
text: d
}),
$cb = $('<input>', {
type: 'checkbox',
value: d
});
$text.appendTo($label);
$cb.appendTo($label);
ddmenu.append($('<li>').append($label));
});
});
$(".cb-dropdown-wrap").each(function(){
console.log($(this).parent().width());
$(this).width($(this).parent().width());
});
}
});
});