How to properly format and filter this server-side data?
How to properly format and filter this server-side data?
GoldenJoe
Posts: 6Questions: 2Answers: 0
I have an existing API that returns data in a format like:
"status":"success",
"data":{
"groupA":[
{
"name_first":"Joe",
"age":32
},
{
"name_first":"Geoff",
"age":31
}],
"groupB":[
{
"id":001,
"class":3
},
{
"id":002,
"class":1
}]
}
or in the event of an error:
"status":"failure",
"errorcode":1404,
"errordesc":"invalid user id"
For sake of simplicity, suppose I want a simple table that displays the "name_first" and "age" properties of objects in groupA only. How would I do that, and handle the possible absence of the "data" array in the event of an error?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Currently, I am using an ajax dataFilter, as described in mboo2005's comment here:
https://www.datatables.net/manual/server-side
However, this seems to break search and ordering.
To show data from one of the array's only use
ajax.dataSrc
(in this casedata.groupA
withcolumns.data
set to beage
andname_first
for each column).Its only when you want to combine data from the two arrays that things would become a little bit more tricky. At that point you would need to use the
dataFilter
property (orajax.dataSrc
as a function that will combine the arrays).Allan
Thanks, allan. Why does using dataFilter break the search/sort?
Also, how do you handle data source errors gracefully? As far as I can tell the table will just fail silently. I was thinking of just grabbing the data with jQuery.ajax and then setting the table's data in the success block, but I'm not sure how that would work with a large data set.
Return an
error
property which DataTables will show the value of, or listen forxhr
and check for your error properties.I'd need a link to a page showing the issue to be able to debug it.
Allan
Thanks again for your time allan. I did a lot of searching and figured a bit out:
I had serverSide set to "true", which requires the server to handle sort/search functions. However, I can't find in the documentation exactly how to do that. Can you link me to the appropriate page or an example? For the small data set I am developing with, setting it to "false" restored sort and search functionality.
Return it from where? From the server? Given the error response format above, is my only option to use ajax.dataFilter to reconstruct the JSON, or is there another way to get the table to concatenate the errorcode and errordesc?
I don't have an example that uses
dataFilter
I'm afraid, but those show how server-side processing with the table is expected to work.Yes, as the documentation linked to above notes you can return an
error
property in the JSON (or construct suitable JSON usingdataFilter
in this case).Allan