Skip to content

Table Instance and Events

ShadcnSvelteTable creates a table for you from options. To read or change the table from elsewhere in your component, create it with createShadcnSvelteTable and hand it over:

<script lang="ts">
import { ShadcnSvelteTable, createShadcnSvelteTable } from 'shadcn-svelte-tanstack-table';
const table = createShadcnSvelteTable({
columns,
data,
enableRowSelection: true,
});
const selected = $derived(table.getSelectedRowModel().rows.length);
</script>
<button onclick={() => table.resetRowSelection()}>Clear {selected} selected</button>
<ShadcnSvelteTable {table} />

createShadcnSvelteTable must run while a component initialises (like any rune based code). The instance is a TanStack Table v9 table with MRT’s additions:

Member
table.mrtStateMRT state: isFullScreen, density, creatingRow, editingRow, showColumnFilters, draggingRow …
table.userOptionsThe options you passed, with defaults applied.
table.setCreatingRow, setEditingRow, setEditingCell, setIsFullScreen, setDensity, setShowGlobalFilter …Setters for MRT state.
table.getSelectedRowModel(), getRowModel(), getPrePaginatedRowModel()TanStack row models.
table.setSorting, setColumnFilters, setPageIndex, setGrouping, resetRowSelection …TanStack setters and resets.
table.atoms.<slice>.get()Reactive reads of a state slice, for use in $derived and templates.

You can read the same instance from snippets: every snippet that takes a context also gets table.

There is no global event system. Attach listeners to the element you care about with the ...Props options:

{
tableBodyRowProps: ({ row }) => ({ onclick: () => open(row), ondblclick: () => edit(row) }),
tableBodyCellProps: ({ cell }) => ({ oncontextmenu: (event) => menu(event, cell) }),
tableHeadCellProps: ({ column }) => ({ onmouseenter: () => hint(column) }),
}

For state changes use the on...Change options (onSortingChange, onColumnFiltersChange, onRowSelectionChange, onPaginationChange, onIsFullScreenChange …). They receive a TanStack updater, which is either a value or a function of the old value. See State management.

table.refs holds the DOM nodes (tableContainerRef, tablePaperRef, searchInputRef …) as { current } objects, and rowVirtualizerInstanceRef / columnVirtualizerInstanceRef options give you the virtualizers, for example to scroll to a row after sorting.