Yarin
Yarin

Reputation: 183829

Dynamically create template-based table cells based on data

How does one dynamically create table cells based on a dataset? Is Javascript/ajax required for this? Is it possible to use a template div for the cells?

Just looking for some direction.

Upvotes: 0

Views: 549

Answers (1)

Harmen
Harmen

Reputation: 22436

If you want to dynamically create table cells, you need to manipulate the DOM, so you need Javascript.

An example, which will add a new row to a table:

var table = document.getElementById('[tablebodyelement]'),
    row = document.createElement('tr'),
    data = ["cell1", "cell2", "cell3"];

for(var i=0; i<row.length; i++){
  var cell = document.createElement('td');
  td.appendChild(document.createTextnode(row[i]));
  row.appendChild(td);
}

table.appendChild(row);

Upvotes: 1

Related Questions