Table with Expando Rows


0

I regularly work on projects with HTML tables that have been pushed to the edge with styles, scripts, and widget features. A common pattern is where rows are hidden until the user opts to show them. Unfortunately, the pattern is often over-complicated with unnecessary script and styles that regularly break the table semantics or fail to work across all contexts.

Typically there are only two things you as an author need to do for a row expando to retain table semantics (other than using a contiguous

, of course):

  1. Make sure you use the table-row CSS display property instead of block, and
  2. Use a disclosure widget as your toggle.

Example

You can visit the pen directly to fiddle with it, or view in debug mode to test it in your favorite AT (assistive technology).

Note this example is not responsive.

See the Pen
Table with Expando Rows
by Adrian Roselli (@aardrian)
on CodePen.

Disclosure Widget

The disclosure widget is a native HTML

[…] […]

The aria-controls value is a space-separated list of the ids of the rows you are affecting. Where aria-controls is supported, confirm that its announcement in screen readers (JAWS only today) is not too verbose for your users. If it is, you may want to remove it altogether. Its absence won’t result in a barrier, but it may make the widget easier to use for some users when useful screen reader support starts to appear.

The CSS

The easiest way to ensure the programmatic state of the button matches the visual styles is to use attribute selectors such as button[aria-expanded="true|false"]. The styles for both example tables only change the SVG.

.cell button[aria-expanded="true"] svg {
  transform: rotate(90deg);
}
.row button[aria-expanded="true"] svg {
  transform: rotate(180deg);
}

Your own styles will likely vary, of course.

The Script

The function is nothing special. It takes a list of ids and feeds them into a query selector, and it takes the id of the button as well. It checks then flips the aria-expanded value on the button while swapping the value of the class between shown and hidden.

If I could use adjacent sibling selectors (keying off the aria-expanded value) to toggle the visibility of the rows I would. The HTML table structure precludes that, which is the only reason I am using a class to do the work.

This is not production-ready script. It does, however, take Internet Explorer into account by not using classList.replace, which IE does not support.

function toggle(btnID, eIDs) {
  // Feed the list of ids as a selector
  var theRows = document.querySelectorAll(eIDs);
  // Get the button that triggered this
  var theButton = document.getElementById(btnID);
  // If the button is not expanded...
  if (theButton.getAttribute("aria-expanded") == "false") {
    // Loop through the rows and show them
    for (var i = 0; i

At page load, the rows in my example are not hidden with inline CSS. Whether you hide or display them on initial load in the name of Progressive Enhancement is up to you and your use case. Either one can be a valid approach, but account for it in your function as well.

Tables

I made two tables so you could see two ways this might work. The full-row disclosure widget shows the text that is also announced to screen readers, and it provides a much larger hit area. As a column-spanning cell it can complicate table navigation for novice screen reader users but is much easier to find — you can stumble across it from any column.

The other example has the disclosure widget in its own column, arguably making it easier to avoid. It also warrants its own column header. The value is Toggle and I use a well-tested technique to visually hide it (partly because NVDA does not support the abbr attribute on

The CSS

The CSS for the two classes that adjust the display of the row is critical. If you use display: block instead of display: table-row then the browser drops all the semantics for the row and assistive technology cannot navigate it. See my post Tables, CSS Display Properties, and ARIA for more detail.

tr.shown, tr.hidden {
  background-color: #eee;
  display: table-row;
}
tr.hidden {
  display: none;
}

Screen Readers

Note that while a screen reader will not announce how many new rows are added (hence the accessible name to manage that expectation), once the new rows are visible the screen reader factors them into the total row count and user’s position within the table.

It is also important to ensure any new rows you add come after the control that makes them appear in the source order. Otherwise a screen reader use cannot be expected to know where they have appeared, let alone navigate around the table looking for them.

I have embedded some videos showing how a screen reader user might navigate the expando feature.

JAWS / IE11

Using JAWS 2018 and Internet Explorer 11 to navigate the table with a disclosure widget that lives in a single cell. Note that JAWS is visually highlighting the first cell in a row that has content, not the blank cells. It also does not highlight the buttons.

NVDA / Firefox

Using NVDA 2019 and Firefox 69 beta to navigate the table with a disclosure widget that lives in cell that spans all columns. NVDA only visually highlights the interactive controls (the red border around the spanning button).

VoiceOver / Safari

Using VoiceOver and Safari on macOS 10.14.5 to navigate the table with a disclosure widget that lives in a single cell. You may notice that the

Mary Shelley
). See the visually-hidden class to steal the styles.

The HTML for the row is not complex. Just an id and a class, with the latter toggled via script.

    
disappears when the disclosure is opened (it moves to below the table); I have no idea why and I have not taken time to dive into it.


Like it? Share with your friends!

0
admin