I have followed this example to add dropdown filtering on my DataTables. It works fine when there is only 1 table on a page but I have a scenario where I have 2 tables.
As per the DataTables documentation (link 1, link 2), the API get access to all the DataTables instance at the same time.
So, I tried to target each table with something like
this.api().tables([0]).columns([3, 4]).every( function () {
but the filtering drop downs are appearing in the 1st table only.
$('#table1').DataTable({
"dom": '<"dtWrapper"<f>lrti<"paginate0rem"p>>',
drawCallback: function(){
if (feather) {
feather.replace({
width: 14,
height: 14
});
}
},
orderCellsTop: true,
initComplete: function () {
this.api().tables([0]).columns([3, 4]).every( function () {
let column = this,
select = $('<select class="form-control"><option value=""></option></select>')
.appendTo($('thead tr:eq(1) th:eq(' + this.index() + ')'))
.on('change', function () {
let 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>')
});
});
}
});
$('#table2').DataTable({
"dom": '<"dtWrapper"<f>lrti<"paginate0rem"p>>',
drawCallback: function(){
if (feather) {
feather.replace({
width: 14,
height: 14
});
}
},
orderCellsTop: true,
initComplete: function () {
this.api().tables([1]).columns([2, 3]).every( function () {
let column = this,
select = $('<select class="form-control"><option value=""></option></select>')
.appendTo($('thead tr:eq(1) th:eq(' + this.index() + ')'))
.on('change', function () {
let 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>')
});
});
}
});