Search docs

Jump to any table example

Shadcn Table with Saved Views

Tabs like My open, High priority and Archived each remember their filters, sort, visible columns and density. Change anything and the tab shows it; reset, save, or keep it as a new view. The pattern Linear and Notion use, on TanStack Table state.

$ npx shadcn add https://www.shad-table.dev/r/saved-views-table.json
ID
ENG-107Webhook retries double-sendTodoUrgentPriya Shah
3
Nov 29
ENG-116Mobile table overflowsIn reviewUrgentYou
2
Nov 29
ENG-101Checkout fails on saved cardsIn progressUrgentYou
3
Nov 28
ENG-105Migrate auth sessions to RedisIn progressHighJonas Weber
8
Nov 28
ENG-102Rate limit the public APITodoHighYou
5
Nov 27
ENG-110SSO with OktaIn reviewHighPriya Shah
8
Nov 27
ENG-103Flaky invoice export testIn reviewMediumYou
1
Nov 26
ENG-112Slow dashboard on large teamsTodoHighMaya Chen
5
Nov 26
ENG-104Dark mode for settingsDoneLowMaya Chen
2
Nov 25
ENG-124Duplicate key in audit logDoneUrgentJonas Weber
2
Nov 25
ENG-111CSV import drops last rowDoneHighYou
2
Nov 24
ENG-114Billing emails in user localeIn progressMediumPriya Shah
3
Nov 23
ENG-121Trial extension requestsTodoHighMaya Chen
2
Nov 22
ENG-115Remove legacy v1 endpointsTodoMediumJonas Weber
5
Nov 21
ENG-108Onboarding checklist copyTodoMediumYou
1
Nov 20
ENG-122Session timeout too shortBacklogHighYou
1
Nov 19
ENG-109Search ignores accentsBacklogMediumJonas Weber
2
Nov 18
ENG-123Export to ParquetBacklogMediumPriya Shah
5
Nov 15
ENG-106Audit color tokensBacklogLowMaya Chen
3
Nov 12
ENG-113Keyboard shortcuts help panelBacklogLowYou
2
Nov 10

How it works

1.A view is TanStack's own state, saved as JSON

Filter State Shape shows how to keep filters in one value. A view goes one step further and keeps every slice the table needs to look the same again: columnFilters, sorting and columnVisibility exactly as TanStack stores them, plus density. Nothing is converted on the way in or out, so a view can be stored anywhere that takes JSON.

src/components/saved-views/views.ts
export type ViewState = {
columnFilters: ColumnFiltersState
sorting: SortingState
columnVisibility: VisibilityState
density: Density
}

2.Edits go into a draft per view

The table reads the active view's draft if there is one, otherwise the saved state, and every change writes a draft for that view. Switching tabs never throws work away: go back and your edits are still there, with a dot on the tab until you save or reset.

src/components/saved-views/data-table.tsx
const state = drafts[activeView.id] ?? activeView.state
function update(change: Partial<ViewState>) {
setDrafts((current) => ({
...current,
[activeView.id]: { ...state, ...change },
}))
}

3."Changed" means changed in effect

Ticking a filter and unticking it again, or picking the same filters in another order, should not mark the view as changed. Each state is turned into a normalized string, with filters and values sorted, empty filters dropped and only hidden columns listed, and two states are compared by that string.

src/components/saved-views/views.ts
export const isModified = (view: SavedView, draft: ViewState | undefined) =>
draft !== undefined && fingerprint(draft) !== fingerprint(view.state)

4.Save as view leaves the original alone

Saving a new view copies the current draft into it and opens it, and the view you started from goes back to its saved state, which is what you expect after "Save as". Built-in views cannot be overwritten; your own views get Save and Delete. Custom views are kept in localStorage, read after mount so server rendering matches; in an app you would save them per user on the server.

src/components/saved-views/data-table.tsx
function saveAsView(name: string) {
const view: SavedView = { id: `view-${Date.now()}`, name, state }
changeViews([...views, view])
dropDraft(activeView.id)
setActiveId(view.id)
}

5.Counts on every tab, from the same filter code

Each tab shows how many issues it holds, including unsaved changes. The counts run the same matchesFilter function the columns use as their filterFn, so a tab never promises a different number than the table shows when you open it.

src/components/saved-views/views.ts
export function countMatching(rows: Issue[], filters: ColumnFiltersState) {
return rows.filter((row) =>
filters.every((f) => matchesFilter(row[f.id as keyof Issue], f.value)),
).length
}