Datatable not populating with my array dataset js
Datatable not populating with my array dataset js
rscarlet
Posts: 4Questions: 1Answers: 1
Hello,
I'm trying to populate my datatable with a set of array data that is generated with websql..
However no data seems to be populating.
My code is as below.
var dataset = new Array();
console.log(dataset);
var table = $('#testo').DataTable();
/** Table creation **/
db.transaction(function (tx) {
............ (creating websql data line of codes)
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM GAMES', [], function (tx, results) {
dataset = [[ results.rows.item(0).name, results.rows.item(0).platform, results.rows.item(0).genre, results.rows.item(0).status]]
console.log(dataset);
for (i = 1; i < len; i++){
var addData = [ results.rows.item(i).name, results.rows.item(i).platform, results.rows.item(i).genre, results.rows.item(i).status]
dataset.push(addData);
}
/* lines of code in this comment solve it, removing document.ready...
$('#testo').DataTable({
"data": dataset,
"columns": [
{ "title": "Name" },
{ "title": "Platform" },
{ "title": "Genre" },
{ "title": "Status" }
]
}); */
console.log(dataset);
console.log(dataset[2]);
}, null);
});
}
}
$(document).ready(function() {
$('#testo').DataTable({
"data": dataset,
"columns": [
{ "title": "Name" },
{ "title": "Platform" },
{ "title": "Genre" },
{ "title": "Status" }
]
});
} );
<table id="testo" class="display" cellspacing="0" width="100%"></table>
Console outputs:
[]
[Array[4]]
[Array[4], Array[4], Array[4] ........... continues
["XCOM", "PC", "Tactical RPG ", "Completed"]]
This question has accepted answers - jump to:
This discussion has been closed.
Answers
You have already initialized DataTables at line 3 and you are trying to do it again at line 30? I think this may be your problem.
Comment out line 3 and try it.
Hi jr42.gordon.
Thank you for your input.
However it doesnt seem to have any effect when i remove it.
Below is the link to the html source that im working on.
https://www.dropbox.com/s/p1q5kc8rh04gpyy/test.html?dl=0
Link is 404
My bad, this should work
edited link above.
I manage to solve it in a weird way. For some reason, the dataset is still undefined when document.ready kicks in..
I swap over the the datatables initialisation into the javascript and it works..
Weird way but the results is what i wanted... so its settled.. thank jr42.gordon
Cool, glad you figured it out. Your code looked like it should work, so glad it was something trivial.