Page MenuHomePhabricator

Table: enable sorting of rows
Closed, ResolvedPublic5 Estimated Story Points

Description

Summary

User story

As a user, I want to be able to sort the rows of a table in a particular order

As a user, I want a powerful way to control the sort order of a table

API

For this patch, 2 new props will be added to the Table component:

  • sort: This prop, bound via v-model will be an object with string keys and "asc" or "desc" values
  • allowMultiSort: This boolean prop can be used to indicate that more than one column can be sorted at a time. Otherwise, when one column is sorted, the other columns will become unsorted.

The TableColumn type will be updated with a new boolean property, sortable (or allowSort?) When true, the sort icon will display in this column's <th> in the <thead>.

Sorting

The parent component must handle sorting the data and providing the rows in the right order. The Table component will simply take in the sort prop, let the component know when the user changes the sort order, and show the appropriate sort icon.

Sort icon behavior

The sort icon behaves in the following way:

  • When a column is sortable but unsorted, the full icon shows
  • When a column is sorted ascending, only the up arrow shows
  • When a column is sorted descending, only the down arrow shows
  • When a user clicks a column to sort it, the column becomes sorted ascending
  • When they click again, the column becomes sorted descending
  • When they click a third time, the column becomes unsorted
  • When multi-sort is off, sorting a column unsorts all others
  • When multi-sort is on, sorting a column does not unsort the others

Sortable <th> elements should have the following interactive states:

Screenshot 2024-03-14 at 13.39.25.png (152×1 px, 27 KB)

Accessibility considerations

The following attributes should be used to make sorting accessible to users of assistive technology:

  • aria-sort to communicate the sorting method
  • role="columnheader" (elements with this role need to be nested as descendants of elements with role="row")
  • scope="col"
  • the sort icons should be removed from the accessibility tree to avoid redundancies using`aria-hidden="true"`

Not all screen readers support aria-sort, so we should find an alternative method to communicate the sorting status and options. I found examples where indications are provided by including buttons in the header and using their labels (ARIA Sortable table example) or by using divs (Carbon's Sorting variant of Table). The recommended label copy is "Sort rows by [column label] in ["descending" or "ascending"] order."

Manual accessibility testing will be performed to evaluate compliance.

Demos and testing

New demos should be added to demonstrate the different types of sorting, and ways that a parent component might handle it (actually sorting the data, or making a new API call).

Unit tests should be added to cover sort functionality.


Acceptance criteria

  • The Table component has new props for sort and allowMultiSort
  • The TableColumn type has a new optional boolean property, sortable (or allowSort)
  • The sort icon is implemented in Table and click events are handled, informing the parent component when sort changes
  • Demos are added to the sandbox page
  • Unit tests cover the new behavior

Event Timeline

Restricted Application added a subscriber: Aklapper. · View Herald Transcript

@Sarai-WMDE could you please confirm the sort icon functionality I listed in this task?

Thank you so much for the detailed outline of the sorting functionality, Anne!

Regarding the sort icon behavior, the only bit I would adjust is:

"When a user clicks a column to sort it, then clicks again, the column becomes sorted ascending" -> Observing existing examples, it appears that an ascending sorting order should be applied right away when the sortable column's heading is clicked.

Other comments/ questions:

  1. Should we mention (maybe just as an AC) that sortable <th>s will display interactive states (hover, active, focus)?

Screenshot 2024-03-14 at 13.39.25.png (152×1 px, 27 KB)

  1. Regarding multi-sort: we originally didn't include this functionality as part of the MVP, but of course is a very nice to have to offer a more complete sorting functionality. We could re-evaluate the scope depending on the implementation cost (of creating the corresponding demo).
  1. Accessibility: There are a bunch of a11y recommendations to follow when implementing sorting (e.g. add aria-sort="value" in addition to scope="col" and role=columnheader, find an alternative way to indicate sorting and direction for screen readers that don't support aria-sort, etc.). Should I add an a11y section to this task, or maybe create a sub-task? Let me know what you think makes the most sense
CCiufo-WMF triaged this task as Medium priority.Mar 14 2024, 3:01 PM

Regarding the sort icon behavior, the only bit I would adjust is:

"When a user clicks a column to sort it, then clicks again, the column becomes sorted ascending" -> Observing existing examples, it appears that an ascending sorting order should be applied right away when the sortable column's heading is clicked.

I have no idea what my brain was thinking when I typed this; I fixed it.

  1. Should we mention (maybe just as an AC) that sortable <th>s will display interactive states (hover, active, focus)?

Added!

  1. Regarding multi-sort: we originally didn't include this functionality as part of the MVP, but of course is a very nice to have to offer a more complete sorting functionality. We could re-evaluate the scope depending on the implementation cost (of creating the corresponding demo).

I went back and forth on this a bit...I kind of want to build it in initially to avoid changing the way the sort prop works in the future, but if we find during development that supporting multisort will be challenging, we could punt it to post-MVP.

  1. Accessibility: There are a bunch of a11y recommendations to follow when implementing sorting (e.g. add aria-sort="value" in addition to scope="col" and role=columnheader, find an alternative way to indicate sorting and direction for screen readers that don't support aria-sort, etc.). Should I add an a11y section to this task, or maybe create a sub-task? Let me know what you think makes the most sense

An a11y section of this task would be great, thank you!

lwatson set the point value for this task to 3.Mar 18 2024, 5:43 PM
lwatson changed the task status from Open to In Progress.Fri, Apr 26, 11:13 PM
lwatson changed the status of subtask T362709: Table: add sort icon from Open to In Progress.
lwatson changed the status of subtask T362710: Table: add single sort from Open to In Progress.