using JSON object in datatable?

using JSON object in datatable?

klauspeterklauspeter Posts: 2Questions: 0Answers: 0

Please let me know; I have a php file that is pulling data from an API and is using some fragments of the array here and there and some data should be used in the datatable. My problem is when I´ve the datatable embed and its manipulating 200 lines of data the site is really getting slow: thats the reason why I wanna try using datatables with JSON instead of looping through the arrays to create the dom that is manipulated by the datatable plugin.

BUT I have complety no idea how to hand over the JSON object to the datatable without creating a php file that is pulling the data; because I am using data pulling from an API and I want less API pulls and just format my data with php to create that JSON object; thats easy but I really have no idea, really no clue how to use it in the datatable, every example I found was pulling the data from a file: I dont wanna create one, just wanna use the data from the API I already have.

So please let me know where I find a hint, so I could try to get it to work, I dont think that it could be that hard to get this to work but I am very sorry but I dont have any idea how to do it. So please gimme hint, so I could try my best.

Replies

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    You can use ajax to fetch data directly from an API. Datatables expects the row data to be in an array as explained in the ajax docs. The ajax examples might be pulling from a file on a server but it could be an API.

    The data source doesn't really matter but the format of the response data does. Can you post an example of the JSON data?

    Kevin

  • klauspeterklauspeter Posts: 2Questions: 0Answers: 0

    Dear Kevin,

    my problem is solved, for everyone who might have the same problem the solution is quite easy.

    Instead of using a second php file to create the JSON file and call the API twice; would lead to doubled file requests and API-Calls, the solution is so simple, that it might hurt a little bit...

    https://datatables.net/examples/data_sources/js_array

    Just write all the data from the API call into an js array and hand it over to the datatable; no need for a second php file that fetches the JSON file, so its a simple one file, one API call and its so much faster as using inline code.

    A quick localhost test says; 230k lines of data (each line having 6 set of data -> 1.4 million data sets) is running fast as 18k lines inline, for me with a maximum of 500 lines it is fast enough and it exactly does what I wanted to achieve.

Sign In or Register to comment.