How to best inject data to empty datatable after page has loaded?
How to best inject data to empty datatable after page has loaded?
Sorry, but I can't quite figure this one out using the documentation (Likely because I'm somewhat new to a lot of this). So I have a page that will be a wizard of sorts. The first section of the page shows in a div and collects some input files and parameters in a form. There is a next button that submits that data via ajax to a servlet for processing. The servlet returns that data via JSON to the web page. At the same time I unhide the div containing my DataTable (which is initially empty). At the same time I unhide the table element on the page, I want to somehow "inject" this JSON object I got from the ajax call in to the datatable. Ideally, the table would not be locked to a certain number of columns (some results will have 4 columns, others 6).
I basically want to do something like:
function theOnClickFunction( jsonData ) {
var resultTable = $('#result_table').DataTable();
resultTable.data = jsonData;
resultTable.draw();
};
What is the best way to do this? There is quite a bit of data being sent in the ajax call (~50kb), and I couldn't quite find a way to make the ajax functionality of datatables work for me. I would prefer doing the ajax separately and just pass the new data to the table on the fly. Thank you in advance!
-Tim
This question has accepted answers - jump to:
Answers
If you need variable number of columns, then I think you'll have to build the table in jQuery before you turn it into a DataTable:
Or something like that. Then add the ajax data via initialization, something like:
Alternatively you can use
rows().add()
to add the ajax data, if you like/need.I've made a little live.datatables.net demo for you as well. Hope that helps!
Thanks, rhino! I appreciate the help!
Since you're on the Java platform, you may want to also look at the JED website for further examples. http://jed-datatables.ca/jed/