v0.5.0
GitHub

reactiveTable

The reactiveTable function creates a reactive table instance that automatically updates when your data changes. It can be extended with plugins to add features like sorting, pagination, and column visibility.

Signature

			function reactiveTable<T>(
	initialData: T[],
	columnDefs: ColumnDef<T>[]
): ReactiveTableWithPlugins<T>;
		

Parameters

ParameterTypeDescription
initialDataT[]Array of data items to populate the table
columnDefsColumnDef<T>[]Column definitions specifying what data to display

Return Value

Returns a ReactiveTableWithPlugins<T> instance with reactive properties, methods, and plugin support.

Basic Usage

			<script lang="ts">
	import { reactiveTable } from 'svelte-reactive-table';

	const data = [
		{ id: 1, name: 'John Doe', age: 30 },
		{ id: 2, name: 'Jane Smith', age: 25 }
	];

	const columns = [
		{ accessor: 'id', header: 'ID', isIdentifier: true },
		{ accessor: 'name', header: 'Name' },
		{ accessor: 'age', header: 'Age' }
	];

	const table = reactiveTable(data, columns);
</script>

<table>
	<thead>
		<tr>
			{#each table.headers as header}
				<th>{header}</th>
			{/each}
		</tr>
	</thead>
	<tbody>
		{#each table.rows as row}
			<tr>
				{#each row.cells as cell}
					<td>{cell.value}</td>
				{/each}
			</tr>
		{/each}
	</tbody>
</table>
		

Adding Plugins

Svelte Reactive Table uses a flexible plugin architecture to extend functionality. Plugins are added using the use method:

			<script>
	import { reactiveTable, reactivePagination } from 'svelte-reactive-table';

	const table = reactiveTable(data, columns).use(
		reactivePagination({
			pageSize: 10,
			page: 0
		})
	);

	// Access the pagination API through table.plugins
	const { pagination } = table.plugins;
</script>
		

For detailed pagination properties and methods, see the reactivePagination API reference.

Properties

PropertyTypeDescription
dataT[]Current data array (reactive)
columnDefsColumnDef<T>[]Current column definitions (reactive)
allColumnsColumn<T>[]All columns with their full configuration
columnsColumn<T>[]Only columns that are currently visible
headersstring[]Array of header texts from visible columns
allRowsRow<T>[]Array of all rows with their cells
rowsRow<T>[]Rows after applying active features (pagination, sorting, etc.)
pluginsRecord<string, unknown>Registry of all installed plugins and their state

Methods

MethodReturn TypeDescription
use(plugin)ReactiveTableWithPlugins<T>Install a plugin to the table

Examples

Basic Table

			const table = reactiveTable(data, columns);
		

With Plugins

			const table = reactiveTable(data, columns)
  .use(reactiveColumnVisibility())
  .use(reactiveSorting())
  .use(reactivePagination({ pageSize: 10 }));

// Access plugins through the plugins property
const { columnVisibility, sorting, pagination } = table.plugins;
		

With TypeScript

			import {
	reactiveTable,
	type ColumnDef,
	type ReactiveTableWithPlugins
} from 'svelte-reactive-table';

type User = {
	id: number;
	name: string;
	age: number;
};

const columns: ColumnDef<User>[] = [
	{ accessor: 'id', header: 'ID', isIdentifier: true },
	{ accessor: 'name', header: 'Name' },
	{ accessor: 'age', header: 'Age' }
];

const table = reactiveTable<User>(users, columns);