Skip to content

Semantic Table: Sort and Filter Without a Widget

4 min read Notes

Semantic Table: Sort and Filter Without a Widget

Most sortable tables in production are divs with a role attribute and a click handler. They work with a mouse and a modern browser, and they stop working for a screen reader, for printer styles, for the browser’s find-in-page and for anyone who copies the list into a spreadsheet.

Code: Oat-Table, MIT licensed.

A semantic table keeps the native element and adds behaviour around it. Oat Table is a small extension for Oat UI that does exactly that, and the design decisions are worth reading one by one.

The table stays the source of truth

Rows live in <tbody>, headers in <thead>, and each header declares its scope. Nothing is rendered from a JavaScript data structure, so the markup is complete before the script loads.

That single decision settles a stack of problems at once: the table is indexable, it prints, it survives a failed bundle, and it can be edited by hand.

Sorting without column definitions

<th data-sort> sorts as text. data-sort="number" and data-sort="date" switch the comparator, and auto detects it. When the display format differs from the sort value, data-sort-value on the cell supplies the raw value.

<th scope="col" data-sort="number">Open tickets</th>
...
<td data-sort-value="42128">$42,128</td>

The script wraps header content in a real <button> and maintains aria-sort. That is the difference between a sortable table and an accessible sortable table: the control is in the tab order and the current direction is announced, not implied by an arrow character.

Filtering, counts and selection

An input with data-table-filter filters on row text, and data-filter-text on a <tr> supplies a better searchable string than the visible cells. An <output data-table-status aria-live="polite"> receives the visible count.

Selection is a native checkbox per row. The select-all checkbox toggles visible rows only, so filtering first and selecting second behaves the way an operator expects, and the payload of a form submission is a normal array of values.

Events instead of callbacks

Three custom events carry the state out: ot-table-sort with column and direction, ot-table-filter with the query and visible rows, ot-table-select with selected values.

That keeps integration boring. Analytics, a bulk action or a server call listens to an event; nothing has to patch an internal method or reach into a component instance.

Rows added by a fetch are picked up by refresh() or by dispatching ot-table-refresh, which matters on a list that polls.

Where this stops

Everything is client-side, so the whole dataset must be in the document. A table of forty rows is fine; a table of forty thousand is not, and neither is a search that has to hit an index.

Editing, virtual scrolling, column resizing and pivots are absent by design. Those need a component with a data model, and pretending otherwise produces a half-built grid.

Testing the fallback

Turn JavaScript off and load the page. The table must render, print and stay reachable by find-in-page. Then block the script request and repeat the check.

A page that loses its content when one file fails is a page with a single point of failure, and the failure usually arrives during a release rather than in development.

What it costs to adopt

One CSS file and one script, both from npm or a CDN, no runtime dependencies, and markup that is valid HTML before the first import. The component degrades to a readable table when the script is blocked, which is the state to test first.

Two related notes cover the neighbouring decisions: picking one response format when the table is fed by an API, and why small APIs beat one big integration when the list grows past what a page can hold.

Background: Oat Table on GitHub and the Oat UI documentation, both MIT.

Custom work

Need an API that does not exist yet?

Describe the task and the response format. We will estimate the endpoint, the integration and the maintenance.

Start a conversation