-
Notifications
You must be signed in to change notification settings - Fork 1k
Add table sorting #3950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add table sorting #3950
Conversation
Status Update 1/20/21 This provides basic alphabetical or numeric sort using localeCompare and the browser's inferred language. Table header implementation notes
Todo:
What we don’t get today from this implementation
Trying to think of better example content to describe various data types. Following existing orthogonal sort pattern from datatables.net: https://datatables.net/manual/data/orthogonal-data where an implementing user provides a non-formatted value to sort using instead of the content displayed within the cell (for example, omitting "Honorable" or other prefixes, spelling out full names, or providing non-formatted number, currency, or date strings.) This seemed less error-prone than trying to guess what what kind of content the cell represented by its punctuation or character contents. I hesitate to suggest we do too much more with tables, as we're veering into data grid territory, which is cool but a whole other ball of wax: https://www.w3.org/TR/wai-aria-practices/examples/grid/dataGrids.html Resources:
|
Though we discussed, I'm adding some other ideas worth exploring here.
|
Thanks Jared! I added a "todo" section to my original comment based on your suggestions. |
Just modified the npx prettier --write ./src/js/components/table.js |
<tr> | ||
<th scope="row" role="rowheader">Hawaii</th> | ||
<td data-sort-value="50">50th</td> | ||
<td data-sort-value="331844400">Jun. 27, 1959</td> |

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where are these sort values coming from?

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this number is a Unix timestamp. For usage, see the "guidance" section in the first comment of the PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be worth in the guidance adding indicating supporting additional formats like YYYY:MM:DD? Thinking of support for tools where Unix timestamps might be as readily available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've deferred parsing dates from strings because they're finicky and would probably have weird issues across timezones unless we included a library like Moment.JS or one of its successors (which I am loathe to do).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I spent some time this morning building with this component in a React application and pulling data from https://www.ftc.gov/developer/api/v0/endpoints/do-not-call-dnc-reported-calls-data-api.
Implementation
- I was able to get up and running within just a few minutes following the guidance from the documentation.
table.js
slice.call()
is good for those NodeLists- readability is good/clear
- great comments
spec
- these assertions are deprecated, can we update? ie.
equal()
->strictEqual()
etc.
Everything works well for me, nice job
@scottqueen-bixal @mejiaj Updated those deprecated assertions in this PR. Also, updated settings to use |
Adds table sorting to existing table component.
Component / Fractal Federalist Link: https://federalist-3b6ba08e-0df4-44c9-ac73-6fc193b0e19c.app.cloud.gov/preview/uswds/uswds/accelerator/3935-sortable-table/components/detail/table--sortable.html
USWDS Site Federalist Link: https://federalist-ead78f8d-8948-417c-a957-c21ec5617a57.app.cloud.gov/preview/uswds/uswds-site/accelerator/3935-table-sorting/components/table/
Addresses #3935
See also: #1643 , #1079
Draft usage documentation
The following will be added to existing table page here (subject to change):
Guidance
Enable sort where useful. Add row sorting to individual columns of long tables where the data can be logically ordered either alphabetically or numerically. To activate row sorting, add the
data-sortable
attribute to the table header element (<th>
) of any column with sortable data, and insert an element with thearia-live=”polite”
attribute and the class “.usa-table__announcement-region” immediately following the table.Set a default sort column and direction. To sort a table’s rows by a specific sortable column on load, add the attribute
aria-sort
equal to a sort direction such as “ascending” or “descending” to that column header. For example,<th data-sortable aria-sort="ascending" scope=”col”>
.Provide raw values for cells with formatted number content. If you have formatted your cell content for display (such as using percent, currency, or comma formatting) or if your cell content is non-numeric but should be sorted in a numeric order (such as months, days of the week, or dates), then provide a numeric-sortable value in a
data-sort-value
attribute on each table cell. For example:Numbers without currency or comma formatting:
<td data-sort-value="132773.54"> $132,773.54 </td>
Percentages or fractions, converted to decimal:
<td data-sort-value="0.943"> 94.3% </td>
Months, weekdays, or other orderable text:
<td data-sort-value="2”> February </td>
Dates, in Unix timestamp:
<td data-sort-value="327092400"> Aug. 21, 1959 </td>
Don’t use row sorting with merged cells. Sorting will not work properly if your table contains
colspan
orrowspan
attributes on the cells.Don’t use row sorting with the mobile stacked variants. With these variants, the column headers at the top of the table do not appear at narrow widths, and are instead moved into the cell content in each row as prefixing headers.
Accessibility
Add an aria-live region to the page when enabling row sorting. An
aria-live
region immediately following the<table>
element automatically announces when the sort state changes for visitors using screen readers, but it must be added to the HTML document before load:Enabling row sorting automatically adds
aria-label
attributes to the sortable column headers and their toggle sort buttons via JavaScript. These labels are updated to reflect each column’s current sort state (ascending, descending, or unsorted) whenever sort changes. You don’t need to apply these to sortable headers yourself.Implementation
(these will be added to the existing table)
$theme-table-sorted-border-color
"accent-cool-darker"
$theme-table-sorted-header-background-color
"accent-cool-light"
$theme-table-sorted-background-color
"accent-cool-lighter"
$theme-table-sorted-stripe-background-color
"blue-cool-10v"
$theme-table-sorted-icon-color
"ink"
$theme-table-unsorted-icon-color
"base"
Other
npm test
and make sure the tests for the files you have changed have passed.