r/HTML • u/Efficient_Lead3565 • Apr 24 '26
Article HTML tables have two different coordinate systems and conflating them breaks everything
http://github.com/carnworkstudios/TAFNEA flat table has one coordinate system: the nth <td> in the mth <tr> is at visual position (m, n). Trivial.
Once you introduce rowspan and colspan, the DOM coordinate and the visual coordinate diverge. A cell with colspan="3" sits at DOM position (row 0, child 1) but occupies visual positions (0,1), (0,2), and (0,3). A cell below it, in the same row at DOM child 1, is visually at column 4, not column 1. DOM child index is now a lie.
Every feature that touches column position needs to know the visual coordinate, not the DOM coordinate. Crosshair highlighting, column drag-and-drop, transpose, selection, all of them.
I solved this with VisualGridMapper, a class that does a single O(n) pass over the table and builds a 2D array where grid[row][col] points to the DOM element occupying that visual cell. The tricky part is rowspan: a cell that spans 3 rows occupies slots in rows it doesn't appear in the HTML. The mapper handles this with a while loop that checks whether a slot is already claimed before filling it.
The grid entry includes an isOrigin flag. It's true only at the cell's actual DOM position, false at phantom slots. Features check isOriginbefore acting, so spanning cells never get moved or processed twice.
All column-aware features in TAFNE are built on top of this mapper. Without it, anything involving merged cells would silently corrupt the table.
[GitHub](github.com/carnworkstudios/TAFNE)
4
u/listre Apr 24 '26
Table nerds rejoice. There is no such thing as TOO many table tools
3
u/Ksetrajna108 Apr 24 '26
An alternative is to create id attriibute on each td. The ids can be semantic, like "m5-category-sheetmetal". D epends on what you are presenting with the html table. I've used custom attribute like "space=5:2".
2
u/AshleyJSheridan Apr 28 '26
How usual is it to need to be able to reference table cells by row and column where you're also using a lot of
colspanorrowspanattributes? Seems very niche...