Data Tables

Svelte Component

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

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>

Search, Sort, and Pagination

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>

Table Mapper Service

We've provided a utility service for remapping data from a object to an array. The first parameter is the keys, which also defines the order. The second parameter is the object you wish to map.

typescript
import { mapTableSource } from '@brainandbones/skeleton';
typescript
const sourceObject: any = [
	{ 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' },
];
const mappedSource: string[] = mapTableSource(['position', 'name', 'weight', 'symbol'], sourceObject)