You can use it like this:
var header = context.createTHead();
var row = header.insertRow();
With jQuery it is simpler since it is allowed to create elements directly and add them to the table
var header = $('<thead></thead>');
var row = $('<tr></tr>');
var th = $('<th></th>').html('Cabeça');
row.append(th);
header.append(row);
context.append(header);
If you want to insert many rows/columns with simple javascript, here it is an example to add multiple rows and columns:
var context = document.createElement('table');
context.className = 'table';
var header = context.createTHead();
var row = header.insertRow();
for (var i = 0; i < colunas; i++) {
var th = document.createElement('th');
th.innerHTML = 'Coluna ' + i;
row.appendChild(th);
};
var body = context.appendChild(document.createElement('tbody'))
for (var i = 0; i < linhas; i++) {
var row;
row = body.insertRow();
for (var j = 0; j < colunas; j++) {
row.insertCell().innerHTML = 'foo';
};
};
context.appendChild(body);
document.body.appendChild(context);
I use a
jQuery.each()
to popular the table with data coming byAjax
.– Calebe Oliveira
@Calebeoliveira, I added info on how to do with jQuery. If you post your code here I can adapt the answer better
– Sergio
In my case, simple javascript is more visually clean. It’s good like this.
– Calebe Oliveira