Blazor Grid State Persistence

    The Ignite UI for Blazor State Persistence in Blazor Grid allows developers to easily save and restore the grid state. When the IgbGridState is applied on the Blazor IgbGrid, it exposes the GetStateAsString and ApplyStateFromString methods that developers can use to achieve state persistence in any scenario.

    Supported Features

    IgbGridState directive supports saving and restoring the state of the following features:

    • Sorting
    • Filtering
    • Advanced Filtering
    • Paging
    • CellSelection
    • RowSelection
    • ColumnSelection
    • RowPinning
    • Expansion
    • GroupBy
    • Columns
      • Multi column headers
      • Columns order
      • Column properties defined by the IColumnState interface.

    Usage

    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.

    <IgbGrid>
        <IgbGridState @ref="gridState"></IgbGridState>
    </IgbGrid>
    
    @code {
        // get all features` state in a serialized JSON string
        string stateString = gridState.GetStateAsString(new string[0]);
    
        // get the sorting and filtering expressions
        string sortingFilteringStates = gridState.GetStateAsString(new string[] { "sorting", "filtering" });
    }
    

    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.

    gridState.ApplyStateFromString(gridStateString, new string[0]);
    gridState.ApplyStateFromString(sortingFilteringStates, new string[0])
    

    The Options object implements the IgbGridStateOptions 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. GetStateAsString methods will not put the state of these features in the returned value and ApplyStateFromString methods will not restore state for them.

    gridState.Options = new IgbGridStateOptions
        {
            CellSelection = false,
            Sorting = false
        };
    

    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:

    @using IgniteUI.Blazor.Controls
    @using Newtonsoft.Json
    @implements IDisposable
    
    @inject IJSRuntime JS
    @inject NavigationManager Navigation
    
    <IgbGrid Rendered="OnGridRendered">
        <IgbGridState @ref="gridState"></IgbGridState>
        <IgbColumn Field="ContactName" Header="Name" MinWidth="200px" ></IgbColumn>
        <IgbColumn Field="ContactTitle" Header="Title" MinWidth="200px" Sortable="true" Filterable="true" Groupable="true"></IgbColumn>
        <IgbColumn Field="CompanyName" Header="Company" MinWidth="200px" Sortable="true" Filterable="true" Groupable="true"></IgbColumn>
    </IgbGrid>
    
    @code {
        protected override void OnAfterRender(bool firstRender)
        {
            Navigation.LocationChanged += OnLocationChanged;
        }
    
        void OnLocationChanged(object sender, LocationChangedEventArgs e)
        {
            SaveGridState();
        }
    
        void IDisposable.Dispose()
        {
            // Unsubscribe from the event when our component is disposed
            Navigation.LocationChanged -= OnLocationChanged;
        }
    
        void OnGridRendered()
        {
            RestoreGridState();
        }
    
        async void SaveGridState() {
            string state = gridState.getStateAsString(new string[0]);
            await JS.InvokeVoidAsync("window.localStorage.setItem", "grid-state", state);
        }
    
        async void RestoreGridState() {
            string state = await JS.InvokeAsync<string>("window.localStorage.getItem", "grid-state");
            if (state) {
                gridState.ApplyStateFromString(state, new string[0]);
            }
        }
    }
    

    Demo

    Limitations

    Additional Resources