0

I am trying to use javascript and jquery to build an HTML table based on information returned from a database. I have the JSON and the database hit working properly, but I am unable to show the data on my page.

This is code to display the data

var temp;

var init = function() {
    $(_getMarketInfo()).appendTo(document.body);

    // Used for logging
    temp = $(_getMarketInfo());
    console.log(temp);
};

And then to get and process the data

function _getMarketInfo() {
    $.ajax({
        url:'scripts/db_getMarketInfo.cgi',
        dataType:'json',
        success:function(data, testStatus) {
            var html = '';
            html +='<div class="marketInfoHolder">';
            html +='<table class="tbl" border=1>';
            html +='    <tr><th>Market Information</th></tr>';
            html +='    <tr><td>Market</td></tr>';

            for (var i=0;i< data.marketInfo.length; i++){
                html += '<tr><td>' + data.marketInfo[i].market_name + '</td></tr>';
            }

            html +='</table>';
            html +='</div>';

            //used for logging
            console.log(html);

            return html;
        },
        error:function(data, textStatus) {
            alert('There was an error getting market information.');
        }
    });
 };

According to the console logs, the html variable does have proper html code for a table, but then when I look at temp variable, it is logged as []. It appears that, for some reason, the _getMarketInfo() isn't properly returning html to temp.

3 Answers 3

1

just move your appendTo logic inside the success:function(data, testStatus) { of your ajax call

Sign up to request clarification or add additional context in comments.

1 Comment

That was a lot easier than it should have been...Thanks!
0

I would use a custom event that is triggered on the success of the ajax call.

var init;

// bind a custom event to the document that is
// triggered in your ajax success callback
$(document).bind('init', function (html) {
    init = html;

    // Used for logging
    console.log(init);    
});

// run _getMarketInfo in a document ready handler
// because you are accessing the DOM
$(document).ready(function () {
    $(_getMarketInfo()).appendTo(document.body);    
});

// your ajax call function
function _getMarketInfo() {
    $.ajax({
        url:'scripts/db_getMarketInfo.cgi',
        dataType:'json',
        success:function(data, testStatus) {
            var html = '';
            html +='<div class="marketInfoHolder">';
            html +='<table class="tbl" border=1>';
            html +='    <tr><th>Market Information</th></tr>';
            html +='    <tr><td>Market</td></tr>';

            for (var i=0;i< data.marketInfo.length; i++){
                html += '<tr><td>' + data.marketInfo[i].market_name + '</td></tr>';
            }

            html +='</table>';
            html +='</div>';

            // trigger your custom event with
            // the html used as a custom parameter;
            // custom event parameters must be passed
            // in an array
            $(document).trigger('init', [html]);

            // return your html for appending
            return html;
        },
        error:function(data, textStatus) {
            alert('There was an error getting market information.');
        }
    });
 };

Comments

0

You can't return that way from the success function since ajax is run async. Also if you wrap stuff in $() they will become an array since that's the way jQuery works.

var init = function() {
  $.ajax({
    url: 'scripts/db_getMarketInfo.cgi',
    dataType: 'json',
    success: marketsDownloaded,
    error: errorRecieved
  });
},

marketsDownloaded = function(data, status) {
  // build your html here and append it to body
},

errorRecieved = function(data, status) {
}

$(function() {
  init();
}

You could also put the code in somekind of namespacing to clean it up even more

1 Comment

I also recommend jQuery.tmpl if you want to build html a bit easier

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.