I have an issue with Datatables populating data from javascript.
If I load the same javascript result from file it works perfectly using the "ajax" attribute in Datatable parameter definition. Have learned that I need to use the "data" attribute instead.
File contains: { "data": [{ "meter": "test", "id": 15, "desc": "testDesc"}] }
This is my function:
$(document).ready(function () {
dataset = {
"data": [{
"meter": "test",
"id": 15,
"desc": "testDesc"
}]
};
//var dataset = [ ['test','15','testDesc'] ];
$('#MeterDataTable').DataTable({
//"ajax": 'DataTables-1.10.7/examples/ajax/data/meterDataJsonDown.txt',
"data": dataset,
"columns": [{
"data": "meter"
}, {
"data": "id"
}, {
"data": "desc"
}]
});
//saveToFile(dataset);
// alert('dataset is '+ dataset);
});
HTML
<table id="MeterDataTable" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>meter</th>
<th>id</th>
<th>desc</th>
</tr>
</thead>
<tfoot>
<tr>
<th>meter</th>
<th>id</th>
<th>desc</th>
</tr>
</tfoot>
</table>
This works but need to format the jSon javascript returned data by removing the column names.
$(document).ready(function () {
var dataset = [
['test', '15', 'testDesc']
];
$('#MeterDataTable').DataTable({
"data": dataset,
"columns": [{
"title": "meter"
}, {
"title": "id"
}, {
"title": "desc"
}]
});
});
Updated: http://jsfiddle.net/j5a390d9/