React Pivot Grid State Persistence

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

    Supported Features

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

    Usage

    The getState method returns the grid state in a IgrGridStateInfo 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.

    <IgrPivotGrid>
        <IgrGridState ref={(ref) => { gridState = ref; }}></IgrGridState>
    </IgrPivotGrid>
    
    // get an `IgrGridStateInfo` object, containing all features original state objects, as returned by the grid public API
    const state: IgrGridStateInfo = gridState.getState([]);
    
    // get all features` state in a serialized JSON string
    const stateString: string = gridState.getStateAsString([]);
    
    // get the sorting and filtering expressions
    const sortingFilteringStates: IgrGridStateInfo = gridState.getState(['sorting', 'filtering']);
    

    ApplyState - The method accepts a IgrGridStateInfo 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.

    gridState.applyState(gridState, []);
    gridState.applyStateFromString(gridStateString, []);
    gridState.applyState(sortingFilteringStates, [])
    

    The Options object implements the IgrGridStateOptions 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.

    <IgrGridState options={{ cellSelection: false, sorting: false }}></IgrGridState>
    

    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:

    <IgrPivotGrid rendered={restoreGridState}>
        <IgrGridState ref={(ref) => { gridState = ref; }}></IgrGridState>
    </IgrPivotGrid>
    
    useEffect(() => {
        restoreGridState();
        window.addEventListener('beforeunload', saveGridState);
        return () => {
          window.removeEventListener('beforeunload', saveGridState);
        }
    }, []);
    
    // Using methods that work with IgrGridStateInfo object.
    function saveGridState() {
        const state = gridState.getState([]);
        window.localStorage.setItem('grid-state', JSON.stringify(state));
    }
    
    function restoreGridState() {
        const state = window.localStorage.getItem('grid-state');
        if (state) {
            gridState.applyState(JSON.parse(state), []);
        }
    }
    
    //Or using string alternative methods.
    function saveGridState() {
        const state = gridState.getStateAsString([]);
        window.localStorage.setItem('grid-state', state);
    }
    
    function restoreGridState() {
        const state = window.localStorage.getItem('grid-state');
        if (state) {
            gridState.applyStateFromString(state, []);
        }
    }
    

    Restoring Pivot Configuration

    IgrGridState will not persist pivot dimension functions, value formatters, etc. by default (see limitations). Restoring any of these can be achieved with code on application level. The IgrPivotGrid exposes two events which can be used to set back any custom functions you have in the configuration: DimensionInit and ValueInit. Let's show how to do this:

    • Assign event handlers for the DimensionInit and ValueInit events:

    The DimensionInit and ValueInit events are emitted for each value and dimension defined in the pivotConfiguration property.

    • In the ValueInit event handler set all custom aggregators, formatters and styles:
      function onValueInit(s: IgrPivotGrid, event: IgrPivotValueEventArgs) {
        const value: IgrPivotValueDetail = event.detail;
        if (value.member === "AmountofSale") {
          value.aggregate.aggregator = totalSale;
          value.aggregateList?.forEach((aggr: any) => {
            switch (aggr.key) {
              case "SUM":
                aggr.aggregator = totalSale;
                break;
              case "MIN":
                aggr.aggregator = totalMin;
                break;
              case "MAX":
                aggr.aggregator = totalMax;
                break;
            }
          });
        } else if (value.member === "Value") {
          value.styles.upFontValue = (rowData: any, columnKey: any): boolean =>
            parseFloat(rowData.aggregationValues.get(columnKey.field)) > 150;
          value.styles.downFontValue = (rowData: any, columnKey: any): boolean =>
            parseFloat(rowData.aggregationValues.get(columnKey.field)) <= 150;
        }
      }
    

    Demo

    Limitations

    • getState method uses JSON.stringify() method to convert the original objects to a JSON string. JSON.stringify() does not support Functions, thats why the IgrGridState directive will ignore the pivot dimension MemberFunction, pivot values Member, Formatter, custom Aggregate functions, Styles and pivot configuration strategies: ColumnStrategy and RowStrategy.