Render paginated accessible data tables
Choose a logical table pipeline
A data-intensive interface must make it easy to find and compare records without losing the meaning of the underlying data. This original equipment-inspection fixture contains four synthetic observations measured in whole minutes. Filter the complete dataset, then sort, then take a page. Sorting only the displayed page is not a global sort. A row ID identifies a record even when its visible position changes. React 18+ syntax is the proposed baseline; no third-party dependency is needed for the source examples.
export const records = [ {id:'a', name:'Drill', group:'Power', minutes:12}, {id:'b', name:'Saw', group:'Hand', minutes:8}, {id:'c', name:'Sander', group:'Power', minutes:20}, {id:'d', name:'Clamp', group:'Hand', minutes:4}];export function pageRows(rows, {query='', descending=false, page=0, size=2}={}) { if (!Number.isSafeInteger(page) || page<0 || !Number.isSafeInteger(size) || size<1 || size>100) throw new RangeError('Invalid page'); const filtered=rows.filter(r=>r.name.toLowerCase().includes(query.trim().toLowerCase())); filtered.sort((a,b)=>(descending ? b.minutes-a.minutes : a.minutes-b.minutes) || (a.id<b.id?-1:a.id>b.id?1:0)); const pages=Math.ceil(filtered.length/size); const index=Math.min(page, Math.max(0,pages-1)); return {rows:filtered.slice(index*size,(index+1)*size),total:filtered.length,pages,index};}Initially the first two rows are Clamp (4) and Saw (8). Descending order puts Sander (20) and Drill (12) first. Searching for “sa” finds Saw and Sander. An empty result has zero pages, and an out-of-range page is clamped. A new filter should reset the current page; the helper does not mutate the source array. The ID tie-breaker ensures stable ordering for equal durations.
Render a semantic table with sortable headers
import React,{useState} from 'react';import {records,pageRows} from './rows.mjs';export default function InspectionTable(){ const [query,setQuery]=useState(''),[descending,setDescending]=useState(false),[page,setPage]=useState(0); const [selected,setSelected]=useState<string[]>([]); const result=pageRows(records,{query,descending,page}); return <section><label>Filter equipment <input value={query} onChange={e=>{setQuery(e.target.value);setPage(0);}} /></label> <p role="status">{result.total} matches; {selected.length} selected across all records</p> <table><caption>Inspection duration in minutes</caption><thead><tr><th scope="col">Equipment</th><th scope="col" aria-sort={descending?'descending':'ascending'}><button onClick={()=>{setDescending(v=>!v);setPage(0);}}>Minutes ({descending?'descending':'ascending'})</button></th><th scope="col">Select</th></tr></thead> <tbody>{result.rows.map(r=><tr key={r.id}><th scope="row">{r.name}</th><td>{r.minutes}</td><td><input type="checkbox" aria-label={`Select ${r.name}`} checked={selected.includes(r.id)} onChange={e=>{const checked=e.target.checked;setSelected(ids=>checked?[...ids.filter(id=>id!==r.id),r.id]:ids.filter(id=>id!==r.id));}}/></td></tr>)}</tbody></table> {!result.total && <p>No matching equipment.</p>} <button disabled={result.index===0} onClick={()=>setPage(result.index-1)}>Previous</button> <span> {result.pages?result.index+1:0} of {result.pages} </span> <button disabled={result.index+1>=result.pages} onClick={()=>setPage(result.index+1)}>Next</button> <button onClick={()=>setSelected([])}>Clear selection</button></section>;}The caption identifies the dataset and unit. Header scope connects labels with cells; a native button makes sorting keyboard reachable, and aria-sort conveys the active direction. Checkboxes use record IDs rather than page indexes. Selection persists across filters intentionally; the status tells users it includes hidden records and Clear selection makes recovery possible. In production, distinguish “select this page” from “select all matching records” and authorize bulk actions on the server. Keep focus on the activating control and verify keyboard behavior and screen-reader announcements. Do not assign role=grid merely to make a table interactive; grids require a more elaborate keyboard model.
Define a server-side query contract
export function dashboardKey({site,query,page,size,descending}) { if (!['north','south'].includes(site) || typeof query!=='string' || typeof descending!=='boolean' || !Number.isSafeInteger(page) || page<0 || !Number.isSafeInteger(size) || size<1 || size>100) throw new TypeError('Invalid request'); return ['inspections',site,query.trim().toLowerCase(),page,size,descending?'desc':'asc','minutes','UTC'];}For a large dataset send filter, sort, page and size to the server and return rows plus a total or cursor metadata. This fixture uses normalized case-insensitive search and a fixed UTC/minutes reporting contract. Include every result-affecting field in the cache key; tenant and authorization scope must also be isolated in real systems. Do not filter the current server page again and call the result global. A cursor can have no known final page. Grouping and aggregation must use the same filter scope and snapshot; a chart based on the visible page should say so. TanStack Table v8 supplies headless row models, sorting, grouping, pagination and selection; manualPagination/manualSorting leave those operations to the caller. Use getRowId for record identity. The v8 docs are the reference lane; newer versions may change setup. No library runtime test was performed. Practice: predict the descending results after filtering, and explain how selection should behave.