Web Components Hierarchical Grid State Persistence
The Ignite UI for Web Components State Persistence in Web Components Hierarchical Grid allows developers to easily save and restore the grid state. When the IgcGridState is applied on the Web Components IgcHierarchicalGridComponent, it exposes the GetState, GetStateAsString, ApplyState and ApplyStateFromString methods that developers can use to achieve state persistence in any scenario.
Supported Features
IgcGridState directive supports saving and restoring the state of the following features:
RowIslands
saving/restoring features for all child grids down the hierarchy
Sorting
Filtering
AdvancedFiltering
Paging
CellSelection
RowSelection
ColumnSelection
RowPinning
Expansion
Columns
Multi column headers
Columns order
Column properties defined by the IColumnState interface.
Usage
The getState method returns the grid state in a IgcGridStateInfo object, containing all the state info. Additional steps may be required in order to save it.
The GetStateAsString returns a serialized JSON string, so developers can just take it and save it on any data storage (database, cloud, browser localStorage, etc).
The developer may choose to get only the state for a certain feature/features, by passing in an array with feature names as an argument. Empty array will result to using the default state options.
var gridState = document.getElementById('gridState') as IgcGridStateComponent;
// get an `IgcGridStateInfo` object, containing all features original state objects, as returned by the grid public APIconst state: IgcGridStateInfo = gridState.getState();
// get all features` state in a serialized JSON stringconst stateString: string = gridState.getStateAsString();
// get the sorting and filtering expressionsconst sortingFilteringStates: IgcGridStateInfo = gridState.getState(['sorting', 'filtering']);
typescript
ApplyState - The method accepts a IgcGridStateInfo object as argument and will restore the state of each feature found in the object or specified features as second argument.
ApplyStateFromString - The method accepts a serialized JSON string as argument and will restore the state of each feature found in the JSON string or specified features as second argument.
The Options object implements the IgcGridStateOptions interface, i.e. for every key, which is the name of a certain feature, there is the boolean value indicating if this feature state will be tracked. GetState/GetStateAsString methods will not put the state of these features in the returned value and ApplyState/ApplyStateFromString methods will not restore state for them.
The simple to use single-point API's allows to achieve a full state persistence functionality in just a few lines of code. Copy paste the code from below - it will save the grid state in the browser LocalStorage object every time the user leaves the current page. Whenever the user returns to main page, the grid state will be restored. No more need to configure those complex advanced filtering and sorting expressions every time to get the data you want - do it once and have the code from below do the rest for your users:
constructor() {
window.addEventListener("load", () => { this.restoreGridState(); });
window.addEventListener("beforeunload", () => { this.saveGridState(); });
}
// Using methods that work with IgcGridStateInfo object.publicsaveGridState() {
const state = this.gridState.getState();
window.localStorage.setItem('grid-state', JSON.stringify(state));
}
publicrestoreGridState() {
const state = window.localStorage.getItem('grid-state');
if (state) {
this.gridState.applyState(JSON.parse(state));
}
}
// Or using string alternative methods.publicsaveGridStateString() {
const state = this.gridState.getStateAsString();
window.localStorage.setItem('grid-state', state);
}
publicrestoreGridStateString() {
const state = window.localStorage.getItem('grid-state');
if (state) {
this.gridState.applyStateFromString(state);
}
}
typescript
Restoring Child Grids
Saving / Restoring state for the child grids is controlled by the RowIslands property and is enabled by default. IgcGridState will use the same options for saving/restoring features both for the root grid and all child grids down the hierarchy. For example, if we pass the following options:
Then the getState API will return the state for all grids (root grid and child grids) features excluding Selection and Sorting. If later on the developer wants to restore only the Filtering state for all grids, use:
<!DOCTYPE html><html><head><title>Sample | Ignite UI | Web Components | infragistics</title><metacharset="UTF-8" /><linkrel="shortcut icon"href="https://static.infragistics.com/xplatform/images/browsers/wc.png" ><linkrel="stylesheet"href="https://fonts.googleapis.com/icon?family=Material+Icons" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Kanit&display=swap" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Titillium Web" /><linkrel="stylesheet"href="https://static.infragistics.com/xplatform/css/samples/shared.v8.css" /><linkrel="stylesheet"href="/src/index.css"type="text/css" /></head><body><divid="root"><divclass="sampleContainer"><divclass="container horizontal"><igc-buttonid="restoreState"variant="contained"><igc-iconname="restore"collection="material"></igc-icon>
Restore
</igc-button><igc-buttonid="saveState"variant="contained"><igc-iconname="save"collection="material"></igc-icon>
Save
</igc-button><igc-buttonid="resetState"variant="contained"><igc-iconname="clear"collection="material"></igc-icon>
Reset
</igc-button><igc-buttonid="leavePage"variant="contained"><igc-iconname="forward"collection="material"></igc-icon>
Leave
</igc-button><igc-buttonid="clearStorage"variant="contained"><igc-iconname="delete"collection="material"></igc-icon>
Clear
</igc-button><igc-buttonid="reloadPage"variant="contained"><igc-iconname="refresh"collection="material"></igc-icon>
Reload
</igc-button></div><divclass="container horizontal"><ul><li>Clicking the SAVE button or leaving the page <aid="leaveLink"href="./grids/hierarchical-grid/state-persistence-about"><strong>here</strong></a> will save grid state to localStorage.</li><li>Use the control buttons to SAVE / RESTORE / RESET / DELETE / grid state or LEAVE the page.</li><li>Select/Deselect checkboxes to control saving / restoring feature state.</li></ul></div><divclass="container horizontal"><igc-checkboxid="allFeatures"checked>All Features</igc-checkbox><igc-checkboxid="advancedFiltering"checked>Adv. Filtering</igc-checkbox><igc-checkboxid="cellSelection"checked>Cell Selection</igc-checkbox><igc-checkboxid="columns"checked>Columns</igc-checkbox><igc-checkboxid="columnSelection"checked>Col Selection</igc-checkbox><igc-checkboxid="expansion"checked>Expansion</igc-checkbox><igc-checkboxid="filtering"checked>Filtering</igc-checkbox><igc-checkboxid="paging"checked>Paging</igc-checkbox><igc-checkboxid="rowPinning"checked>Row Pinning</igc-checkbox><igc-checkboxid="rowSelection"checked>Row Selection</igc-checkbox><igc-checkboxid="sorting"checked>Sorting</igc-checkbox><igc-checkboxid="moving"checked>Moving</igc-checkbox><igc-checkboxid="rowIslands"checked>Row Island</igc-checkbox></div><igc-hierarchical-gridid="grid"name="grid"primary-key="Photo"width="98%"height="500px"auto-generate="false"moving="true"allow-filtering="true"><igc-grid-stateid="gridState"></igc-grid-state><igc-grid-toolbar><igc-grid-toolbar-actions><igc-grid-toolbar-hiding></igc-grid-toolbar-hiding><igc-grid-toolbar-pinning></igc-grid-toolbar-pinning></igc-grid-toolbar-actions></igc-grid-toolbar><igc-action-strip><igc-grid-pinning-actions></igc-grid-pinning-actions></igc-action-strip><igc-paginator></igc-paginator><igc-columnfield="Artist"sortable="true"></igc-column><igc-columndata-type="image"field="Photo"editable="false"sortable="true"></igc-column><igc-columnfield="Debut"data-type="number"sortable="true"></igc-column><igc-columnfield="GrammyNominations"header="Grammy Nominations"data-type="number"sortable="true"></igc-column><igc-columnfield="GrammyAwards"header="Grammy Awards"data-type="number"sortable="true"></igc-column><igc-row-islandheight="null"child-data-key="Albums"id="albumsRowIsland"auto-generate="false"primary-key="Album"allow-filtering="true"><igc-columnfield="Album"sortable="true"></igc-column><igc-columnfield="LaunchDate"header="Launch Date"data-type="date"sortable="true"></igc-column><igc-columnfield="BillboardReview"header="Billboard Review"sortable="true"></igc-column><igc-columnfield="USBillboard200"header="US Billboard 200"sortable="true"></igc-column><igc-row-islandheight="null"child-data-key="Songs"id="songsRowIsland"auto-generate="false"primary-key="Number"allow-filtering="true"><igc-columnfield="Number"header="No."sortable="true"></igc-column><igc-columnfield="Title"sortable="true"></igc-column><igc-columnfield="Released"data-type="date"sortable="true"></igc-column><igc-columnfield="Genre"></igc-column></igc-row-island></igc-row-island><igc-row-islandheight="null"child-data-key="Tours"id="toursRowIsland"auto-generate="false"primary-key="Tour"allow-filtering="true"><igc-columnfield="Tour"sortable="true"></igc-column><igc-columnfield="StartedOn"header="Started on"sortable="true"></igc-column><igc-columnfield="Location"sortable="true"></igc-column><igc-columnfield="Headliner"sortable="true"></igc-column></igc-row-island></igc-hierarchical-grid></div></div><!-- This script is needed only for parcel and it will be excluded for webpack -->
<% if (false) { %><scriptsrc="src/index.ts"></script><% } %>
</body></html>html
Like this sample? Get access to our complete Ignite UI for Web Components toolkit and start building your own apps in minutes. Download it for free.
Limitations
When restoring all grid features at once (using applyState API with no parameters), then column properties for the root grid might be resetted to default. If this happens, restore the columns or column selection feature separately after that: