Build a DataTable at runtime
Build a DataTable at runtime
Karl53
Posts: 72Questions: 29Answers: 0
Is it possible to build a DataTable at runtime?
This is my markup:
<table class="display compact" id="MY-TABLE" style="width:100%">
</table>
This is the JavaScript constructor call:
tblMine = $('#MY-TABLE').DataTable();
When the call to the constructor executes, I get this error:
jquery.dataTables.js:5856 Uncaught TypeError: Cannot read property 'aDataSort' of undefined
I do not know either the row count or number of columns until runtime.
TIA.
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
You can use
columns.title
to build thetable
header. This example derives the column headers from the ajax response. Maybe it will give you some ideas:http://live.datatables.net/huyexejo/1/edit
Kevin
Thank you for the link, @kthorngren
The error I was making was calling the DataTable constructor before building my column array. I was attempting to take a DataTable instance and point its column attribute to my dynamic column structure.
So, my problem was, I didn't know this:
After a DataTable has been initialised any attempt to use these options will result in an error.
The example you pointed to got me on the right track.