Using filters with Datatables?
Using filters with Datatables?
Link to test case: https://pastebin.com/qzmjLVBF
Debugger code (debug.datatables.net): I don't know, NA
Error messages shown: NA
Description of problem: I would like to know how to use select box filters with datatables. I've managed to hack it so that it does a refresh when I change the filter but it's not how I would like to do it. I would just call $("#table").DataTable({}) again but it doesn't allow that. I've had a look through the list of AJAX capabilities but I can't see what I need.
This question has an accepted answers - jump to answer
Answers
The issue at the moment is that
"url":"fetch.php?filter="+filter,
is only ever evaluated once. What you want is that every time you reload the data for the table (which you would do usingajax.reload()
is for the filter value to re-evaluated. For that, useajax.data
:Also make sure you assign the DataTable to a variable:
And in your
change
event handler callajax.reload()
:Job done
Allan
Brilliant! It worked first time thanks. In this example:
Javascript syntax never gets any easier
It just an example file name. You could have
/my/really/long/path/to/myData
or anything else - as long as it returns valid JSON.Not it isn't a "class" in the technical term - it is just an object with properties attached to it. The full list is in the reference.
Our latest package of DataTables has typescript typings in it, so if you are using TypeScript it should auto complete when everything is setup and working.
Allan