0

I have some HTML, JSON, and Javascript. I want to render a table consisting of 3 rows (as of JSON). Problem is that all my data comes out in TD's in one tr

<table>
<tbody>
    <tr>
        <td>car</td>
        <td>1000</td>
        <td>2000</td>
        <td>1500</td>
        <td>1000</td>
        <td>house</td>
        <td>500</td>
        <td>600</td>
        <td>700</td>
        <td>800</td>
        <td>internet</td>
        <td>100</td>
        <td>200</td>
        <td>2350</td>
        <td>1940</td>
    </tr>
</tbody>
</table>

I want

<table>
<tbody>
    <tr>
        <td>car</td>
        <td>1000</td>
        <td>2000</td>
        <td>1500</td>
        <td>1000</td>
    </tr>
    <tr>
        <td>house</td>
        <td>500</td>
        <td>600</td>
        <td>700</td>
        <td>800</td>
    </tr>
    <tr>
        <td>internet</td>
        <td>100</td>
        <td>200</td>
        <td>2350</td>
        <td>1940</td>
    </tr>
</tbody></table>

Here is my code HTML

<div class="calc"></div>

JSON

{
"item": [
    {
        "name": "car",
        "cost": [1000, 2000, 1500, 1000]
    },
    {
        "name": "house",
        "cost": [500, 600, 700, 800]
    },
    {
        "name": "internet",
        "cost": [100, 200, 2350, 1940]
    }
]
}

Javascript

$.getJSON("calc.json", function(data) {

    var $table = $('<table/>'),
        $row = $('<tr/>');

    for(var i = 0; i < data.item.length; i ++){
        $row.append( '<td>' + data.item[i].name + '</td>' );

        for (var cost = 0; cost < data.item[i].cost.length; cost ++) {
            $row.append( '<td>' + data.item[i].cost[cost] + '</td>' );
        }
    }

    $('.calc').append($table.append($row));

});

EDIT a less code intensive version

$.getJSON("calc.json", function(data) {

    var $table = $("<table/>");

    $.each(data.item, function(k,v) {
        var $row = $("<tr/>");
        $row.append("<td>" + v.name + "</td>");

        $.each(v.cost, function(k,v) {
            $row.append("<td>" + v + "</td>");
        });

        $table.append($row);

    });

    $(".calc").append($table);

});
6
  • Please, format the 1st html code Commented Jul 21, 2014 at 9:04
  • ok. Should be more readable now Commented Jul 21, 2014 at 9:07
  • Currently your JS code produce the 1st html? Commented Jul 21, 2014 at 9:08
  • yes. and i want the 2. html Commented Jul 21, 2014 at 9:09
  • 2
    The outer FOR loop needs to be creating the <tr> tags. I can't produce a complete answer right now, but that might get you going. Commented Jul 21, 2014 at 9:11

3 Answers 3

3

Hi i have changed you function a little but now you can use below given function:-

$.getJSON("calc.json", function(data) {

        var $table = $('<table/>');


        for(var i = 0; i < data.item.length; i ++){
           var $row = $('<tr/>');
            $row.append( '<td>' + data.item[i].name + '</td>' );

            for (var cost = 0; cost < data.item[i].cost.length; cost ++) {
                $row.append( '<td>' + data.item[i].cost[cost] + '</td>' );
            }
           $table.append($row);
        }

        $('.calc').append($table );

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

2 Comments

Thanks everyone. makes perfect sense, and all your examples worked.
I get this i even better. About 100 charachter less code. See edit in original post
3
var $table = $('<table/>');

for(var i = 0; i < data.item.length; i ++){
    var  $row = $('<tr/>');
    $row.append( '<td>' + data.item[i].name + '</td>' );

    for (var cost = 0; cost < data.item[i].cost.length; cost ++) {
        $row.append( '<td>' + data.item[i].cost[cost] + '</td>' );
    }
    $table.append($row)
}

$('.calc').append($table);

Demo

Comments

1

Problem is this because you have declared only one row for all Items.

Create new Row for Each item.

change your function like bellow.

 $.getJSON("calc.json", function(data) {

    var $table = $('<table/>');

    for(var i = 0; i < data.item.length; i ++){
       var $row = $('<tr/>');  //new Row for current ITEM
       $row.append( '<td>' + data.item[i].name + '</td>' );

       for (var cost = 0; cost < data.item[i].cost.length; cost ++) {
          $row.append( '<td>' + data.item[i].cost[cost] + '</td>' );
       }
       $table.append($row); // append current row
    }
    $('.calc').append($table);
});

Comments

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.