# PivotController (@salmonui/pivot-table)

> The PivotController class of @salmonui/pivot-table: The store of one pivot report. Create it with usePivot(); pass it to the pivot parts as pivot.

Canonical: https://salmonui.dev/docs/reference/pivot-table/PivotController

```ts
import { PivotController } from '@salmonui/pivot-table';
```

Entry index: https://salmonui.dev/docs/reference/pivot-table (Markdown with every symbol of the entry: https://salmonui.dev/docs/reference/pivot-table.md).

## Reference

Kind: class

```ts
class PivotController<Row = unknown> {
    handlers: Handlers<Row>;
    private options;
    private state;
    private readonly listeners;
    private engine;
    private unsubscribeEngine;
    windowed: WindowedPivot | null;
    private memberCache;
    private data;
    private initial;
    private mounted;
    private appliedPropKey;
    private pending;
    constructor(options: UsePivotOptions<Row>);
    readonly subscribe: (listener: Listener) => (() => void);
    readonly getSnapshot: () => PivotState;
    get result(): PivotResult | null;
    get status(): PivotState['status'];
    get model(): PivotModel;
    getData(): readonly Row[];
    private set;
    private engineOptions;
    private createEngine;
    private readonly syncFromEngine;
    mount(): () => void;
    private isLarge;
    private whenReady;
    updateOptions(options: UsePivotOptions<Row>): void;
    applyModelProp(input: PivotModelInput | undefined): void;
    applyDataProp(data: readonly Row[] | undefined): void;
    private event;
    private allowed;
    private emitCompute;
    private emitError;
    private run;
    setModel(input: PivotModelInput | ((model: PivotModel) => PivotModelInput), options?: PivotCommandOptions & {
        readonly action?: PivotAction;
        readonly field?: string | null;
    }): boolean;
    private commit;
    applyDraft(source?: PivotEventSource): boolean;
    cancelDraft(): void;
    beginDraft(): void;
    moveField(field: string, to: PivotAxis | null, index?: number, options?: PivotCommandOptions): boolean;
    stepField(field: string, axis: PivotAxis, step: -1 | 1, options?: PivotCommandOptions): boolean;
    setFilter(field: string, filter: PivotFilter | null, options?: PivotCommandOptions): boolean;
    setSort(field: string, sort: PivotMemberSort, options?: PivotCommandOptions): boolean;
    setValueSort(valueSort: PivotValueSort | null, options?: PivotCommandOptions): boolean;
    setAggregate(index: number, aggregate: PivotAggregate, options?: PivotCommandOptions): boolean;
    aggregateMenuItems(field: string, index: number, items: PivotAggregate[], source?: PivotEventSource): PivotAggregate[] | null;
    setCalculatedField(name: string, calculatedField: PivotCalculatedField | null, options?: PivotCommandOptions & {
        readonly addToValues?: boolean;
    }): boolean;
    setFormat(field: string, format: PivotFormat | null, options?: PivotCommandOptions): boolean;
    setConditionalFormats(conditionalFormats: readonly PivotConditionalFormat[], options?: PivotCommandOptions): boolean;
    setGrouping(field: string, grouping: PivotGrouping | null, options?: PivotCommandOptions): boolean;
    setTotals(totals: Partial<PivotTotals>, options?: PivotCommandOptions): boolean;
    private isExpanded;
    expand(path: PivotMemberPath, options?: PivotCommandOptions): boolean;
    collapse(path: PivotMemberPath, options?: PivotCommandOptions): boolean;
    toggle(node: PivotHeaderNode, options?: PivotCommandOptions): boolean;
    private drill;
    private expandedPathsFromResult;
    expandAll(axis?: 'rows' | 'columns', options?: PivotCommandOptions): boolean;
    collapseAll(axis?: 'rows' | 'columns', options?: PivotCommandOptions): boolean;
    private drillAll;
    setSelection(cells: readonly PivotCellKey[], options?: PivotCommandOptions & {
        readonly confirmed?: boolean;
    }): boolean;
    getMembers(field: string, options?: {
        search?: string;
        limit?: number;
    }): readonly PivotMember[];
    getMembersAsync(field: string, options?: {
        search?: string;
        limit?: number;
    }): Promise<readonly PivotMember[]>;
    getLoadedMembers(field: string): readonly PivotMember[] | null;
    getCellRows(rowIndex: number, columnIndex: number): readonly Row[];
    getCellRowsAsync(rowIndex: number, columnIndex: number): Promise<readonly Row[]>;
    getCellRowIndexes(rowIndex: number, columnIndex: number): readonly number[];
    drillThrough(rowIndex: number, columnIndex: number, options?: PivotCommandOptions & {
        readonly openDialog?: boolean;
    }): boolean;
    updateRows(changes: readonly {
        readonly index: number;
        readonly row: Row | null;
    }[], action?: PivotDataChangeEvent<Row>['action']): void;
    refresh(): void;
    openDialog(dialog: PivotDialogState, source?: PivotEventSource): boolean;
    closeDialog(): void;
    getState(extra?: Omit<PivotTableState, 'version' | 'pivotModel'>): PivotTableState;
    restoreState(state: PivotTableState | string, source?: PivotEventSource): boolean;
    get isMounted(): boolean;
}
```

The store of one pivot report. Create it with `usePivot()`; pass it to the
pivot parts as `pivot`.

Members (75):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `handlers` | `handlers: Handlers<Row>;` |  | Latest event handlers (replaced on every `usePivot()` render, read when an event fires). |
| `options` | `private options;` |  |  |
| `state` | `private state;` |  |  |
| `listeners` | `private readonly listeners;` |  |  |
| `engine` | `private engine;` |  |  |
| `unsubscribeEngine` | `private unsubscribeEngine;` |  |  |
| `windowed` | `windowed: WindowedPivot \| null;` |  | Window adapter of an asynchronous (Worker) engine; null for the local engine. |
| `memberCache` | `private memberCache;` |  |  |
| `data` | `private data;` |  |  |
| `initial` | `private initial;` |  |  |
| `mounted` | `private mounted;` |  |  |
| `appliedPropKey` | `private appliedPropKey;` |  |  |
| `pending` | `private pending;` |  |  |
| `subscribe` | `readonly subscribe: (listener: Listener) => (() => void);` |  |  |
| `getSnapshot` | `readonly getSnapshot: () => PivotState;` |  |  |
| `result` | `get result(): PivotResult \| null;` |  | Current result (null before the first compute). |
| `status` | `get status(): PivotState['status'];` |  |  |
| `model` | `get model(): PivotModel;` |  |  |
| `getData` | `getData(): readonly Row[];` |  | Raw rows the engine aggregates (after drill-through edits). |
| `set` | `private set;` |  |  |
| `engineOptions` | `private engineOptions;` |  |  |
| `createEngine` | `private createEngine;` |  |  |
| `syncFromEngine` | `private readonly syncFromEngine;` |  | Reads the engine snapshot into the store (also the engine listener). |
| `mount` | `mount(): () => void;` |  | Called from an effect: subscribes to the engine, starts a deferred compute, reports the initial compute. |
| `isLarge` | `private isLarge;` |  |  |
| `whenReady` | `private whenReady;` |  |  |
| `updateOptions` | `updateOptions(options: UsePivotOptions<Row>): void;` |  | Latest options (from `usePivot()` renders): handlers and creation-only fields. |
| `applyModelProp` | `applyModelProp(input: PivotModelInput \| undefined): void;` |  | Applies the reactive `pivotModel` prop when it changed structurally. |
| `applyDataProp` | `applyDataProp(data: readonly Row[] \| undefined): void;` |  | Applies a new `data` prop (identity change). |
| `event` | `private event;` |  |  |
| `allowed` | `private allowed;` |  | Fires a before-event; true when it was not cancelled. |
| `emitCompute` | `private emitCompute;` |  |  |
| `emitError` | `private emitError;` |  |  |
| `run` | `private run;` |  | Runs an engine command with the compute events, the status, and error reporting. |
| `setModel` | `setModel(input: PivotModelInput \| ((model: PivotModel) => PivotModelInput), options?: PivotCommandOptions & {     readonly action?: PivotAction;     readonly field?: string \| null; }): boolean;` |  | Applies a model (cancellable through `onBeforePivotModelChange`). Returns false when the change was cancelled or changed nothing. |
| `commit` | `private commit;` |  | Shared commit path: model before-event, the engine command, the after-event. `apply` runs a specialized engine command (expand, collapse) instead of `setModel`. |
| `applyDraft` | `applyDraft(source?: PivotEventSource): boolean;` |  | Commits the draft model of a deferred field list (Apply). |
| `cancelDraft` | `cancelDraft(): void;` |  | Drops the draft model (Cancel). |
| `beginDraft` | `beginDraft(): void;` |  | Starts a draft from the applied model (a deferred field list opening). |
| `moveField` | `moveField(field: string, to: PivotAxis \| null, index?: number, options?: PivotCommandOptions): boolean;` |  | Moves a field between axes or inside one; `to: null` removes it (cancellable through `onBeforeFieldMove`). |
| `stepField` | `stepField(field: string, axis: PivotAxis, step: -1 \| 1, options?: PivotCommandOptions): boolean;` |  | Moves a field one step inside its axis (Alt+ArrowUp / ArrowDown). |
| `setFilter` | `setFilter(field: string, filter: PivotFilter \| null, options?: PivotCommandOptions): boolean;` |  | Member, label or value filter of a field; null clears it (cancellable through `onBeforeFilterChange`). |
| `setSort` | `setSort(field: string, sort: PivotMemberSort, options?: PivotCommandOptions): boolean;` |  | Member sort of a field (cancellable through `onBeforeSortChange`). |
| `setValueSort` | `setValueSort(valueSort: PivotValueSort \| null, options?: PivotCommandOptions): boolean;` |  | Value sort; null clears it. |
| `setAggregate` | `setAggregate(index: number, aggregate: PivotAggregate, options?: PivotCommandOptions): boolean;` |  | Aggregate of the value field at `index`. |
| `aggregateMenuItems` | `aggregateMenuItems(field: string, index: number, items: PivotAggregate[], source?: PivotEventSource): PivotAggregate[] \| null;` |  | Aggregates the menu offers; `onBeforeAggregateMenuOpen` may cancel or edit `items`. Null when cancelled. |
| `setCalculatedField` | `setCalculatedField(name: string, calculatedField: PivotCalculatedField \| null, options?: PivotCommandOptions & {     readonly addToValues?: boolean; }): boolean;` |  | Adds or replaces a calculated field (by `name`), or removes it (`calculatedField: null`). |
| `setFormat` | `setFormat(field: string, format: PivotFormat \| null, options?: PivotCommandOptions): boolean;` |  | Number or date format of a value field or a member field (null clears it). |
| `setConditionalFormats` | `setConditionalFormats(conditionalFormats: readonly PivotConditionalFormat[], options?: PivotCommandOptions): boolean;` |  |  |
| `setGrouping` | `setGrouping(field: string, grouping: PivotGrouping \| null, options?: PivotCommandOptions): boolean;` |  | Number range, date or custom grouping of a field; null ungroups it. |
| `setTotals` | `setTotals(totals: Partial<PivotTotals>, options?: PivotCommandOptions): boolean;` |  |  |
| `isExpanded` | `private isExpanded;` |  |  |
| `expand` | `expand(path: PivotMemberPath, options?: PivotCommandOptions): boolean;` |  | Expands a member (incremental in the engine). |
| `collapse` | `collapse(path: PivotMemberPath, options?: PivotCommandOptions): boolean;` |  |  |
| `toggle` | `toggle(node: PivotHeaderNode, options?: PivotCommandOptions): boolean;` |  | Expands or collapses a header node. |
| `drill` | `private drill;` |  |  |
| `expandedPathsFromResult` | `private expandedPathsFromResult;` |  | Paths of every expanded node of the current result (to turn `expanded: 'all'` into a list). |
| `expandAll` | `expandAll(axis?: 'rows' \| 'columns', options?: PivotCommandOptions): boolean;` |  | Expands every member of one axis or both (Ctrl+ArrowDown). |
| `collapseAll` | `collapseAll(axis?: 'rows' \| 'columns', options?: PivotCommandOptions): boolean;` |  |  |
| `drillAll` | `private drillAll;` |  |  |
| `setSelection` | `setSelection(cells: readonly PivotCellKey[], options?: PivotCommandOptions & {     readonly confirmed?: boolean; }): boolean;` |  | Replaces the selected cells (cancellable through `onBeforeSelectionChange`). |
| `getMembers` | `getMembers(field: string, options?: {     search?: string;     limit?: number; }): readonly PivotMember[];` |  | Members of a field for the member editor: every member, searchable, no node limit. |
| `getMembersAsync` | `getMembersAsync(field: string, options?: {     search?: string;     limit?: number; }): Promise<readonly PivotMember[]>;` |  | Members, waiting for an asynchronous engine. |
| `getLoadedMembers` | `getLoadedMembers(field: string): readonly PivotMember[] \| null;` |  | Members already loaded by `getMembersAsync()` (null when not loaded yet). |
| `getCellRows` | `getCellRows(rowIndex: number, columnIndex: number): readonly Row[];` |  | Raw rows behind a value cell. |
| `getCellRowsAsync` | `getCellRowsAsync(rowIndex: number, columnIndex: number): Promise<readonly Row[]>;` |  | Raw rows behind a value cell, waiting for an asynchronous engine. |
| `getCellRowIndexes` | `getCellRowIndexes(rowIndex: number, columnIndex: number): readonly number[];` |  | Raw row indexes behind a value cell. |
| `drillThrough` | `drillThrough(rowIndex: number, columnIndex: number, options?: PivotCommandOptions & {     readonly openDialog?: boolean; }): boolean;` |  | Opens drill through for a value cell (cancellable through `onBeforeDrillThrough`). `openDialog: false` only fires the events (an app with its own UI). |
| `updateRows` | `updateRows(changes: readonly {     readonly index: number;     readonly row: Row \| null; }[], action?: PivotDataChangeEvent<Row>['action']): void;` |  | Writes edited raw rows (drill-through editing): replaces the rows at `index`, appends `index: -1` rows, removes `row: null` rows, then re-aggregates incrementally and fires `onDataChange`. |
| `refresh` | `refresh(): void;` |  | Re-reads the data and recomputes. |
| `openDialog` | `openDialog(dialog: PivotDialogState, source?: PivotEventSource): boolean;` |  |  |
| `closeDialog` | `closeDialog(): void;` |  |  |
| `getState` | `getState(extra?: Omit<PivotTableState, 'version' \| 'pivotModel'>): PivotTableState;` |  |  |
| `restoreState` | `restoreState(state: PivotTableState \| string, source?: PivotEventSource): boolean;` |  | Applies a saved state; false (with `onError`) when it is not a pivot state of a known version. |
| `isMounted` | `get isMounted(): boolean;` |  | True while mounted (effects ran). |
