Get or set local storage value in Angular TreeGrid component
25 Aug 20251 minute to read
When the enablePersistence
property is set to true
, the TreeGrid’s state and property values are automatically saved in window.localStorage
. This allows you to restore the component’s state between sessions.
You can programmatically get or set the local storage value for the TreeGrid by using the getItem
and setItem
methods of window.localStorage
. The storage key is a concatenation of the component name and its id (e.g., "treegridTreeGrid"
).
// Get the TreeGrid model value from localStorage
let value: string = window.localStorage.getItem('treegridTreeGrid'); // "treegridTreeGrid" = component name + component id
let model: Object = value ? JSON.parse(value) : {};
// Set the TreeGrid model value in localStorage
window.localStorage.setItem('treegridTreeGrid', JSON.stringify(model)); // "treegridTreeGrid" = component name + component id