1

I have a table with three columns and two rows. I have this code which appends all data to just the first column. I know I am appending all the rows but I am not sure how to change it to add new columns instead.

var jdata = [];
$.getJSON(getURL(), function(json) {
    jdata = json.items;
    var menu = [];
    $.each(jdata, function(key, val){
        menu.push("<tr>");
        menu.push("<td id=''"+key+"''>"+jdata[key].item_code+"</td>");
        menu.push("<tr>");

        menu.push("<tr>");
        menu.push("<td id=''"+key+"''>"+jdata[key].dish_id+"</td>");
        menu.push("<tr>");

    });
    $("<tbody/>", {html: menu.join("")}).appendTo("table");

});

this is what the result looks like:

result

1 Answer 1

1

Like this?

menu.push("<tr>");
menu.push("<td id=''"+key+"''>"+jdata[key].item_code+"</td>");
menu.push("<td id=''"+key+"''>"+ `your data here` +"</td>");
menu.push("<td id=''"+key+"''>"+ `your data here` +"</td>");
menu.push("</tr>");

Also as mentioned by @christophano, you need to close your tr like this, </tr> instead of just this <tr>

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

2 Comments

Don't forget to make the last tr a closing tag - </tr>
Thanks for pointing out the </tr> error. That still didnt fix the issue. It seems i am appending to the table everytime as opposed to appending to the next column. Any idea how i an do that? TIA!

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.