Repository navigation
Saving and Restoring Layouts
github-actions[bot] edited this page Sep 14, 2026
·
1 revision
The layout is your array of items: the grid writes x, y, cols and rows back into the item objects. Saving means storing those values; restoring means passing them back.
Save after changes, debounced: one drag can change several items (pushed, swapped or compacted ones).
import { Component } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { GridsterConfig, GridsterItemConfig } from 'angular-gridster2-plus';
import { debounceTime, Subject } from 'rxjs';
type WidgetItem = GridsterItemConfig & { id: string };
type SavedPosition = Pick<WidgetItem, 'id' | 'x' | 'y' | 'cols' | 'rows'>;
@Component({/* ... */})
export class Dashboard {
dashboard: WidgetItem[] = [];
private readonly changes = new Subject<void>();
options: GridsterConfig = {
itemChangeCallback: () => this.changes.next()
};
constructor() {
this.changes.pipe(debounceTime(300), takeUntilDestroyed()).subscribe(() => this.save());
}
save(): void {
const layout: SavedPosition[] = this.dashboard.map(({ id, x, y, cols, rows }) => ({ id, x, y, cols, rows }));
localStorage.setItem('dashboard', JSON.stringify(layout));
}
}Store only the layout fields and the id; keep the widget configuration separate if it is large.
restore(widgets: WidgetItem[]): void {
const saved: SavedPosition[] = JSON.parse(localStorage.getItem('dashboard') ?? '[]');
const positions = new Map(saved.map(position => [position.id, position]));
this.dashboard = widgets.map(widget => ({ ...widget, ...positions.get(widget.id) }));
}When the saved layout does not fit the current options:
- items that overlap or do not fit are moved to the first free position (or hidden with
disableAutoPositionOnConflict: true), see Items; - sizes outside the item limits are clamped and
itemChangeis emitted, so the corrected layout is saved again.
Save one layout per user, and one per breakpoint when the columns change with the screen size (see Responsive and Mobile).
MIT License. Originally created by Tiberiu Zuld as angular-gridster2. These pages are generated from the wiki/ folder of the repository: propose changes with a pull request.
Start
Guides
- Grid Types
- Grid Size and Margins
- Items
- Drag
- Resize
- Push and Swap
- Compaction
- Empty Cells
- Multi-Layer
- Responsive and Mobile
- RTL
- Dynamic Widgets
- Saving and Restoring Layouts
Reference
Help