Table Component — Astro
Accessible data table with sorting and optional filtering
Table Component
An accessible data table for displaying tabular data. Supports column header sorting (click or Enter/Space), optional filter input to search across all columns, striped rows, and responsive horizontal scroll.
Add this component
The command below includes <strong>Table</strong>—run it in your project directory to install this component (and the CSS if needed). No prompts.
Choose your package manager — click a tab to select, then copy the command.
Props
columns(array, required) - Column definitions:{ key, label, sortable?, type? }.typeis'string'or'number'for sort order.data(array, required) - Row data: array of objects with keys matching columnkey.caption(string, optional) - Table caption for accessibility.sortable(boolean, optional) - Enable column header sorting (default: true).filterable(boolean, optional) - Show filter input above table (default: false).filterPlaceholder(string, optional) - Placeholder for filter input (default: "Filter table…").striped(boolean, optional) - Striped rows (default: true).class(string, optional) - Additional CSS classes.
Basic Usage
Table sorting/filtering script runs after DOM ready when the component is used.
| Alice | Developer | Active |
| Bob | Designer | Active |
| Carol | Manager | Away |
Usage
Full example for each framework (Astro, Vanilla, Svelte, Vue, React). Switch framework via View as or by clicking a Usage tab—both stay in sync.
With Sorting (number column)
Set type: 'number' on columns that should sort numerically.
| Alice | 92 | 3 |
| Bob | 87 | 2 |
| Carol | 95 | 4 |
<Table
caption="Scores"
columns={[
{ key: 'name', label: 'Name' },
{ key: 'score', label: 'Score', type: 'number' },
{ key: 'level', label: 'Level', type: 'number' },
]}
data={[
{ name: 'Alice', score: 92, level: 3 },
{ name: 'Bob', score: 87, level: 2 },
{ name: 'Carol', score: 95, level: 4 },
]}
/> With Filter
Use filterable to show a search input that filters rows by any column.
| Alice | Developer | Active |
| Bob | Designer | Active |
| Carol | Manager | Away |
| Dave | Developer | Active |
<Table
caption="Filterable list"
columns={[...]}
data={[...]}
filterable
filterPlaceholder="Search…"
/> Without Stripes
| 1 | 2 |
| 3 | 4 |
Features
- Accessible - Semantic
<table>,<caption>,scope="col",aria-sorton sortable headers, keyboard (Enter/Space) to sort. - Sorting - Sortable column headers use the Sort icon; click to sort ascending/descending;
type: 'number'for numeric sort. - Filtering - Optional filter input with Filter icon; rows are shown/hidden by matching text in any cell.
- Striped rows - Optional alternating row background; row hover uses theme colors for contrast.
- Responsive - Table wrapper scrolls horizontally on small screens.