I am using JQuery DataTables to show tabular data.
I am using the following code for the GET ajax request:
$(document).ready(function() {
var table = $('#data_table').DataTable( {
ajax: {
type: 'GET',
url: url_path,
data: {'data_id':data_id},
success: function(data) {
#here is where I am not sure how to populate the table with the data
for (var i = 0; i < data.length; i++) {
$.each(data[i], function(name, value) {
console.log(name + ": " + value);
});
}
}
}
} );
example console output:
>>obj1_property1: value1 #to go in table row 1 column 1
>>obj1_property2: value2 #to go in table row 1 column 2
>>obj2_property1: value1 #to go in table row 2 column 1
>>obj3_property2: value2 #to go in table row 2 column 2
From the ajax request I get an array of objects and I can get their name and value properties from the code inside the success function. But I can't work out how to populate the DataTable with this data.
The example I have found here: https://datatables.net/examples/ajax/objects.html shows to use:
"ajax": "data/objects.txt",
"columns": [
{ "data": "name" },
But I can't work out how to tailor this to the specific example I have provided - where and how would I specify 'columns' in my example returning an array of objects?