looking to see what filers are applied
looking to see what filers are applied

I am trying to determine what filters are applied. I have the below code off a button and I am testing with a global filter and a filter on a column using the search builder. The first part passes but writes out undefined to the console. The second part loops through but doesn't indicate that there are any filters in place.
{
text: 'What is filtered',
action: function (e, dt, node, config) {
if ($('#maintable').find('input[type=search]').val() == '') {
// Table is not filtered
console.log('not filtered ');
} else {
// Table is filtered
console.log(' filtered ', $('#maintable').find('input[type=search]').val());
};
var dtable = $('#maintable').DataTable();
maintable.columns().every(function () {
var column = this;
var columnIndex = column.index(); // Get the column's index
var currentFilter = column.search(); // Get the current search string for this column
if (currentFilter !== '') {
console.log('Column ' + columnIndex + ' has a filter applied: "' + currentFilter + '"');
// You can perform further actions here based on the filtered column
} else {
console.log(' Column ' + columnIndex + ' does not have a filter applied.');
}
});
}
This question has an accepted answers - jump to answer
Answers
For the global search input you will want to use
search()
to get the search term.SearchBuilder doesn't use
column().search()
. UsesearchBuilder.getDetails()
to get the SearchBuilder search terms. See this test case:https://live.datatables.net/jigutico/1/edit
Click the
Get Details
after adding one or more search terms.Kevin