dimanche 2 mai 2021

Is there a Web API to retrieve the cells that naturally belong to a table row (for tables with cells spanning multiple rows)?

Evidently, HTMLTableRow.prototype.cells property allows one to get an array of cells that a table row element contains as its children. But for tables which contain cells that span multiple rows, retrieving cells that can be said to "naturally" apply to that row, becomes a non-trivial operation, especially for tables of arbitrary layout (meaning you can't assume a particular row span for all of its cells).

Am I right that would have to design an algorithm myself for retrieving cells that would naturally belong in a row, as described? To explain what I mean by "naturally belong", consider first the following table with two cells that span two rows each:

/* The stylesheet is inconsequential to the question, it just makes the table more readable */
td {
    border: thin solid silver;
    padding: 1em;
}
<table>
    <tbody>
        <tr><td>A</td><td>B</td><td>C</td><td rowspan="2">D</td></tr>
        <tr><td>E</td><td rowspan="2">F</td><td>G</td></tr>
        <tr><td>H</td><td>I</td><td>J</td></tr>
    </tbody>
</table>

I would like to know, for example, which cell values naturally belong to the second row, if we look at it from "informational perspective". For the above table as table, the expression table.rows[1].cells evaluates to an array of 3, not 4, table cell elements. It seems to merely mirror the amount of children for the row element, no more no less (table.rows[1].children is also a collection of the same 3 elements). That does not express that the cell denoted "D" also applies to the second row, which beget my question -- how to compute the collection of cells that belong to a given row?

Now, I am not saying that what the cells property lets me retrieve is wrong in any way, mind you, just that I need a different view on which cells belong to a row.

For example, for the second row, such "view" would be the ordered collection of cells denoted "E", "F", "G" and "D", since "D" spans to the end of the second row and thus can be said to naturally apply to that row as well.

Is there such an API that would allow me to compute such views, or in the worst case some third party library?




Aucun commentaire:

Enregistrer un commentaire