Sortable Table Example
The example below illustrates an implementation of the table design pattern for a table with sortable rows.
The example uses HTML table markup for all elements of the table structure, e.g., cells, rows, column headers, and caption.
The aria-sort
attribute is set on the column header of the currently sorted column, and the header text of sortable columns is wrapped in a button
element.
One column, the Address
column is not sortable.
Similar examples include:
- Table Example: A table constructed using ARIA roles to convey table semantics.
- Data Grid Examples: Three example implementations of grid that include features relevant to presenting tabular information, such as content editing, sort, and column hiding.
Example Option
Adds a diamond shaped icon (e.g. ♢
) to the header of each column that can be sorted but is not currently sorted.
Some sortable tables add an icon to unsorted columns to help users distinguish sortable columns from columns that cannot be sorted.
It is important that the shape of the unsorted icon differ in more than just color and size from the icons that indicate sort direction (e.g. '▼' and '▲') so people with visual impairments can easily distinguish them.
Example
Address | ||||
---|---|---|---|---|
Fred | Jackson | Canary, Inc. | 123 Broad St. | 56 |
Sara | James | Cardinal, Inc. | 457 First St. | 7 |
Ralph | Jefferson | Robin, Inc. | 456 Main St. | 513 |
Nancy | Jensen | Eagle, Inc. | 2203 Logan Dr. | 3.5 |
Accessibility Features
- To help screen reader users understand the purpose of the buttons in the column headers, an off-screen description of the sort functionality of the buttons is appended to the caption text. The description is added to the caption instead of to each button to prevent repetitious verbosity that could interfere with understanding of the column titles.
- To enhance perceivability when operating the sort buttons, visual keyboard focus and hover are styled using CSS
:hover
and:focus
pseudo-classes:- To make it easier to perceive when a button has focus, the focus indicator encompasses both the column label and sort direction icon.
- The cursor is changed to a pointer when hovering over the button to help people identify it as an interactive element.
- To make it easier to perceive that clicking either the column label or the sort direction icon will sort the table, hover styles the button and icon in the same way that focus does.
- To make activating sort easier for people with visual and movement impairments who are using a pointing device, the click target is maximized not only by making both the column label and sort icon clickable but also by using CSS positioning and sizing to make the button fill the entire header cell area.
- To ensure the sorting direction icons have sufficient contrast with the background when high contrast settings invert colors, character entities (e.g. '▼' and '▲') are used to indicate the sorting direction.
Keyboard Support
Not applicable: The only interactive elements are HTML button elements, and all their keyboard functionality is provided by browsers.
Role, Property, State, and Tabindex Attributes
Role | Attribute | Element | Usage |
---|---|---|---|
aria-sort="value" |
th |
|
|
aria-hidden="true" |
span |
Removes the character entities used for sort icons from the accessibility tree to prevent them from being included in the accessible name of the sort buttons. |
Javascript and CSS Source Code
- CSS: sortable-table.css
- Javascript: sortable-table.js
HTML Source Code