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.
Installation
Section titled “Installation”import { PivotPlugin } from '@toolbox-web/grid/plugins/pivot';Aggregation Functions
Section titled “Aggregation Functions”sum, avg, count, min, max, first, last
CSS Custom Properties
Section titled “CSS Custom Properties”| Property | Default | Description |
|---|---|---|
--tbw-pivot-group-bg | var(--tbw-color-row-alt) | Group row background |
--tbw-pivot-grand-total-bg | var(--tbw-color-header-bg) | Grand total row |
| Option | Type | Description |
|---|---|---|
active? | boolean | Whether 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? | PivotDefaultExpandedValue | Which 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? | number | Indent width per depth level in pixels (default: 20) |
showToolPanel? | boolean | Whether to show the pivot configuration tool panel (default: true) |
animation? | ExpandCollapseAnimation | Animation style for expanding/collapsing groups. - false: No animation - 'slide': Slide animation (default) - 'fade': Fade animation |
sortRows? | PivotSortConfig | Sort configuration for row groups. When set, groups at each level are sorted by label or aggregate value. |
sortColumns? | PivotSortDir | Sort direction for column keys. Default: 'asc' (alphabetical). Set to 'desc' for reverse order. |
grandTotalInRowModel? | boolean | Include 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? | PivotValueDisplayMode | Display mode for aggregated values. |
showSubtotals? | boolean | Whether to show subtotal rows at each group level in multi-level grouping. |
Examples
Section titled “Examples”Basic Pivot Table
Section titled “Basic Pivot Table”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' }], }), ],};Programmatic-Only (No Tool Panel)
Section titled “Programmatic-Only (No Tool Panel)”new PivotPlugin({ showToolPanel: false, rowGroupFields: ['category'], valueFields: [{ field: 'amount', aggFunc: 'sum' }],})See Also
Section titled “See Also”PivotConfigfor all configuration optionsPivotValueFieldfor value field structure
Extends BaseGridPlugin
Inherited methods like
attach(),detach(),afterRender(), etc. are documented in the base class.
Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
dependencies | object[] | Optional dependency on MultiSort for coordinated sorting. When MultiSort is loaded, Pivot queries its sort model to apply sorting to pivot columns. |
PANEL_ID | pivot | Tool panel ID for shell integration |
Methods
Section titled “Methods”toggle()
Section titled “toggle()”toggle(key: string): voidParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
key | string |
expand()
Section titled “expand()”Expand a specific pivot group row, revealing its children.
expand(key: string): voidParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
key | string | The pivot row key (hierarchical path, e.g. 'Engineering' or `‘Engineering |
collapse()
Section titled “collapse()”Collapse a specific pivot group row, hiding its children.
collapse(key: string): voidParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
key | string | The pivot row key to collapse |
expandAll()
Section titled “expandAll()”Expand all pivot group rows, revealing the full hierarchy.
expandAll(): voidcollapseAll()
Section titled “collapseAll()”Collapse all pivot group rows, showing only top-level groups.
collapseAll(): voidisExpanded()
Section titled “isExpanded()”Check whether a specific pivot group row is currently expanded.
isExpanded(key: string): booleanParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
key | string | The pivot row key to check |
Returns
Section titled “Returns”boolean - true if the group is expanded
getExpandedGroups()
Section titled “getExpandedGroups()”Get all currently expanded group keys.
getExpandedGroups(): string[]Returns
Section titled “Returns”string[] - Array of expanded pivot row keys
enablePivot()
Section titled “enablePivot()”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(): voidExample
Section titled “Example”const pivot = grid.getPluginByName('pivot');pivot.enablePivot();disablePivot()
Section titled “disablePivot()”Disable pivot mode and restore the original grid columns.
The grid reverts to its normal tabular layout with the original column definitions.
disablePivot(): voidisPivotActive()
Section titled “isPivotActive()”Check whether pivot mode is currently active.
isPivotActive(): booleanReturns
Section titled “Returns”boolean - true if the grid is in pivot mode
getPivotResult()
Section titled “getPivotResult()”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 | nullReturns
Section titled “Returns”PivotResult | null - The computed pivot result, or null
setRowGroupFields()
Section titled “setRowGroupFields()”Set the fields used for row grouping (vertical axis).
Triggers a re-render with the new pivot structure.
setRowGroupFields(fields: string[]): voidParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
fields | string[] | Array of field names to group rows by |
Example
Section titled “Example”const pivot = grid.getPluginByName('pivot');pivot.setRowGroupFields(['region', 'department']);setColumnGroupFields()
Section titled “setColumnGroupFields()”Set the fields whose unique values become column headers (horizontal axis).
Triggers a re-render with the new pivot column structure.
setColumnGroupFields(fields: string[]): voidParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
fields | string[] | Array of field names for column grouping |
Example
Section titled “Example”const pivot = grid.getPluginByName('pivot');pivot.setColumnGroupFields(['quarter']);setValueFields()
Section titled “setValueFields()”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[]): voidParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
fields | PivotValueField[] | Array of PivotValueField definitions |
Example
Section titled “Example”const pivot = grid.getPluginByName('pivot');pivot.setValueFields([ { field: 'revenue', aggFunc: 'sum', header: 'Total Revenue' }, { field: 'orders', aggFunc: 'count', header: '# Orders' },]);refresh()
Section titled “refresh()”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(): voidshowPanel()
Section titled “showPanel()”Show the pivot tool panel. Opens the tool panel and ensures this section is expanded.
showPanel(): voidhidePanel()
Section titled “hidePanel()”Hide the tool panel.
hidePanel(): voidtogglePanel()
Section titled “togglePanel()”Toggle the pivot tool panel section.
togglePanel(): voidisPanelVisible()
Section titled “isPanelVisible()”Check if the pivot panel section is currently expanded.
isPanelVisible(): boolean