Rowpine for TanStack Table
Buy a license
DocsAPI reference

Pivot designer state

The fields, layouts and presets behind the pivot designer, and the pure functions that change a layout.

rowpine 1.0.3Generated from the package's type definitions

PivotField<T = Record<string, unknown>>

type

One field the designer offers. Dimensions land in Rows by default; measures in Values.

Properties

NameTypeDescription
fieldkeyof T & stringKey of the field in each data row.
labelstringName shown in the field list and on chips.
kind"dimension" | "measure"'dimension' may go to Rows, Columns or Filters; 'measure' to Values or Filters.
groupoptionalstringOptional group heading in the field list, e.g. 'Customer'.
formatoptional((value: unknown) => string)Display text for this field's values when it is a measure.
defaultAggoptionalPivotAggregatorIdAggregation used when the field is added to Values. Default 'sum' for measures.

PivotLayout

type

The whole designer state, as plain JSON.

Properties

NameTypeDescription
rowsstring[]Fields in the Rows well, outer-most first.
colsstring[]Fields in the Columns well, outer-most first.
valuesPivotValueChip[]Chips in the Values well, in column order.
filtersPivotFilterChip[]Chips in the Filters well.
hideSubtotalsoptionalbooleanShow no numbers on group rows. The group rows stay and still expand and collapse.
hideGrandTotalsoptionalbooleanHide the grand-total row and column.

PivotPreset

type

A saved layout offered in the designer's Presets menu.

Properties

NameTypeDescription
namestringName shown in the Presets menu.
layoutPivotLayoutThe layout applied when the preset is picked.

PivotValueChip

type

A chip in the Values well.

Properties

NameTypeDescription
fieldstringThe field it aggregates.
aggPivotAggregatorIdHow it aggregates the field.
labeloptionalstringColumn header. The designer functions set it to e.g. 'Sum of Amount'; when missing, the pivot uses that form.

PivotFilterChip

type

A chip in the Filters well. allowed: null lets every value through.

Properties

NameTypeDescription
fieldstringThe field it filters on.
allowedstring[] | nullValues that pass, compared as text (String(value)). An empty array lets nothing through.

Well

type

One of the designer's four wells.

Type
type Well = 'rows'
  | 'cols'
  | 'values'
  | 'filters'

EMPTY_LAYOUT

constant

A layout with every well empty.

Type PivotLayout

ALL_AGGREGATORS

constant

Every built-in aggregator id, in the order the designer lists them.

Type PivotAggregatorId[]

AGG_LABEL

constant

Display name of each aggregator, e.g. 'Average' for 'avg'. Value chip labels are built from it.

Type Record<PivotAggregatorId, string>

KIND_WELLS

constant

Which wells each kind of field may go to.

Type Record<"dimension" | "measure", Well[]>

WELL_LABEL

constant

Display name of each well: 'Rows', 'Columns', 'Values', 'Filters'.

Type Record<Well, string>

defaultLayoutFor

function

First dimension in Rows, first measure in Values (with its defaultAgg, else 'sum').

Signature
defaultLayoutFor<T>(fields: ReadonlyArray<PivotField<T>>) => PivotLayout

Parameters

NameTypeDescription
fieldsreadonly PivotField<T>[]The fields the designer offers.

Returns

PivotLayout

defaultWellFor

function

The well a field goes to when added without one: Values for a measure, Rows otherwise.

Signature
defaultWellFor(field: Pick<PivotField, "kind">) => Well

Parameters

NameTypeDescription
fieldPick<PivotField<Record<string, unknown>>, "kind">The field, or anything with its kind.

Returns

Well

layoutToConfig

function

The engine config for a layout. Value labels come from the chips, and a measure's format is used except on count and distinct count. hideSubtotals and hideGrandTotals turn off the matching config switches.

Signature
layoutToConfig<T>(
  layout: PivotLayout,
  fields: ReadonlyArray<PivotField<T>>,
) => PivotConfig<T>

Parameters

NameTypeDescription
layoutPivotLayoutThe layout.
fieldsreadonly PivotField<T>[]The fields the designer offers, for labels and formats.

Returns

PivotConfig<T>

pivotFromLayout

function

Filter, pivot and label in one step. Returns null when the layout has no values yet, which a UI shows as an empty state. Runs createPivot, so without a license key it logs a notice once.

Signature
pivotFromLayout<T>(
  data: ReadonlyArray<T>,
  layout: PivotLayout,
  fields: ReadonlyArray<PivotField<T>>,
  opts?: ToColumnsOptions,
) => PivotTable | null

Parameters

NameTypeDescription
datareadonly T[]The source rows. Not changed.
layoutPivotLayoutThe layout to build.
fieldsreadonly PivotField<T>[]The fields the designer offers. Their labels name the row-header column.
optsoptionalToColumnsOptionsSettings for every value column, such as an Excel number format.

Returns

PivotTable | null

applyLayoutFilters

function

The rows that pass the layout's filters. Returns a new array.

Signature
applyLayoutFilters<T>(data: ReadonlyArray<T>, layout: PivotLayout) => T[]

Parameters

NameTypeDescription
datareadonly T[]The source rows. Not changed.
layoutPivotLayoutThe layout whose Filters well applies.

Returns

T[]

addToWell

function

Add a field to a well at index (default: the end). In Rows, Columns and Filters a field appears at most once, so adding it moves it. In Values the same field may appear with different aggregations (sum and average of one amount), but not twice with the same one. sourceIndex is the chip's position in Values when it is being moved out of that well. Returns the layout unchanged when field is not in fields.

Signature
addToWell<T>(
  layout: PivotLayout,
  fields: ReadonlyArray<PivotField<T>>,
  field: string,
  well: Well,
  index?: number,
  sourceIndex?: number,
) => PivotLayout

Parameters

NameTypeDescription
layoutPivotLayoutThe current layout. Not changed.
fieldsreadonly PivotField<T>[]The fields the designer offers.
fieldstringKey of the field to add.
wellWellThe well to add it to.
indexoptionalnumberPosition in the well. Default: the end.
sourceIndexoptionalnumberWhen moving a chip out of Values, its position there. A chip moved within Values keeps its aggregation.

Returns

PivotLayout

removeFromWell

function

Remove a field from a well. In Values, valueIndex picks the one chip.

Signature
removeFromWell(
  layout: PivotLayout,
  field: string,
  well: Well,
  valueIndex?: number,
) => PivotLayout

Parameters

NameTypeDescription
layoutPivotLayoutThe current layout. Not changed.
fieldstringKey of the field to remove.
wellWellThe well to remove it from.
valueIndexoptionalnumberIn Values, the chip's position. Without it every chip of the field is removed.

Returns

PivotLayout

clearWell

function

Empty one well.

Signature
clearWell(layout: PivotLayout, well: Well) => PivotLayout

Parameters

NameTypeDescription
layoutPivotLayoutThe current layout. Not changed.
wellWellThe well to empty.

Returns

PivotLayout

toggleField

function

Field-list checkbox: take the field out of every well, or add it to its default well.

Signature
toggleField<T>(
  layout: PivotLayout,
  fields: ReadonlyArray<PivotField<T>>,
  field: string,
) => PivotLayout

Parameters

NameTypeDescription
layoutPivotLayoutThe current layout. Not changed.
fieldsreadonly PivotField<T>[]The fields the designer offers. A field not in it leaves the layout unchanged.
fieldstringKey of the field to toggle.

Returns

PivotLayout

isFieldInLayout

function

True when the field is in any well.

Signature
isFieldInLayout(layout: PivotLayout, field: string) => boolean

Parameters

NameTypeDescription
layoutPivotLayoutThe current layout.
fieldstringThe field key.

Returns

boolean

setAggregatorAt

function

Change a value chip's aggregation. A change that would duplicate another chip removes this one. The chip's label is rebuilt from the new aggregation.

Signature
setAggregatorAt<T>(
  layout: PivotLayout,
  fields: ReadonlyArray<PivotField<T>>,
  index: number,
  agg: PivotAggregatorId,
) => PivotLayout

Parameters

NameTypeDescription
layoutPivotLayoutThe current layout. Not changed.
fieldsreadonly PivotField<T>[]The fields the designer offers, for the label.
indexnumberThe chip's position in Values. Out of range leaves the layout unchanged.
aggPivotAggregatorIdThe new aggregation.

Returns

PivotLayout

setFilterAllowed

function

Set which values of a filter field pass. null lets everything through.

Signature
setFilterAllowed(
  layout: PivotLayout,
  field: string,
  allowed: string[] | null,
) => PivotLayout

Parameters

NameTypeDescription
layoutPivotLayoutThe current layout. Not changed.
fieldstringKey of a field in the Filters well. Any other field has no effect.
allowedstring[] | nullThe values that pass, as text, or null for all.

Returns

PivotLayout

distinctValues

function

Every distinct value of a field in the full data, as text, sorted.

Signature
distinctValues<T>(data: ReadonlyArray<T>, field: string) => string[]

Parameters

NameTypeDescription
datareadonly T[]The source rows.
fieldstringThe field key.

Returns

string[]