Get filter / search value on page load
Get filter / search value on page load
Hi,
I'm using this code to display a warning if filter is applied.
The reason is that the filter form may be toggle and I want the user see that filter is applied because stateSave keep the filter value in memory.
It is working fine when applying using the filter or do a search but how can I check on page load....
table7.on('search.dt', function() {
// check datatables search
if (table7.search(this.value) != "") {
$('#users-list_filter input').addClass('dfilter_applied');
} else {
$('#users-list_filter input').removeClass('dfilter_applied');
}
// check yadcf filters
if ( yadcf.exGetColumnFilterVal(table7, 0) != "" )
{
$('#users_7').show();
}
else {
$('#users_7').hide();
}
});
Also how can I echo / console.log the filter value (I mean the value used by the filter / search box, not the result)?
Thanks
This question has an accepted answers - jump to answer
Answers
Hi lenamtl,
This example shows how just
search()
without parameters returns the current search. Likewise,columns().search()
does the same if you allow searching on individual columns.I'm not too sure what the intention of your line 3 is -
able7.search(this.value)
will never return""
, it always returnsDataTables.Api
.For page load, if that's the initial load, you can add the event
init
on line 1.Hope that helps,
Cheers,
Colin
Hi Colin,
I have edited , I have remove this.value from the code so now it is
if (table7.search() != "")
and this check if the search field have a value this is still working the same .. I guess this.value has just no effect..I already tried init stateLoaded.dt event with no luck?
I'm out of idea...
Other solution I'm thinking about is looking directly in localstorage values but this is not easy to parse...
Could you give me an example with , columns().search() that have multiple columns search box, is there a way to globally find out if there is a search value applied or I have to check every single fields ID?
Thanks
I have tried all events and none was working.
So for page load, I wont use Datatables event, I'm using plain Javascript instead.
Hi lenamtl,
I'm struggling too! I'm trying to create an example that is doingwhat you want, but I'm also not seeing the stateSave events getting. Once I'm there, I'll post back!
Cheers,
Colin
Here we go! This example, if you open the console, will show the current state of the searches for the table and all columns. This also gets displayed correctly on table refresh and on each search. I hope this gets you going again!
Cheers,
Colin
Thanks