Close
Angular React Web Components Blazor Web Components
Premium

Web Components Pivot Grid 状態保持

Web Components Pivot Grid の Ignite UI for Web Components 状態保持を使用すると、開発者はグリッドの状態を簡単に保存および復元できます。GridState が Web Components IgcPivotGrid に適用されると、GetStateGetStateAsStringApplyState および ApplyStateFromString メソッドが公開され、開発者はこれを使用して、あらゆるシナリオで状態の保持を実現できます。

サポートされる機能

GridState は、以下の機能の状態の保存および復元をサポートします。

使用方法

GetState メソッドは、すべての状態情報を含むグリッドの状態を IgcGridStateInfo オブジェクトで返します。保存するには追加の手順が必要になる場合があります。

GetStateAsString は、シリアル化された JSON 文字列を返します。これは、開発者がそれを取得して任意のデータストレージ (データベース、クラウド、ブラウザーの localStorage など) に保存できます。

開発者は、引数として機能名を含む配列を渡すことにより、特定の機能の状態のみを取得することを選択できます。空の配列では、デフォルトの状態オプションが使用されます。

<igc-pivot-grid id="grid">
    <igc-grid-state id="gridState"></igc-grid-state>
</igc-pivot-grid>
var gridState = document.getElementById("gridState") as IgcGridStateComponent;

// get an `IgcGridStateInfo` object, containing all features original state objects, as returned by the grid public API
const state: IgcGridStateInfo = gridState.getState();

// get all features` state in a serialized JSON string
const stateString: string = gridState.getStateAsString();

// get the sorting and filtering expressions
const sortingFilteringStates: IgcGridStateInfo = gridState.getState([
  "sorting",
  "filtering",
]);

GridState.applyState - このメソッドは IgcGridStateInfo オブジェクトを引数として受け取り、オブジェクト内で見つかった各機能の状態、または 2 番目の引数として指定された機能を復元します。

GridState.applyStateFromString - このメソッドはシリアル化された JSON 文字列を引数として受け取り、JSON 文字列内で見つかった各機能の状態、または 2 番目の引数として指定された機能を復元します。

gridState.applyState(gridState);
gridState.applyStateFromString(gridStateString);
gridState.applyState(sortingFilteringStates)

Options オブジェクトは IgcGridStateOptions インターフェースを実装します。特定の機能の名前であるキーごとに、その機能の状態を追跡するかどうかを示すブール値があります。GridState.getState/GridState.getStateAsString メソッドはこれらの機能の状態を戻り値に含めず、GridState.applyState/GridState.applyStateFromString メソッドはその状態を復元しません。

gridState.options = { cellSelection: false, sorting: false };

シンプルな単一の API を使用することで、わずか数行のコードで完全な状態の保持機能を実現できます。以下のコードをコピーして貼り付けてください。これにより、ユーザーが現在のページを離れるたびに、ブラウザーの LocalStorage オブジェクトにグリッドの状態が保存されます。ユーザーがメインページに戻ると、グリッドの状態が復元されます。目的のデータを取得するために複雑な高度なフィルタリングやソートの式を毎回設定する必要はもうありません。一度設定するだけで、以下のコードがユーザーのために残りの処理を行います。

constructor() {
    window.addEventListener("load", () => { this.restoreGridState(); });
    window.addEventListener("beforeunload", () => { this.saveGridState(); });
}

// Using methods that work with IgcGridStateInfo object.
public saveGridState() {
    const state = this.gridState.getState();
    window.localStorage.setItem('grid-state', JSON.stringify(state));
}

public restoreGridState() {
    const state = window.localStorage.getItem('grid-state');
    if (state) {
        this.gridState.applyState(JSON.parse(state));
    }
}

// Or using string alternative methods.
public saveGridStateString() {
    const state = this.gridState.getStateAsString();
    window.localStorage.setItem('grid-state', state);
}

public restoreGridStateString() {
    const state = window.localStorage.getItem('grid-state');
    if (state) {
        this.gridState.applyStateFromString(state);
    }
}

ピボット構成の復元

GridState は、デフォルトではピボット ディメンション関数、値フォーマッタなどを保持しません (制限を参照)。これらの復元は、アプリケーション レベルのコードで実現できます。IgcPivotGrid は、構成に含まれるカスタム関数を戻すために使用できる 2 つのイベント (DimensionInitValueInit) を公開します。以下はその方法です。

  • DimensionInit および ValueInit イベントのイベント ハンドラーを割り当てます。
<igc-pivot-grid id="grid">
    <igc-grid-state id="gridState"></igc-grid-state>
</igc-pivot-grid>
 constructor() {
    var grid = document.getElementById('grid') as IgcPivotGridComponent;
    grid.pivotConfiguration = this.pivotConfiguration;
    grid.addEventListener("valueInit", (ev:any) => this.onValueInit(ev));
    grid.addEventListener("dimensionInit", (ev:any) => this.onDimensionInit(ev));
}

DimensionInit および ValueInit イベントは、PivotConfiguration プロパティで定義された値とディメンションごとに発行されます。

  • ValueInit イベント ハンドラーで、カスタムのアグリゲーター、フォーマッタ、およびスタイルをすべて設定します:
public onValueInit(event: any) {
    const value: IgcPivotValue = event.detail;
    if (value.member === 'AmountofSale') {
        value.aggregate.aggregator = this.totalSale;
        value.aggregateList?.forEach((aggr: any) => {
            switch (aggr.key) {
                case 'SUM':
                    aggr.aggregator = this.totalSale;
                    break;
                case 'MIN':
                    aggr.aggregator = this.totalMin;
                    break;
                case 'MAX':
                    aggr.aggregator = this.totalMax;
                    break;
            }
        });
    } else if (value.member === 'Value') {
        value.formatter = (value: any) => value ? '$' + parseFloat(value).toFixed(3) : undefined;
        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;
    }
}
  • In the DimensionInit event handler set all custom MemberFunction implementations:
public onDimensionInit(event: any) {
    const dim: IgcPivotDimension = event.detail;
    switch (dim.memberName) {
        case 'AllProducts':
            dim.memberFunction = () => 'All Products';
            break;
        case 'ProductCategory':
            dim.memberFunction = (data: any) => data.ProductName;
            break;
        case 'City':
            dim.memberFunction = (data: any) => data.City;
            break;
        case 'SellerName':
            dim.memberFunction = (data: any) => data.SellerName;
            break;
    }
}

デモ

ピボットストラテジの復元

GridState は、リモート ピボット操作もカスタム ディメンション ストラテジも保持しません。これらの復元は、アプリケーション レベルのコードで実現できます。GridState は、グリッド状態が適用される前に追加で変更するために使用できる StateParsed というイベントを公開します。以下はその方法です。

StateParsed は、IgcIgcPivotGrid.setState を文字列引数で使用している場合にのみ発行されます。

  • カスタムのソート ストラテジ、およびカスタムのピボット列・行ディメンション ストラテジを設定します:
    <igc-pivot-grid default-expand-state="true" super-compact-mode="true" show-pivot-configuration-ui="false"
        height="600px" id="grid">
        <igc-grid-state id="gridState"></igc-grid-state>
    </igc-pivot-grid>
public pivotConfiguration: IgcPivotConfiguration = {
    columnStrategy: IgcNoopPivotDimensionsStrategy.instance(),
    rowStrategy: IgcNoopPivotDimensionsStrategy.instance(),
    columns: [...],
    rows: [...],
    values: [...],
    filters: [...]
};
private gridState: IgcGridStateComponent;

constructor() {
    var grid = document.getElementById("grid") as IgcPivotGridComponent;
    this.gridState = document.getElementById('gridState') as IgcGridStateComponent;
    grid.pivotConfiguration = this.pivotConfiguration;
    PivotNoopData.getData().then((value) => {
        grid.data = value;
    });
    this.gridState.addEventListener('stateParsed', (ev:any) => this.stateParsedHandler(ev) );
}
  • LocalStorage から状態を復元し、カスタム ストラテジを適用する方法は次のとおりです:
public restoreGridState() {
    const state = window.localStorage.getItem(this.stateKey);
    if (state) {
        this.gridState.applyStateFromString(state);
    }
}

public stateParsedHandler(ev: any) {
    const parsedState = ev.detail;
    parsedState.pivotConfiguration.rowStrategy = IgcNoopPivotDimensionsStrategy.instance();
    parsedState.pivotConfiguration.columnStrategy = IgcNoopPivotDimensionsStrategy.instance();
}

制限