I am working on a web application and need to group a set of columns under a single header in a datatable. I am considering two approaches:
Using colspan:
This seems straightforward and keeps the HTML structure simple.
However, I am concerned about how it will scale with more complex data structures and if there are any limitations.
Using nested tables:
This approach might provide more flexibility for complex groupings, but I am worried it could make the code harder to maintain and affect accessibility.
Are there any specific pros and cons of using colspan versus nested tables that I should be aware of?
Are there considerations about structuring my datatable for accessibility and maintainability?
Here are a couple of reasons
You could use nesting tables, but you need to be very careful with events, and with styling. Probably you'll have to remove all styling from the table itself and ensure to style only the outer table or have specific styles for each table/level. If you want to keep accessibility you can use the aria
attributes to simulate the table like aria-colspan and aria-role
If you want more flexibility you can try to generate the table based on the data itself, like write a json2table function or something like that, so that the html is generated based on the data and you have only one source to update (and not have to add a column to the data, then to the HTML everytime the data changes). Also if you care about accessibility you can use other optional aria atributes like aria-level and things like that
Good luck