I have following code which gets JSON from WebAPI.(for clarity of the question, I have defined the array as data from web API).
I need the data table to be dynamic and that's why I am creating the table headers at run time.
This works fine, but I do not see any data on data table and get the error:
DataTables warning: table id=tableId - Requested unknown parameter '0' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
var data = [{
"Number": "10031",
"Description": "GYPROCK PLUS RE 10MM 1200X4200",
"FarmLocation": "WH5",
"LocationIn": "LINE_1C2AA",
"Quantity": 18
},
{
"Number": "95844",
"Description": "CEMINSEAL WALLBOARD RE 6MM 1350X3000",
"FarmLocation": "WH5",
"LocationIn": "LINE_1C2AB",
"Quantity": 6
}
];
var $thead = $('#tableId').find('thead');
var tr = $("<tr>");
$thead.append(tr);
$.each(data[0], function(name, value) {
$(tr).append('<th>' + name + '</th>');
});
$('#tableId').DataTable({
data: data,
});
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<table id="tableId" class="table table-condensed responsive">
<thead>
</thead>
<tbody>
</tbody>
</table>