I have a datatable in my web application, and I need to group a set of columns under a single header. I am trying to determine the best practice for achieving this.
Here are the two approaches I am considering:
-
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.
Which method is considered a best practice for grouping columns in a datatable? Are there any specific pros and cons of using colspan
versus nested tables that I should be aware of?
Is there a better way to structure my datatable for accessibility and maintainability?
2
Answers
In my case im use like this, but if you export as csv then have add second head in your js
TL;DR Use colspan
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-roleIf 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