I find the documentation for DataTables when using AJAX as the data source limited. I've got a method in my controller in an ASP.NET MVC 4 application that returns a simple JSON result, and I'm trying to fill a DataTable with the following:
$.ajax({
type: "GET",
dataType: "json",
contentType: "application/json",
url: "/Chart/Ajax",
success: function (data) {
var returnData = [];
for (var i = 0; i < data.length; i++) {
//makes the data an array of arrays
returnData.push($.makeArray(data[i]));
}
$('#secondary').dataTable({
"aaData": returnData,
"aoColumns": [
{ "sTitle": "DrugClass" },
{ "sTitle": "DrugClassSize" },
{ "sTitle": "DistinctPatients" },
]
});
}
});
Ideally, I wouldn't create the table element until the success callback had fired, but in this instance an empty table element is on the page. With the following code I get the error: Uncaught TypeError: Object [object Object] has no method 'dataTable'
the thing is, I already have a different DataTable already on the page and it works fine. This script is at the very bottom of the page, after the working data table. Why am I getting this error and what's an easy way to get DataTables to play nice with AJAX data sources?