Data Tables

Interactive table with support for search, sort, and pagination.

javascript
import { DataTable } from '@brainandbones/skeleton';

Examples

Render a table using data that is client-side only.

Positions Name Weight Symbol
1Hydrogen1.0079H
2Helium4.0026He
3Lithium6.941Li
4Beryllium9.0122Be
5Boron10.811B
6Carbon12.0107C
7Nitrogen14.0067N
8Oxygen15.9994O
9Fluorine18.9984F
10Neon20.1797Ne
Count: 10 Items

Usage

Ensure your heading and source keys are defined in the same order left-to-right. Note that source values support stringified HTML.

typescript
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' },
];
html
<DataTable {headings} {source}></DataTable>

Fully Featured

The example below includes search, sort, and item count. Note that source is binding to provide item count.

typescript
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' },
    ]
};
html
<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.

typescript
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
headingsstring[][]Provide a list of table headings.
sourceany[][]Provide the table body content.
asyncbooleanfalse-Disables search/sort within the component, allowing for server-side pagination.
searchany--Provide a term for local fuzzy search within the compoonent.
sortstring--Defines the sort key value.
countnumber(source length)-When using async mode, use this to get a count of rows.
interactivebooleanfalse-Enables row hover and selection features.
Prop Type Default Description
headerstringbg-surface-50 dark:bg-surface-700Provide classes to set the table header background color.
bodystringbg-surface-200 dark:bg-surface-800Provide classes to set the table body background color.
textstringtext-smProvide classes to set the table text size.
colorstring-Provide classes to set the table text color.
hoverstringhover:bg-primary-500/10Provide classes to set the hover background color.

Events

Name Description
sortedFires when a table heading is selected for sorting. Contains a key name reference.
selectedIf interactive enabled, fires when a row is selected. Contains the complete row data.

Slots

Name Description
headerDislays above the table. Useful for embedding search and filter inputs.
emptyOverrides the default "no results found" message when the table is empty.
footerDisplays below the table. Useful for embedding pagination.

Accessibility

ARIA Guidelines
Prop 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 ArrowMoves focus one cell to the right. If focus is on the right-most cell in the row, focus does not move.
Left ArrowMoves focus one cell to the left. If focus is on the left-most cell in the row, focus does not move.
Down ArrowMoves focus one cell down. If focus is on the bottom cell in the column, focus does not move.
Up ArrowMoves focus one cell Up. If focus is on the top cell in the column, focus does not move.
HomeMoves focus to the first cell in the row that contains focus.
End Moves focus to the last cell in the row that contains focus.