AdaptTable for Unstyled

Pivot tables in Unstyled

Grouping answers “what is the total per team”. A pivot answers “what is the total per team per status”, and that second dimension becomes columns your data never had.

Fields move between the three zones with buttons rather than drag, so the pivot can be built from the keyboard. Subtotals close every group and a grand total closes the table.

The configuration — axes, measures, aggregation and what you folded — lives in the URL, so a pivot you build is a pivot you can send someone.

The code

import { Component, computed, input } from "@angular/core";
import { AdaptDataTable } from "@adapttable/angular-unstyled";
import {
  AdaptPivotPanel, injectPivotUrlState, pivot, pivotTableModel,
  type PivotField,
} from "@adapttable/angular-unstyled/pivot";

@Component({
  selector: "app-spend",
  imports: [AdaptDataTable, AdaptPivotPanel],
  template: `
    <adapt-pivot-panel
      [fields]="fields" [config]="state.config()"
      [onChange]="state.onConfigChange"
    />
    <adapt-data-table
      [data]="model().rows" [columns]="model().columns"
      [rowKey]="model().rowKey" [summaryRow]="model().summaryRow"
    />
  `,
})
export class Spend {
  readonly rows = input.required<readonly Person[]>();
  readonly fields: readonly PivotField[] = [
    { key: "team", label: "Team" }, { key: "status", label: "Status" },
    { key: "budget", label: "Budget" },
  ];
  readonly state = injectPivotUrlState({
    urlKey: "p",
    defaultConfig: {
      rows: ["team"], columns: ["status"],
      measures: [{ key: "budget", agg: "sum" }],
    },
  });
  readonly model = computed(() => pivotTableModel(
    pivot(this.rows(), this.state.config(), { collapsed: this.state.collapsed() }),
    { fields: this.fields },
  ));
}

Install

pnpm add @adapttable/angular-unstyled @adapttable/angular @adapttable/core

The same feature in the other kits

More Unstyled features

Reference: pivot. More of this kit: AdaptTable for Unstyled, or the live demo.