-1
 var tablerows = '';
var dataSet = [];
if (response.hasOwnProperty('records') && response.metadata.record_count > 0) {
    _.each(response.records, function (record) {
        var sku_data = get_api_data(urls.sku.list + "/" + record.skuid, api.access_token);

        if (sku_data != undefined) {
            sku_data.done(function (sku) {

                dataSet.push(sku.records[0].name);


                console.log(dataSet);
            })



        }

    })


    $('#demo').html('<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>');

    $('#example').dataTable({
        "data": dataSet,
        "columns": [
            { "title": "Product" }
        ],
        "aoColumns": [
                null
        ]
    });

How do i dynamically add all the dataSet values to the Datatable.

Please help right now i am getting a blank array.

5
  • are you getting data in dataset? If yes, you need to initialize database once you receive data in dataset. Commented Jul 16, 2015 at 6:55
  • all the code is in a javascript function Commented Jul 16, 2015 at 7:00
  • Not using any database calls calling apis Commented Jul 16, 2015 at 7:00
  • what you get when you console.log(dataSet) Commented Jul 16, 2015 at 7:15
  • Oops... Sorry I wanted to say datatable and not database. Commented Jul 16, 2015 at 9:32

1 Answer 1

1

It is not about jQuery DataTable, you need to use promise to solve your problem

    var tablerows = '';
    var dataSet = [];
    var dataSetPromise = [];
    if (response.hasOwnProperty('records') && response.metadata.record_count > 0) {
        _.each(response.records, function (record) {
            var sku_data = get_api_data(urls.sku.list + "/" + record.skuid, api.access_token);

            if (sku_data != undefined) {
                dataSetPromise.push(sku_data.done(function (sku) {

                    dataSet.push(sku.records[0].name);


                    console.log(dataSet);
                }));



            }

        })

    $.when.apply(null, dataSetPromise).done(function () {
        $('#demo').html('<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>');

        $('#example').dataTable({
            "data": dataSet,
            "columns": [
                { "title": "Product" }
            ],
            "aoColumns": [
                    null
            ]
        });
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this. Though i still get an empty table
what's the output of console.log(dataSet)
is sku_data a jquery Promise? just like the return value of $.get()
yes. It goes twice inside the $.when once the array has 0 values and once it has 35 values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.