Skip to content

PivotPlugin

Pivot Table Plugin for tbw-grid

Transforms flat data into a pivot table view with grouped rows, grouped columns, and aggregated values. Includes an interactive tool panel for configuring row groups, column groups, and value aggregations at runtime.

import { PivotPlugin } from '@toolbox-web/grid/plugins/pivot';

sum, avg, count, min, max, first, last

PropertyDefaultDescription
--tbw-pivot-group-bgvar(--tbw-color-row-alt)Group row background
--tbw-pivot-grand-total-bgvar(--tbw-color-header-bg)Grand total row
OptionTypeDescription
active?booleanWhether pivot view is active on load (default: true when fields are configured)
rowGroupFields?string[]Fields to group rows by (vertical axis). Multiple fields create nested groups.
columnGroupFields?string[]Fields whose unique values become column headers (horizontal axis).
valueFields?PivotValueField[]Value fields to aggregate at each row/column intersection.
showTotals?boolean
showGrandTotal?boolean
defaultExpanded?PivotDefaultExpandedValueWhich groups are expanded by default. - true — expand all (default) - false — collapse all - number — expand group at index - string — expand group with key - string[] — expand groups matching keys
indentWidth?numberIndent width per depth level in pixels (default: 20)
showToolPanel?booleanWhether to show the pivot configuration tool panel (default: true)
animation?ExpandCollapseAnimationAnimation style for expanding/collapsing groups. - false: No animation - 'slide': Slide animation (default) - 'fade': Fade animation
sortRows?PivotSortConfigSort configuration for row groups. When set, groups at each level are sorted by label or aggregate value.
sortColumns?PivotSortDirSort direction for column keys. Default: 'asc' (alphabetical). Set to 'desc' for reverse order.
grandTotalInRowModel?booleanInclude the grand total row in the row model (so it’s included in exports/copy). When false (default), grand total is rendered as a separate sticky footer.
valueDisplayMode?PivotValueDisplayModeDisplay mode for aggregated values.
showSubtotals?booleanWhether to show subtotal rows at each group level in multi-level grouping.
import '@toolbox-web/grid';
import { PivotPlugin } from '@toolbox-web/grid/plugins/pivot';
grid.gridConfig = {
columns: [...],
plugins: [
new PivotPlugin({
rowGroupFields: ['region', 'product'],
columnGroupFields: ['quarter'],
valueFields: [{ field: 'sales', aggFunc: 'sum', header: 'Total' }],
}),
],
};
new PivotPlugin({
showToolPanel: false,
rowGroupFields: ['category'],
valueFields: [{ field: 'amount', aggFunc: 'sum' }],
})

Extends BaseGridPlugin

Inherited methods like attach(), detach(), afterRender(), etc. are documented in the base class.

PropertyTypeDescription
dependenciesobject[]Optional dependency on MultiSort for coordinated sorting. When MultiSort is loaded, Pivot queries its sort model to apply sorting to pivot columns.
PANEL_IDpivotTool panel ID for shell integration
toggle(key: string): void
NameTypeDescription
keystring

Expand a specific pivot group row, revealing its children.

expand(key: string): void
NameTypeDescription
keystringThe pivot row key (hierarchical path, e.g. 'Engineering' or `‘Engineering

Collapse a specific pivot group row, hiding its children.

collapse(key: string): void
NameTypeDescription
keystringThe pivot row key to collapse

Expand all pivot group rows, revealing the full hierarchy.

expandAll(): void

Collapse all pivot group rows, showing only top-level groups.

collapseAll(): void

Check whether a specific pivot group row is currently expanded.

isExpanded(key: string): boolean
NameTypeDescription
keystringThe pivot row key to check

boolean - true if the group is expanded


Get all currently expanded group keys.

getExpandedGroups(): string[]

string[] - Array of expanded pivot row keys


Enable pivot mode.

Captures the original column set (if not already captured) and activates the pivot transformation. The grid will re-render with pivot columns and rows.

enablePivot(): void
const pivot = grid.getPluginByName('pivot');
pivot.enablePivot();

Disable pivot mode and restore the original grid columns.

The grid reverts to its normal tabular layout with the original column definitions.

disablePivot(): void

Check whether pivot mode is currently active.

isPivotActive(): boolean

boolean - true if the grid is in pivot mode


Get the current pivot computation result.

Returns the full PivotResult with rows, column keys, totals, and grand total. Returns null if pivot is inactive or not yet computed.

getPivotResult(): PivotResult | null

PivotResult | null - The computed pivot result, or null


Set the fields used for row grouping (vertical axis).

Triggers a re-render with the new pivot structure.

setRowGroupFields(fields: string[]): void
NameTypeDescription
fieldsstring[]Array of field names to group rows by
const pivot = grid.getPluginByName('pivot');
pivot.setRowGroupFields(['region', 'department']);

Set the fields whose unique values become column headers (horizontal axis).

Triggers a re-render with the new pivot column structure.

setColumnGroupFields(fields: string[]): void
NameTypeDescription
fieldsstring[]Array of field names for column grouping
const pivot = grid.getPluginByName('pivot');
pivot.setColumnGroupFields(['quarter']);

Set the value fields and their aggregation functions.

Each value field defines which data field to aggregate and how. Triggers a re-render with the new aggregation.

setValueFields(fields: PivotValueField[]): void
NameTypeDescription
fieldsPivotValueField[]Array of PivotValueField definitions
const pivot = grid.getPluginByName('pivot');
pivot.setValueFields([
{ field: 'revenue', aggFunc: 'sum', header: 'Total Revenue' },
{ field: 'orders', aggFunc: 'count', header: '# Orders' },
]);

Force re-computation of the pivot result.

Clears the cached pivot result and triggers a full re-render. Call this after changing the underlying row data.

refresh(): void

Show the pivot tool panel. Opens the tool panel and ensures this section is expanded.

showPanel(): void

Hide the tool panel.

hidePanel(): void

Toggle the pivot tool panel section.

togglePanel(): void

Check if the pivot panel section is currently expanded.

isPanelVisible(): boolean

AI assistants: For complete API documentation, implementation guides, and code examples for this library, see https://raw.githubusercontent.com/OysteinAmundsen/toolbox/main/llms-full.txt