Data Tables
Interactive table with support for search, sort, and pagination.
import { DataTable } from '@brainandbones/skeleton';
Examples
Render a table using data that is client-side only.
Positions ↓ | Name | Weight | Symbol |
---|---|---|---|
1 | Hydrogen | 1.0079 | H |
2 | Helium | 4.0026 | He |
3 | Lithium | 6.941 | Li |
4 | Beryllium | 9.0122 | Be |
5 | Boron | 10.811 | B |
6 | Carbon | 12.0107 | C |
7 | Nitrogen | 14.0067 | N |
8 | Oxygen | 15.9994 | O |
9 | Fluorine | 18.9984 | F |
10 | Neon | 20.1797 | Ne |
Usage
Ensure your heading and source keys are defined in the same order left-to-right. Note that source values support stringified HTML.
const headings: string[] = ['Positions', 'Name', 'Weight', 'Symbol'];
const source: any[] = [
{ position: 1, name: '<strong class="text-red">Hydrogen</strong>', weight: 1.0079, symbol: 'H' },
{ position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' },
{ position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' },
];
<DataTable {headings} {source}></DataTable>
Fully Featured
The example below includes search, sort, and item count. Note that source is binding to provide item count.
const tableLocal: any = {
search: undefined,
sort: 'position',
headings: ['Positions', 'Name', 'Weight', 'Symbol'],
source: [
{ position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' },
{ position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' },
{ position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' },
{ position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be' },
{ position: 5, name: 'Boron', weight: 10.811, symbol: 'B' },
{ position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C' },
{ position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N' },
{ position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O' },
{ position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F' },
{ position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne' },
]
};
<DataTable
headings={tableLocal.headings}
bind:source={tableLocal.source}
search={tableLocal.search}
sort={tableLocal.sort}
interactive
on:sorted={onSort}
on:selected={onSelect}
>
<svelte:fragment slot="header"><input type="search" placeholder="Search..." bind:value={tableLocal.search}></svelte:fragment>
<svelte:fragment slot="footer">{tableLocal.source.length} Items</svelte:fragment>
</DataTable>
Handle events for sort and row selection. These are enabled for the demos at the top of the page. View your browser's console log during interaction.
function onSort(event: any): void { console.log('event:onSort', event.detail); }
function onSelect(event: any): void { console.log('event:onSelect', event.detail); }
Properties
Prop | Type | Default | Required | Description |
---|---|---|---|---|
headings | string[] | [] | ✓ | Provide a list of table headings. |
source | any[] | [] | ✓ | Provide the table body content. |
async | boolean | false | - | Disables search/sort within the component, allowing for server-side pagination. |
search | any | - | - | Provide a term for local fuzzy search within the compoonent. |
sort | string | - | - | Defines the sort key value. |
count | number | (source length) | - | When using async mode, use this to get a count of rows. |
interactive | boolean | false | - | Enables row hover and selection features. |
Prop | Type | Default | Description |
---|---|---|---|
header | string | bg-surface-50 dark:bg-surface-700 | Provide classes to set the table header background color. |
body | string | bg-surface-200 dark:bg-surface-800 | Provide classes to set the table body background color. |
text | string | text-sm | Provide classes to set the table text size. |
color | string | - | Provide classes to set the table text color. |
hover | string | hover:bg-primary-500/10 | Provide classes to set the hover background color. |
Events
Name | Description |
---|---|
sorted | Fires when a table heading is selected for sorting. Contains a key name reference. |
selected | If interactive enabled, fires when a row is selected. Contains the complete row data. |
Slots
Name | Description |
---|---|
header | Dislays above the table. Useful for embedding search and filter inputs. |
empty | Overrides the default "no results found" message when the table is empty. |
footer | Displays below the table. Useful for embedding pagination. |
Accessibility
ARIA GuidelinesProp | Required | Description |
---|---|---|
labelledby | - | Provide the ID of the element that labels the table. |
describedby | - | Provide the ID of the element that describes the table. |
Keyboard Interactions
The following keys provide grid navigation by moving focus among cells of the grid. Implementations of grid make these key commands available when an element in the grid has received focus, e.g., after a user has moved focus to the grid with Tab.
Keys | Description |
---|---|
Right Arrow | Moves focus one cell to the right. If focus is on the right-most cell in the row, focus does not move. |
Left Arrow | Moves focus one cell to the left. If focus is on the left-most cell in the row, focus does not move. |
Down Arrow | Moves focus one cell down. If focus is on the bottom cell in the column, focus does not move. |
Up Arrow | Moves focus one cell Up. If focus is on the top cell in the column, focus does not move. |
Home | Moves focus to the first cell in the row that contains focus. |
End | Moves focus to the last cell in the row that contains focus. |