#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:
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;
};
@Component({
selector: 'my-grid',
imports: [AdaptableProvider, AdaptableWorkspace],
template: `
<adaptable-provider [persistState]="persistState" [loadState]="loadState"
adaptableId="my-grid"
primaryKey="id"
[data]="data"
[defaultState]="defaultState"
>
<adaptable-workspace />
</adaptable-provider>
`,
})
export class CustomPersistence {
protected readonly data = data;
protected readonly defaultState = defaultState;
protected readonly persistState = persistState;
protected readonly loadState = loadState;
}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 outside the component or as a field of
it, which is created once, not in a method or a computed that returns a new
function. 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
The (stateChange) output emits the new state after every change (the
onStateChange input, a callback, also gets the
previous state):
<adaptable-provider
adaptableId="my-grid"
primaryKey="id"
[data]="data"
[defaultState]="defaultState"
(stateChange)="onStateChange($event)"
/>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:
@Component({
selector: 'my-grid',
imports: [AdaptableProvider],
template: `
<adaptable-provider
adaptableId="my-grid"
primaryKey="id"
[data]="data"
[defaultState]="defaultState"
[onStateChangeWithSelectors]="listeners"
/>
`,
})
export class Selectors {
protected readonly data = data;
protected readonly defaultState = defaultState;
protected readonly listeners = [
withState({
selector: (state) => state.view.currentViewId,
callback: (currentViewId, previousViewId) => {
console.log(`view changed from ${previousViewId} to ${currentViewId}`);
},
}),
];
}A pair can also carry an equals(a, b) function, when the default comparison
isn't what you need.
Pass a stable array (a field of the component, or a constant outside it, not an array literal in the template): every new array re-subscribes all the listeners.
(stateChange) 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 inject functions
instead (the Angular counterparts of the hooks):
injectAdaptableState returns a signal of
the slice a selector picks,
injectAdaptableStateSubscribe
calls a callback when that slice changes, and
injectCurrentViewState returns a
signal of the current view.
#Controlled state
To own the state yourself, bind state instead of
defaultState: state is a model, so
[(state)] binds a signal of yours both ways:
@Component({
selector: 'my-grid',
imports: [AdaptableProvider, AdaptableWorkspace],
template: `
<adaptable-provider [(state)]="state"
adaptableId="my-grid"
primaryKey="id"
[data]="data"
>
<adaptable-workspace />
</adaptable-provider>
`,
})
export class Controlled {
protected readonly data = data;
protected readonly state = signal<AdaptableUserState>(initialState);
}The grid renders the state you bind. User
interactions write the next state into your signal (and emit (stateChange)),
and a value you set is applied to the grid.
As everywhere in Angular, the state is not strictly controlled: with a one-way
[state], user interactions still change the grid, but your signal doesn't
follow them. Also pick one mode per instance: whether the Provider is
controlled is decided when it initializes, from whether state is set, and
later switches between state and
defaultState are ignored. To start a new instance,
give the <adaptable-provider> element a new identity (@for … track).
Controlled state lets you drive the grid declaratively: set 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 written into your signal.
The buttons change the current view and the sorting by setting the signal
bound with [(state)]. Changes made in the grid itself flow back into the
same signal.