I would like to query a JSON via post request from a rest API:
http://localhost/post1
param1='1'
that returns the following:
{
"json_table": [
{
"date": 123,
"test": "hello2"
},
{
"date": 19,
"test": "hello"
},
}
and it should then be automatcially populated into a jquery datatable, a little bit how it is described here:
$('#myTable').DataTable( {
ajax: '/api/myData'
} );
What I don’t understand is:
- How can I tell it to make a post request with a parameter
- How can I create the table without having to predefine it in HTML, so that it’s completely agnostic what is returned from the JSON and just displays the table accordingly, whatever is within the ‘json_table’ record (which is generated from a pandas data frame df.to_json(orient=’records’)?
- How can I make it refresh (query again) every 15 seconds and update the table?
Any suggestions are appreciated.
2
Answers
Check this Ans
Creating a dynamic DataTable from arbitrary JSON is certainly possible – but it can get complicated depending on how many of the features of DataTables you are trying to use.
Also, if you have control over the JSON being sent from the server, you can make things easier for yourself. I will assume you do have such control for what follows.
Static Example
Assume we start with this simple static example:
The server sends this JSON data:
And we have a simple DataTables definition like this:
Enhancing the JSON
To make things easier for ourselves, we can enhance the JSON being sent from the server, to contain metadata about the columns in the data records:
This new
columns
section in the JSON is basically the same information as was hard-coded into the first DataTables definition.We can read that into a variable, when we receive the JSON from the server, and then use this variable in our DataTables configuration.
And we can do the same thing for the data records also:
For more flexibility, the ajax request can be separated out from DataTables:
The easiest way to assemble what you need is (I think) to write the hard-coded version of your data table – and then to start replacing pieces with dynamically created variables.
POST with parameters
The above ajax example includes these lines:
This means the ajax request will be a POST and will contain a request body in this case containing
foo=bar
. You can, of course put whatever you need in there. It’s standard form data. The server would read this data in whatever standard way it uses (e.g. request context form parameters).Auto-Refresh
I haven’t done this myself, but there are solutions documented using
setInterval
– something like this:Final note – just to reiterate – this is not going to be able to handle whatever JSON you throw at it. And if you have no control over the JSON structures, then there may well be too many differences from one payload to the next.
But if you control the JSON and if you want to define more features (e.g. default sorting, hidden columns) then you can start adding those to the JSON as additional metadata items.
Hope that helps or at least gives you some pointers.
By the way, there should generally be no need to use code which manipulates the DOM (e.g. building strings of HTML, or manipulating tags). That is counter to how DataTables is designed to work. You should instead be working with the DataTables object itself, wherever possible.