I have a datatable and the 5th column of this table contains several labels. I also have a separate labels list with checkboxes, where user can select multiple labels and filter the table. I found the following way to filter the table:
table.column(5).search('value1|value2', true, false).draw();
This returns all the rows, that contain value1
OR value2
, but I need to return the rows that contain both, value1
AND value2
, but I could not find anything about this. I tried something like this as an experiment, but it did not work:
table.column(5).search('value1&value2', true, false).draw();
How do I search the datatable with an AND condition?
As andrewJames suggested in the comment, I used the Datatables search plug-in and created a custom filter function:
$('.labels-filter-button').unbind().click(function(){
$.fn.dataTable.ext.search.push(function (settings, data, dataIndex) {
let selectedLabels = $('#labelsFilterSelector').val();
let content = data[5];
let rowIncludes = true;
for (var i = 0; i < selectedLabels.length; i++) {
if (content.indexOf(selectedLabels[i]) == -1){
rowIncludes = false;
}
}
return rowIncludes;
});
table.draw();
$.fn.dataTable.ext.search.pop();
});
Works like a charm on any number of inputs.