How to add a filter above my table that's filled via Ajax?
How to add a filter above my table that's filled via Ajax?

Hi, I'm only days into DataTables, but already pretty hooked. I have a project, that I was working on and have now rebuilt it with DataTables – and solved most of the initial problems. But now I'm a bit stuck:
Let me explain: I am building a table, that I fill with data that I'm creating/manipulating/refining in Google Sheets (for me as a basically non-coder this is by far the easiest way to do that, and I have already built something, I really like). But I don't like very much how Sheets is presenting the table and now finally I found a way of doing that via:
https://www.bpwebs.com/how-to-display-google-sheets-data-on-a-website
Thanks to the explanation from DataTables and this very helpful forum, I've already gotten very far, but now I haven't been able to add the possibility to filter the data via forms above the columns. I found explanations, but couldn't make them work. Could anyone help me?
This is the relevant code (I think it won't fill the data in there, but for the filter forms this doesn't matter, right?):
https://live.datatables.net/rabavavu/1/edit
Here is the whole page on GitHub (including more scripts, that I tried to use to get the filters):
https://github.com/fingolas23/AccountsOverview/blob/main/overview.html
And here is the page itself (including the data itself):
https://fingolas.eu/fediverse/overview.html
Answers
Sounds like you will want to use
ajax.data
as a function to send the values you have in your inputs to the. server. See this example. In your event handler to search the values useajax.relaod()
to refresh the table data. You don't need to useserverSide: true
, like the example, to useajax.data
as a function.Kevin
It would probably look something like this:
Thank you very much. I didn't get it to work on my first try and will try again later with more time.
What did you try?
What exactly isn't working?
I built a simple example:
https://live.datatables.net/socirone/49/edit
The server script in this environment doesn't use the parameters sent by
ajax.data
. Use the browser's network inspector to see theinput
's value is sent via ajax. For example:If you still need help then please update my example or provide a link to a running test case showing the issue. The test case you posted has some errors.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
But even in your example, the field "Ajax Search" doesn't filter. Or am I missing something?
Sadly, I can't get my example working in live.datatables, It think because it won't load the json from external.
This is the example code:
https://live.datatables.net/xosokahe/1/edit
Here it works:
https://fingolas.eu/fediverse/test.html
What I want is a box exactly like this one (for some columns):
https://datatables.net/extensions/searchpanes/examples/advanced/columnFilter.html
As I said the server script is not setup to filter based on what is entered into the
Ajax search
field. That is why I suggested looking at the network inspector to see the parameter being sent to the server. Once the field is sent to the server it is up to you to develop the server side code to retrieve parameter and filter the data.You can update your Here it works link with your code and we can help debug the client. However we won't be able to debug your server code which is what does most of the work.
SearchPanes will work if all the data is at the client or if you are using Server Side Processing which requires a server library that performs server based searching, paging and sorting. You can use one of the Datatables supplied libraries for server side processing. See this blog about using the Editor server side libraries to support SearchPanes and SearchBuilder.
You can create your own inputs if you want to use the
ajax.data
option to filter the returned dataset based on search inputs.Kevin
Oh sorry, this is the example code:
The json looks like this:
I think that is pretty much the code in your test case. What you have is pretty standard for client side processing with
ajax
loaded data. Do you have specific questions at this point? Reposting the same code without additional questions isn't going to go much further.With this setup you can use any combination of the following:
search()
orcolumn().search()
. Similar to option 1.ajax.data
as a function to send input values as parameters to the server to filter the JSON response. Requires scripting in the server code to fetch the parameter(s) and filter the response data.Now its your turn to choose one or more of these options to try. I suggest trying them with your test case. If you have questions or difficulties we can take a look at the updated test case to see what you are trying to do to help debug.
Kevin
Thank you very much! I'll look into it and come back to you.