Pivot State Persistence
Enabling pivots dynamically creates columns. Control pivot state to customize the pivot configuration to your requirements.
The PivotState type defines the pivot configuration. It includes column-specific
state and expansion states for row and column groups.
export interface PivotState { readonly columnState: { readonly ordering: string[]; readonly resizing: Record<string, number>; readonly pinning: Record<string, ColumnPin>; }; readonly columnGroupState: Record<string, boolean>; readonly rowGroupExpansions: Record<string, boolean | undefined>;}By default, the client data source maintains and controls the pivot state internally. This means pivots support features such as re-ordering and resizing without requiring any external hooks.
Controlled Pivot State
Set pivotState on the client data source to manage pivot state.
To keep it in sync, handle onPivotStateChange and write the
updated state back to pivotState.
The demo below shows a measure summing Profit, with rows pivoted by Country and Product, and columns by Age Group. It demonstrates controlled pivot state. This configuration behaves identically to the default (uncontrolled) approach, but the state is now exposed to your application.
The code to control the pivot state is shown below. The initial state starts with the Australia row
group collapsed. To control pivot state, the onPivotStateChange is set to the setPivotState function
returned by React’s useState hook.
const [pivotState, setPivotState] = useState<Grid.T.PivotState>({ columnGroupState: {}, columnState: { ordering: [], pinning: {}, resizing: {} }, rowGroupExpansions: { Australia: false },});
const ds = useClientDataSource<GridSpec>({ data: salesData, pivotMode: true, pivotState, onPivotStateChange: setPivotState, pivotModel: { columns: [{ id: "ageGroup" }], rows: [{ id: "country" }, { id: "productCategory" }], measures: [ { dim: { id: "profit", name: "Profit", type: "number", cellRenderer: ProfitCell, width: 140 }, fn: "sum", }, ], }, rowGroupDefaultExpansion: true, aggregateFns: { sum: aggSum },});Warning
Only control pivot state when your use case requires it. Do not use controlled pivot state
to modify columns generated by the client data source. To adjust
generated columns, use pivotColumnProcessor instead.
Next Steps
- Row & Column Pivots: Configure row and column dimensions for pivot view.
- Measures: Aggregate and summarize row data when pivoting.
- Pivot Sorting: Customize the sort order of pivoted row groups.
