Dynamically set column data from ajax source
Dynamically set column data from ajax source
roche
Posts: 4Questions: 1Answers: 0
Hi, I'd like to know if it is possible to set the column.data from one of the object returned by datatable ajax?
ie.
My JSON:
{
"column ": [ {"data" : "code"}, {"data" : "title"} ],
"data" : [{"code" : "AAA", "title" : "TripleA"}, {"code" : "W", "title" : "DoubleU"}]
}
datatable options
"ajax": "url/to/json"
"columns" : column
I have a function that initialize a bunch of table at once with mostly same setting (beside ajax url and of course, the column and data itself). And when I do that, it throws error "column is not defined", even though the XHR request returned by datatable shows that whole JSON is returned (column and data).
Thanks in advance.
This discussion has been closed.
Replies
You need to post full example, html + table initialization. Otherwise it's hard to understand where exactly you made an error.
Thanks for replying. Here is my function
And my JSON returned from the server is:
HTML:
Everything is ok (it will output the data) if I manually write the column like:
but if I refer to the "column" key instead, it will return (in console) : "column" is not defined. (Also, no data is shown in the table)
It is as if datatable only 'get' the "data" part of the JSON returned, not the whole JSON, which is why I can set the "columns" manually using any key under "data", but not with "column" (because key "column" is sibling to key "data").
Sadly, the only method I've found to do this requires two server calls (one for columns, one for data). Like this:
$.get("/getColumns", function (cols) { $("#table").DataTable({columns: cols}); } );
Allan answers this here: http://www.datatables.net/forums/discussion/3519/getting-column-names-from-ajax-source