Accessible Sortable Tables
Giving users the ability to sort data tables is useful for everyone. We should ensure they are coded correctly. In the example below, you will learn about the
grid role that you should use for these tables, and how the UI for the sorting routines ensure partially-sighted and blind users know that a sort has been successul.
The script for this table was based on the excellent sortable table example from Deque University.
Example code explanation
Below is the HTML of the above example. Use the dropdown to highlight each of the individual steps that makes the example accessible.
- as an ES6 module using Webpack.
- as a native ES6 module within the browser.
If you haven't done so already, choosing which you should use is obviously a major architectural decision. Here are a few articles that will help you decide:
- Joe Honton discusses that With ES Modules and HTTP/2 You May Not Need Webpack Anymore
- Stack Overflow has a really good thread about Webpack vs ES6 modules as well.
Important Note On The CSS Classes Used In This Module:
This module requires specific CSS class names to be used in order it to work correctly.
These CSS classes begin with
deque-table-sortable__. Please see the documentation above to see where these CSS classes are inserted.
Using NPM/Webpack to load ES6 Modules:
enable-a11yNPM project. Developers who are using webpack should know how to configure webpack to import the enable modules correctly.
You can use the module like this:
// import the JS module import sortableTables from '~enable-a11y/js/modules/sortable-tables'; // import the CSS for the module import '~enable-a11y/css/sortable-tables'; // How to initialize the sortableTables library sortableTables.init(); // If you are adding a new instance of this component after page load, // then do the following (where el is the DOM node of the newly created // element, which contains the CSS class .pagination__table): el.add();
Alternatively, if you are using LESS you can include the styles in your project's CSS using:
Using ES6 modules natively.
This is the method that this page you are reading now loads the scripts.
- Grab the source by either using NPM, grabbing a ZIP file or cloning the enable source code from github.
If you want to load the module as a native ES6 module, copy
css/sortable-tables.cssfrom the repo and put them in the appropriate directories in your project (all JS files must be in the same directory).
Load the CSS in the head of you document:
<html> <head> ... <link rel="stylesheet" href="path-to/css/sortable-tables.css" > ... </head> <body> ... </body> </html>
Load your scripts using:
<script type="module"> import sortableTables from "path-to/sortable-tables.js" sortableTables.init(); </script>
js/modules/es4directory instead of the