1

Background: I thought that I was going to end up with a dynamically created table that had the same number of <TR> tags as entries in the JSON data.

Result: The loop is only creating one table TR and putting all the data in one row.

Below is a snippet of the loop and table creation.

var tbl = document.createElement("table");
  var tblBody = document.createElement("tbody");
  var row = document.createElement("tr");
    var tdname = document.createElement('td');
    var tddate = document.createElement('td');
    var tdassigned = document.createElement('td');


    for (var i in data) {

    console.log("Hello world!" + i);
    tdname.appendChild(document.createTextNode(data[i].name));
    tddate.appendChild(document.createTextNode(data[i].date));
    tdassigned.appendChild(document.createTextNode(data[i].assigned));
    row.appendChild(tdname);
    row.appendChild(tddate);
    row.appendChild(tdassigned);


    }
    tblBody.appendChild(row);

    tbl.appendChild(tblBody);
    document.getElementById("tasklist").appendChild(tbl);

Question: Do I need to create a unique row variable for each loop?

al76
  • 754
  • 6
  • 13
Denoteone
  • 4,043
  • 21
  • 96
  • 150

3 Answers3

1

There were a couple of issues:

  1. You're appending the text to the td, without clearing them. You can instead do document.createElement('td') within the loop for each of your data points.
  2. You need a row for each iteration of data, this can also be done within the loop.
  3. Please use let and const in place of var. Please see this post for more information.

Try something like this:

const data = [{
    name: 'name1',
    date: 'date1',
    assigned: 'assigned1'
  },
  {
    name: 'name2',
    date: 'date2',
    assigned: 'assigned2'
  },
  {
    name: 'name3',
    date: 'date3',
    assigned: 'assigned3'
  },
];

let tbl = document.createElement("table");
let tblBody = document.createElement("tbody");
data.forEach(d => {
  let row = document.createElement("tr");
  row.appendChild(document.createTextNode(d.name));
  row.appendChild(document.createTextNode(d.date));
  row.appendChild(document.createTextNode(d.assigned));
  tblBody.appendChild(row);
});

tbl.appendChild(tblBody);
document.getElementById("tasklist").appendChild(tbl);
<div id='tasklist'></div>

Hope this helps,

Miroslav Glamuzina
  • 4,472
  • 2
  • 19
  • 33
0
  1. If data is an array of objects, start with an array method like .map() or .forEach() (demo uses .foreach() since a new array isn't really needed to make a table).
  2. That will iterate through the array using .insertRow() for each object. Now convert each object into an element of it's values with Object.values() or Object.keys() (demo uses Object.values() which saves a step).
  3. Now iterate through the array of values with .forEach() or .map() and .insertCell() then .textContent the values to the cells.

let data = [{
    name: 'John Doe',
    date: '1/1/19',
    assigned: 'A'
  },
  {
    name: 'Jane Doe',
    date: '2/1/819',
    assigned: 'B'
  },
  {
    name: 'Darth Vader',
    date: '3/11/19',
    assigned: 'C'
  },
  {
    name: 'Obi Wan',
    date: '4/20/19',
    assigned: 'D'
  }
];

const frag = document.createDocumentFragment();
const tbl = document.createElement("table");
const tblB = document.createElement("tbody");
tbl.appendChild(tblB);
frag.appendChild(tbl);

data.forEach((obj, idx) => {
  let row = tblB.insertRow();
  Object.values(obj).forEach(val => {
    let cell = row.insertCell();
    cell.textContent = val;
  });
});

document.body.appendChild(frag);
zer00ne
  • 41,936
  • 6
  • 41
  • 68
-1

I just saw your comment that you prefer vanilla js. Here's the jquery way if interested.

var data = [
    { Column1: "col1row1", Column2: "col2row1" },
    { Column1: "col1row2", Column2: "col2row2" }
];

$("#tasklist")
    .append($('<table>')
        .append($('<tbody>')));

var tbody = $("#tasklist > table > tbody");

$.each(data, function (idx, item) {
    $(tbody).append($('<tr>')
        .append($('<td>')
            .text(item.Column1)
        )
        .append($('<td>')
            .text(item.Column2)
        )
    );
});
tnk479
  • 672
  • 11
  • 26