I want to add a drop-down filter on dataTables
, only on a single column.
I have added the below code, but it add drop-downs on all columns:
$('#dataTables-example').DataTable({
responsive: true,
"pageLength": 100,
"lengthMenu": [100, 250, 500, "All"],
initComplete: function () {
this.api().columns().every( function () {
var column = this;
$('#dataTables-example .head .head_hide').html('');
var select = $('<select id="formfilter" class="filterdropdown"><option value="">'+$(column.header()).text()+'</option></select>')
.appendTo( $(column.header()).empty())
.on( 'change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search( val ? '^'+val+'$' : '', true, false )
.draw();
});
column.data().unique().sort().each( function ( d, j ) {
select.append( '<option value="'+d+'">'+d+'</option>' )
});
});
});
SOLUTION
Change
this.api().columns().every( function () {
to
this.api().columns(1).every( function () {
where 1
is zero-based column index.
NOTES
columns([1,3])
format instead.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With