Ajax call dynamically load datatables
Ajax call dynamically load datatables
navsnavya
Posts: 19Questions: 1Answers: 0
I am trying to load the datatable from json which is returning attached in the css file some sample data
The reason for doing it this way is my columns are dynamic.
All the values in the keys are my column names and values are the data to be displayed against the column in the datatable.
Replies
See the Data Source docs for the supports object data structure. It looks like each ro of your data is a list of obects with key/value pairs:
You will need to convert this to a structure that Datatables supports. For example:
The best place to do this would be at the server script before returning the JSON data to the browser. However you could do it in the getData() function along with building the table headers.
Kevin
can you show me a sample of how i would convert my key value pair to match datatables structure..
i want to build the columns and data dynamically.
because the headers are dynamic td also should populate from json file from getdata
Hi
i was able to manipulate the date to below
with server side code
and js
data = data.replace(/\/g, '');
"[
{"Status":"Deactivated","Action":"No ","Department":"N/A","2021-10":9}
,{"Status":"Draft","Action":"Due in next 30 days","Department":"RM","2021-10":4}
,{"Status":"Draft","Action":"Overdue","Department":"RM","2021-10":1}
,{"Status":"Draft","Action":"Rejected","Department":"RM","2021-10":3}
]"
how ever my datatables does not load yet..please advise.
I'm guessing the problem is wtih
data: data.data,
in line 25. Your data is not in an objectdata
. Change todata: data,
. If this doesn't help then please provide more dtails of what isn't working and what you ahve debugged.Kevin
got it working
had to convert data = JSON.stringify(data);
back to data = JSON.Parse(data);
all falling along now awesome
for anyone struggling with similar problem here is the reference
http://live.datatables.net/fafuyeyu/1/edit
source for object.txt
url: "https://datatables.net/examples/ajax/data/objects.txt",