Callback for Custom Search Builder / Filtering
Callback for Custom Search Builder / Filtering
Hi folks -- simple question that I am having trouble finding an answer to: is there a callback for Filtering on a DataTable?
I'd like to update some variables when the user filters a DataTable, but can't find a way to trigger on the user selecting a new Filter in the Custom Search Builder. Found an old threads about a callback being added in the future, but that was from 2009 and I'm struggling to find documentation on it. Did that ever get added? Perhaps I'm just having trouble finding the callback.
Any guidance would be greatly appreciated!
Replies
Not as such - part of the issue is that the filtering can be done in so many different ways, so generally it is best to just use the generic
draw
event.What is it specifically that you want to do with the SearchBuilder value / condition? It sounds like you might need something specific to SearchBuilder more than a filter callback.
Allan
I'm trying to simplify the state saving process by embedding certain filter values in my page's URL. So when a user adds a new filter to the table, push the value they filtered on to the URL.
My site deals with stats for Pokemon, so they have attack, defense, and hp. If a user were to add a filter to only show attack greater than 100 (for example), I want to update the URL to add a parameter of '&attack=100" in this case.
And can't figure out how to do this as the table isn't being re-drawn when the filters are applied (which is great for performance!) But I also don't want to have to have the user hit a "Save to URL" button to accomplish this, would strongly prefer it be automatic if I can find a way to trigger on the user adding a new filter to the table.
Thanks again for your help!
Thanks for the clarification.
I'd do it on each draw (
draw
) - just check to see if the state that effects the URL has changed or not and updating the URL accordingly. Then it doesn't matter how the state is changed, it will update as needed.It isn't exactly what you are looking for, but have a look at this blog post I wrote back in 2017. It is an attempt to do deep linking into DataTables like you describe. I've never tried it with SearchBuilder though...
Allan
Thanks Allan -- having issues with the drawCallback, for some reason neither the table variable or this.api() is allowing me to call searchBuilder.getDetails() within the drawCallback.
Here's an example showing the issue: https://live.datatables.net/duhilipu/2/edit?html,js,console,output
Feel like I'm missing something obvious here, but if I can get the active filters from the searchBuilder I can "diff" them against a prior draw call to understand what has changed and update the URL accordingly.
Thanks again for all of your help with this!
For completeness, I've also tried two other methods with no success:
Using settings object per the drawCallback docs: https://live.datatables.net/jadayiru/1/
Using table directly: https://live.datatables.net/kayozoyu/1/
So the problem there is that SearcBuilder hasn't actually constructed itself when the first
drawCallback
happens! Indeed, it doesn't actually happen until right at the very end (it usesinit
, which happens afterinitComplete
).Here is how you can work around that: https://live.datatables.net/duhilipu/4/edit .
I think I'll probably change SearchBuilder in that regard, it seems a bit wrong that is isn't initialised until after the table claims to be fully ready.
Allan
Thanks Allan, so initComplete fires, then init fires for the Search Builder and then I should be able to execute the code I'm working with?
Is there a code reference for Search Builder's init callback? Thinking I can just set up a "lock" via some sort of global variable that would be set to true within Search Builder's init and have my function wait for that variable to be true before proceeding if that makes sense.
Or is there a reason why the timeout approach would be the better approach? It does seem simpler from a code complexity standpoint.