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. horizontal is measured from where the time axis starts: the left edge in LTR, the right edge in RTL (since 3.20.0) |
collapsed | IDs of collapsed summary rows |
selected | IDs of selected rows (requires enableSelection; since 3.20.0 restoring it with selection off logs a one-time warning) |
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 |
The state does not include task data or the width of the task-list panel (the split between the list and the timeline); set that with tasksContainerWidth if you want it fixed. Rows opened or closed with expandTask() / collapseTask() (3.20.0+) are part of collapsed, like rows the user toggles.
Right-to-left charts. Since 3.20.0 scroll is restored correctly in right-to-left charts, and scroll.horizontal is measured from the right edge there, so 0 is the start of the timeline in both directions. A state saved under RTL by 3.19 or earlier was measured from the left edge, so it restores to a different position on 3.20.0. If you keep saved state across the upgrade, drop the scroll field of RTL states saved before it, or let the chart open at the start of the timeline.
To save the task data as well, read it back with getTasks() and getDependencies() (3.20.0+); see Saving links.
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().