Live data from Hacker News

Do you know that there is an HTML tables API?

christianheilmann.com

71–80 of 206 posts

Re: Do you know that there is an HTML tables API?

#71
post #36

Earlier quoted context omitted.

Just a catchy title. Not abandoned etc. This is the only API available to manipulate HTML table tags.

Most people use declarative frameworks to build tables, and you could just use `innerHTML` or `append` or any other imperative DOM API to work with tables.

Declarative frameworks build on the imperative DOM API.

Re: Do you know that there is an HTML tables API?

#72

Phew, this post single handedly made me feel old this morning. I started dabbling with the web just over 20 years ago but have mainly been working on the backend the past 10-15 years. I had no clue that nowadays programmers don’t know about this, so I assume it’s supplanted by modern frameworks or modern JS/CSS

I’ve been doing ssr for so long I can’t fathom why you’d build a table using JS.

Re: Do you know that there is an HTML tables API?

#73

Earlier quoted context omitted.

It’s not about the table element, it’s about the API to construct and manipulate that element with a columns and rows interface which is largely superseded by general DOM manipulation.

Exactly like how `getElementById` replaced the direct use of the id as a JavaScript identifier.

I remember there being posts that explicitly discouraged using IDs directly, but I'm not sure of tge reasons anymore. Maybe browser incompatibilities or unclear scoping and confusion with other variables?

Re: Do you know that there is an HTML tables API?

#74
post #50

Earlier quoted context omitted.

Yes, React

Why would it matter what library you use? I'm using React, I do whenever I need to display tabular data, React or no React as no impact on when I'd use .

Just make sure to pass a subscriber to useSyncExternalStore if you decide to venture outside React and use HTMLTable.insertRow, you see react is really smart and won’t let you use this piece of outdated code without punishing you with side effects.

Thanks Vercel & Meta for protecting us.

Re: Do you know that there is an HTML tables API?

#75
post #50
post #34

Abandoned? When? I still use this pretty much everywhere to create HTML tables. Do people use something else now?

Yes, React

React is as orthogonal to DOM manipulation API as it is to letting the browser render tables from HTML.

Re: Do you know that there is an HTML tables API?

#76
post #44
post #15

Interesting, but the JavaScript examples hurt: 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; }) }); Use full words for variable names!

It’s normal to use short names for things with short scopes.

Yes, and the reason for why that's OK is that the context is just few lines. But this is borderline due to the amount of variables.

In just 4 lines you have r, row, t, ri, l, i and c.

The full variables names are so short anyway that personally I'd write them out. Code does evolve and there's a risk of this suffering as a result.

Re: Do you know that there is an HTML tables API?

#77

Phew, this post single handedly made me feel old this morning. I started dabbling with the web just over 20 years ago but have mainly been working on the backend the past 10-15 years. I had no clue that nowadays programmers don’t know about this, so I assume it’s supplanted by modern frameworks or modern JS/CSS

Thank you for confirming I am not... crazy, just old then. I was staring at the code for minutes, trying to spot something unusual that I missed. So, this really just about the ` ` element, or do I actually not see something?

It's about the insertRow() and insertCell() methods and rows[] and cells[] fields on the table element, not the table itself.

Re: Do you know that there is an HTML tables API?

#78
post #34

Abandoned? When? I still use this pretty much everywhere to create HTML tables. Do people use something else now?

Abandonware is a clickbait title insofar as it normally refers to licenses, not standards.

The idea of the author seems to be that this part of the DOM API that could benefit from backwards-compatible additions. So, by "abandoned", he hints at the headroom for building more table capabilities into the platform.

He compares it loosely to the form element API and the additions it received over the last decades.

In the case of tables, I could think of things such as a sorting, filtering API, but I can't tell whether that's what he means.

Re: Do you know that there is an HTML tables API?

#79
post #47

Earlier quoted context omitted.

Personally I'd make everything const instead of let and use for of instead of forEach, but it's like 10 lines of code it doesn't really matter.

Are you sure this old API does the right thing with for…of?

It's only used to iterate the array
Post reply on HN