2

I'm trying to append items from a 2D array into a table, but everything is appearing in the one column on the left. How can I separate tableDataArr[1] to start loading into the next column across?

Javascript

let names = []; //populated dynamically
let language = []; //populated dynamically 
let tableDataArr = [names, language];

function renderData() {
  for (let i = 0; i < tableDataArr.length; i++) {
    tableDataArr[i].forEach(j => {
      let newRow = document.createElement("tr");
      newRow.className = "row";
      newRow.innerHTML = `<td class='cell'>${j}</td>`;
      leftTable.appendChild(newRow);
    });
  }
}

HTML

<div class='left-tbl-wrap'>
  <table class='table' id='left-table'>
    <tr class='row'>
      <th class='th'>Name</th>
      <th class='th'>Language</th>
      <th class='th'>Latest tag</th>
      <th class='th'><span class='delete'></span></th>
    </tr>
  </table>
</div>
0

2 Answers 2

2

For each iteration add name and language to the same row, then insert that row into table.

I added some elements in names and languages array to demostrate

let names = ["name1", "name2", "name3"]; //populated dynamically
let language = ["language1", "language2", "language3"]; //populated dynamically 
let tableDataArr = [names, language];
const leftTable = document.querySelector("#left-table");

function renderData() {
  tableDataArr[0].forEach((j, i) => {
    let newRow = document.createElement("tr");
    newRow.className = "row";
    newRow.innerHTML = `<td class='cell'>${j}</td><td class='cell'>${tableDataArr[1][i]}</td>`;
    leftTable.appendChild(newRow);
  });
}

renderData();
<div class='left-tbl-wrap'>
  <table class='table' id='left-table'>
    <tr class='row'>
      <th class='th'>Name</th>
      <th class='th'>Language</th>
      <th class='th'>Latest tag</th>
      <th class='th'><span class='delete'></span></th>
    </tr>
  </table>
</div>

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

5 Comments

Thanks so much! This is exactly what I was trying to achieve. Also, can I ask why you changed getElementByID to querySelector?
i didnt see how you where selecting the table, im just more used to using query selector thats all
Hey Chris, would you mind taking a look at my latest post (about another problem with this table), since you helped me work out this one? Cheers!! stackoverflow.com/questions/52359079/…
it seems like you already have a decent answer by miozzz, i would have suggested the same solution. You could also use async/await, but since you have a loop of fetch, using Promise.all has better performance. He did make a minor mistake, just change Promise.all(ps) to Promise.all(psArr) and it worked for me.
Thanks Chris! Promise.all was the way to go.
0

It should be like that

function renderData() {
    for (let i = 0; i < tableDataArr.length; i++) {
      let newRow = document.createElement("tr");
      tableDataArr[i].forEach(j=> {
        newRow.className = "row";
        newRow.innerHTML = `<td class='cell'>${j}</td>`;
        leftTable.appendChild(newRow);
      });
    }
}

Because you may do not need to iterate table row in the second loop. If you do like that it will be add each row with only one table data.

1 Comment

Hmm this helped get the language array into the next column, but it only worked for the last item of name and language. How can I have all items from the array listed this way? Thanks for your help

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.