Live data from Hacker News

Do you know that there is an HTML tables API?

christianheilmann.com

21–30 of 206 posts

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

#22
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!

I was just about to comment the same. I’m sure people have a good reason for it (or at leafy _a_ reason), but single-letter variable names always struck me as optimizing for the wrong thing. As someone who likes to program in Haskell, I feel this pain very strongly. :)

Strong BASIC memories. On the Apple IIe, anything after the first two characters of variable names was ignored.

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

#24

The variable naming convention used here could be improved for clarity. I prefer appending `El` to variables that hold DOM elements, as it makes identifiers like `tableEl` clearer and helps avoid ambiguity between variables such as `table` and `row`. Also, the variable named `table` does _not_ actually represent a table element; it would be more accurate to name it `data` or `tableData` to better reflect its purpose.

Probably because I first learned programming with JavaScript and very early started using jQuery, but I've always used prefixed `$` to indicate "This is a DOM element" (started doing this once jQuery stopped being so popular). So the example would be something like this for me:

  let table = [
    ['one','two','three'],
    ['four','five','six']
  ];
  let $body = document.body;
  let $table = document.createElement('table');
  $body.appendChild($table);
Always felt it worked out short and sweet, and as long as you're not refactoring a jQuery codebase, seems to work out well in practice.

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

#25
post #19
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!

Really? The variable name lengths? Not that the code is clearer as: const te = document.createElement('table'); document.body.appendChild(te); [ ['one', 'two', 'three'], ['four', 'five', 'six' ], ].forEach((r, i) => { const re = te.insertRow(i); r.forEach((c, j) => { re.insertCell(j).innerText = c; }) }); My personal stance on short variable names is that they are fine as long as their scope is very limited, which is…

Looks like your code inserts a new row for every cell.

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

#26
post #7

Earlier quoted context omitted.

Tabular data? Tables have a purpose, it's just that people misused them for layout purposes.

To be fair, we did that when there wasn’t much choice for laying out web pages other than using tables. I was there!

It's not that long ago that tables were the only reliable layout tool for HTML emails (mostly due to Outlook supporting only very limited subset of CSS).

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

#27
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!

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.

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

#28
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!

A bike-shedding thread on top as usual.

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

#29
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

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

#30
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!

Looks to me like it's following the JS conventions... Jk, no such thing exists still!!
Post reply on HN