#State
Adaptable keeps everything that describes the grid in a single state object:
the available columns, the views (column order, sorting, grouping, pivoting,
aggregations, filters, selection), the dashboard, styled cells, flashing cells,
alerts, cell notes, the theme and more. Each module owns one key — state.view,
state.dashboard, state.styledCell, state.alert, state.cellNote and so on.
State describes how the grid presents data. It never contains the data
itself — rows come from the data prop (see Local & remote data).
Only view is required — you need at least one view.
Every other slice is optional and falls back to the module defaults. The full
shape, slice by slice, is in the state reference.
#Default state
Pass the initial state through the defaultState prop. Adaptable owns the
state from then on: every user interaction — resizing, reordering or sorting
columns, grouping, switching views, selecting rows — updates it internally.
<Adaptable.Provider
adaptableId="my-grid"
primaryKey="id"
:data="data"
:defaultState="defaultState"
>
<Adaptable.Workspace />
</Adaptable.Provider>The default state defines two views over the same columns: a flat one and one grouped by language. Switch views from the picker in the dashboard, sort or resize a column, then reload the page — your changes are still there.
The primaryKey names the field that uniquely identifies each row. Row and
cell selection, editing and many other features rely on it, so make sure the
values are unique. The field doesn't need a column of its own.
Views and columns are covered in Views; the
getting started page walks through a first
defaultState.
#Persistence
By default, Adaptable persists the state to localStorage, under the
stateKey prop — or the adaptableId when you don't
pass stateKey. On the next mount it loads the saved state and applies it on top of
your defaultState.
What gets saved is not the whole state but a JSON patch: the difference between
your defaultState and the current state. A user who only switched views
stores something like:
[{ "op": "replace", "path": ["view", "currentViewId"], "value": "by-language" }]Functions (renderers, value getters, sort comparators) are never serialized —
they are restored from your defaultState when the patch is applied. If a
stored patch no longer fits your defaultState (for example after you removed
a view it refers to), Adaptable logs a warning and starts from the
defaultState.
State persistence is turned off when no valid license key is found.
#Custom persistence
To store state elsewhere — typically your server — pass a
persistState function, paired with a
loadState function that does the reverse:
<script setup lang="ts">
import {
Adaptable,
type AdaptableProps,
type AdaptableUserState,
} from '@adaptabletools/datagrid-vue';
import { data } from '../getting-started/basic.data';
declare const defaultState: AdaptableUserState;
const persistState: AdaptableProps['persistState'] = async ({
state,
stateKey,
}) => {
await fetch(`/api/grid-state/${stateKey}`, {
method: 'PUT',
body: JSON.stringify(state),
});
};
const loadState: AdaptableProps['loadState'] = async ({ stateKey }) => {
const response = await fetch(`/api/grid-state/${stateKey}`);
return response.ok ? response.json() : null;
};
</script>
<template>
<Adaptable.Provider :persistState="persistState" :loadState="loadState"
adaptableId="my-grid"
primaryKey="id"
:data="data"
:defaultState="defaultState"
>
<Adaptable.Workspace />
</Adaptable.Provider>
</template>Both functions receive a context with stateKey, adaptableId and
adaptableApi; persistState also gets state — the patch to store.
persistState runs after every state change, so debounce or batch it on your
side if your backend needs that. loadState runs once, on mount, and resolves
to whatever persistState last stored (or null when there is nothing).
Keep persistState stable — define it in <script setup>, which runs once,
not as an inline function in the template. Passing a new function triggers
another save.
The state is saved under a custom localStorage key, with a short delay standing
in for a network request. Sort, resize or reorder columns and watch the persisted
patch update below the grid.
Loading state is always asynchronous. When data is a function, the grid waits
for the saved state before its first fetch, so that fetch already uses the
restored view, sorting and grouping — see
Data loading waits for state hydration.
#Listening to state changes
onStateChange is called with the new and the previous
state after every change:
<Adaptable.Provider
v-bind="props"
:onStateChange="
(state: AdaptableUserState, prevState: AdaptableUserState) => {
console.log('state changed', state);
}
"
/>It doesn't tell you what changed. To react to one part of the state, use
onStateChangeWithSelectors — an array of
selector / callback pairs. The callback runs only when the value returned by
its selector changes, and receives the new value, the previous value and a
context with adaptableApi. Wrap each pair in the withState helper to get the
callback typed from the selector:
<script setup lang="ts">
import {
Adaptable,
withState,
type AdaptableProps,
} from '@adaptabletools/datagrid-vue';
declare const props: AdaptableProps;
const listeners = [
withState({
selector: (state) => state.view.currentViewId,
callback: (currentViewId, previousViewId) => {
console.log(`view changed from ${previousViewId} to ${currentViewId}`);
},
}),
];
</script>
<template>
<Adaptable.Provider v-bind="props" :onStateChangeWithSelectors="listeners" />
</template>A pair can also carry an equals(a, b) function, when the default comparison
isn't what you need.
Pass a stable array (define it in <script setup>, not inline in the
template): every new array re-subscribes all the listeners.
onStateChange counts every change, while two selectors track the current view
and its sorting. Switch views and click column headers to sort.
Inside the Provider, components can read the state with composables instead:
useAdaptableState returns a ref of the
slice a selector picks, useAdaptableStateSubscribe
calls a callback when that slice changes, and
useCurrentViewState returns a ref of
the current view.
#Controlled state
To own the state yourself, pass state instead of defaultState, together
with onStateChange:
<script setup lang="ts">
import { shallowRef } from 'vue';
import { Adaptable, type AdaptableUserState } from '@adaptabletools/datagrid-vue';
import { data } from '../getting-started/basic.data';
declare const initialState: AdaptableUserState;
const state = shallowRef<AdaptableUserState>(initialState);
const setState = (nextState: AdaptableUserState) => {
state.value = nextState;
};
</script>
<template>
<Adaptable.Provider :state="state" :onStateChange="setState"
adaptableId="my-grid"
primaryKey="id"
:data="data"
>
<Adaptable.Workspace />
</Adaptable.Provider>
</template>The grid renders whatever state you pass. User interactions don't change it
directly — they call onStateChange with the next state, and the grid updates
once you pass that state back.
Without onStateChange, a controlled grid ignores user interactions. Also pick
one mode per instance: switching between state and defaultState on a
mounted grid recreates it and logs a warning.
Controlled state lets you drive the grid declaratively: pass a new state to
switch the view, reorder columns or change the sorting — no API calls needed.
Persistence still applies: on mount, the saved state is loaded and handed to you
through onStateChange.