DEV Community

Cover image for Abandonware of the web: did you know that there is an HTML tables API?
Christian Heilmann
Christian Heilmann

Posted on

Abandonware of the web: did you know that there is an HTML tables API?

When people turn data into HTML tables using JavaScript, they either use the DOM methods (createElement and the likes), but most of the time just append a huge string and use innerHTML, which always is a security concern. However, did you know that HTML tables also have an old, forgotten API? Using this one, you can loop over tables, create bodies, rows, cells, heads, footers, captions an summaries (yes, HTML tables have all of those) and access the table cells. Without having to re-render the whole table on each change. Check out the Codepen to see how you can create a table from a nested array:

let table = [
  ['one','two','three'],
  ['four','five','six']
];
let b = document.body;
let t = document.createElement('table');
b.appendChild(t);
table.forEach((row,ri) => {
  let r = t.insertRow(ri);
  row.forEach((l,i) => {
    let c = r.insertCell(i);
    c.innerText = l;  
  })
});
Enter fullscreen mode Exit fullscreen mode

You can then access each table cell with an index (with t being a reference to the table):

console.log(t.rows[1].cells[1]);
// => <td>five</td> 
Enter fullscreen mode Exit fullscreen mode

You can also delete and create cells and rows, if you want to add a row to the end of the table with a cell, all you need to do is:

t.insertRow(-1);
t.rows[2].insertCell(0);
t.rows[2].cells[0].innerText = 'foo';
Enter fullscreen mode Exit fullscreen mode

There are a few things here that are odd - adding a -1 to add a row at the end for example - and there seems to be no way to create a TH element instead of a TD. All table cells are just cells.

However, seeing how much of a pain it is to create tables, it would be fun to re-visit this API and add more functionality to it. We did add a lot of things to HTML forms, like formData and the change event, so why not add events and other features to tables. That way they'd finally get the status as data structures and not a hack to layout content on the web.

Top comments (3)

Collapse
 
moopet profile image
Ben Sinclair • Edited

Using negative numbers to index from the end of an array is fairly common in other languages. I'm actually pretty surprised JS never adopted it.

Is this lacking things like head and body awareness because it's (like you said) just too old?

Collapse
 
maxart2501 profile image
Massimo Artizzu

JavaScript has actually adopted negative indexes for a while now, and in some cases they're even supported in older API.

But it's mostly a spottish support. In those cases where you don't have it, you either convert to an array (using Array.from or similar) or apply the method on a different kind of iterable instead:

const sections = document.querySelectorAll('section');
const lastSection = Array.prototype.at.call(sections, -1);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
maxart2501 profile image
Massimo Artizzu • Edited

I knew about these APIs, but I think at W3C they're too embarassed to have them around that they prefer not to think about them at all πŸ˜†

After all, we have all sorts of newer APIs that we can use now that didn't exist when these table methods have been created. Even the HTML specification is way more lax than it was at the time of XHTML 1.0.