Datatable not populating with my array dataset js

Datatable not populating with my array dataset js

rscarletrscarlet Posts: 4Questions: 1Answers: 1
edited March 2016 in Free community support

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:

Answers

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49

    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.

  • rscarletrscarlet Posts: 4Questions: 1Answers: 1
    edited March 2016

    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

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49

    Link is 404

  • rscarletrscarlet Posts: 4Questions: 1Answers: 1

    My bad, this should work
    edited link above.

  • rscarletrscarlet Posts: 4Questions: 1Answers: 1
    Answer ✓

    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

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49
    Answer ✓

    Cool, glad you figured it out. Your code looked like it should work, so glad it was something trivial.

This discussion has been closed.