Saving and Restoring State
ApexGantt can capture where a user left off (zoom, scroll, collapsed rows, selection, sort, filter, manual column widths, and column order) as a serializable snapshot, then restore it later. You can persist it yourself through the API or let the chart save it to localStorage.
What the state captures
The snapshot is a GanttUiState object separate from the task data. Every field is optional on input to setState(), so a partial state applies just that slice and leaves the rest untouched.
| Field | Description |
|---|---|
version | Schema version, set by getState() |
zoom | Zoom level as pixels-per-day |
scroll | { horizontal, vertical } pixel offsets |
collapsed | IDs of collapsed summary rows |
selected | IDs of selected rows (requires enableSelection) |
sort | Active sort criteria; empty array means natural order |
filterRules | Active structured filter, or null |
quickFilter | Text in the quick-filter box |
group | Active grouping { field, direction }, or null |
columnWidths | Manual per-column pixel widths |
columnOrder | User-chosen column order |
Grouping stores only field and direction; a custom accessor or label on the criterion is not persisted, so a restored grouping falls back to the column's own accessor.
Manual persistence with getState / setState
Capture the state and send it to your own backend, then restore it on the next load:
const saved = gantt.getState() // JSON-serializable
// ... persist `saved` wherever you like ...
gantt.setState(saved)
Apply a partial state to change only one slice:
gantt.setState({ sort: [{ key: ColumnKey.Name, direction: 'asc' }] })
setState() re-renders once, then emits sortChange / filterChange for the parts that changed. Suppress those events with { silent: true }:
gantt.setState({ collapsed: ['phase-1'] }, { silent: true })
Automatic persistence with localStorage
Set persistState to have the chart restore on the first render() and save (debounced) on every view change. Pass true for the default key ('apexgantt-state'), or an object to set a custom key:
new ApexGantt(el, { series: tasks, persistState: true })
new ApexGantt(el, { series: tasks, persistState: { key: 'project-42-gantt' } })
A per-project key keeps multiple charts from overwriting each other. The snapshot is versioned and SSR-safe.
React example
import { ApexGanttChart } from 'react-apexgantt'
import type { GanttUserOptions } from 'react-apexgantt'
const options: Omit<GanttUserOptions, 'series'> = {
persistState: { key: 'project-42-gantt' },
}
export default function PersistentGantt() {
return <ApexGanttChart tasks={tasks} options={options} height="500px" />
}
Related
- Sorting and Filtering and Custom Columns for the state that gets captured.
- Methods reference for
getState()andsetState().