Skip to content
EspañolEnglish
All components (283)

Pivot table

Data · PivotTable

What it is and when to use it

Crosses the data by two fields and summarises a third in each crossing: what was invoiced by salesperson and by state, hours by person and by week. One row for each value of the first field, one column for each value of the second, and the totals at the end.

When to use it

  • Having in front of you a comparison that comes out of a long list of records (sales, orders, incidents), without preparing the totals by hand.
  • rowField and columnField say which are the rows and the columns, measure the figure that is summarised and aggregate how (sum by default, avg, min, max or count).
  • The totals are calculated on the source records, not on the cells: the average of a row is the average of its data, not the average of its averages.
  • format writes the figures (in euros, in hours). A crossing with no records shows as "—".
  • The "Swap rows and columns" button turns the table around without asking for the data again; swappable={false} removes it.

When not to

  • A listing of records with its order, filters and selection: Table.
  • A breakdown that is better understood drawn: BarChart or HeatmapChart.

Keyboard: it is a Table: a single tab stop, the arrows move through the cells and the headers sort with Enter or Space.

Accessibility: it is a real <table> with a required name (label); the first column names each row. When swapping, an announcement says what each axis is now. An empty crossing carries "—", not a zero that looks like data.

Fine-tuning: that of Table (--amset-table-*). It accepts its same appearance and behaviour props (size, maxHeight, loading, error, pagination, exportCsv...).

What it looks like

Total of Amount
Sales / StatusCollectedOverduePendingTotal
Aitana Vidal56.600 €12.400 €22.150 €91.150 €
José Ortega31.400 €10.900 €—42.300 €
Marta Ruiz42.800 €—13.250 €56.050 €

<PivotTable
  label="Invoicing by salesperson and state"
  rows={ventas}
  rowField={{ key: "comercial", title: "Sales" }}
  columnField={{ key: "estado", title: "Status" }}
  measure={{ key: "importe", title: "Amount" }}
  aggregate="sum"
  format={enEuros}
/>

Props

Import it with import { PivotTable } from '@amset/ui'. Required props are marked.

PropTypeWhat it does
columnFieldrequiredPivotField<T>The axis from left to right: one column for each distinct value of this field.
labelrequiredstringWhat is being shown: "Sales by salesperson and state". Required.
measurerequiredPivotField<T>The figure that is summarised in each crossing: the amount, the hours.
rowFieldrequiredPivotField<T>The axis from top to bottom: one row for each distinct value of this field.
rowsrequiredreadonly T[]The source data: one row per invoice, order or sale.
advancedFilterAdvancedFilterThe advanced filter, controlled from outside. Without it, the table keeps it.
advancedFiltersbooleanAdds the panel of filters with several conditions joined by "and" / "or".
aggregateTableAggregateHow it is summarised: `sum` (default), `avg`, `min`, `max` or `count`.
cellSelectionbooleanCell selection as in a spreadsheet: one range by dragging, with Shift+click or Shift+arrows, Ctrl/Cmd+A for everything and Escape to clear it; several loose ranges with Ctrl/Cmd+click (on a cell already selected, it removes it) or with Shift+F8 from the keyboard. Ctrl/Cmd+C copies as TSV (and as an HTML table): several ranges only if they share rows or columns, as in Excel. With `onCellEdit`, Ctrl/Cmd+V pastes from the starting cell downwards and to the right, only in the columns with `editor`; a single value is spread over everything selected, also when there are several ranges.
classNamestringMerged with the library classes; it does not replace them.
configurableColumnsbooleanAdds the panel to show, hide and reorder columns.
emptyMessagestring—
errorReactNodeLoading the data failed: this reason is shown instead of the rows (or `true` for the default text), announced as an alert. With `onRetry` it carries a button to request them again.
expandedKeysreadonly string[]Expanded rows (by their key), controlled from outside. Without them, the table keeps track of them.
exportCsvboolean | TableExportOptions<PivotRowData>Adds a button that downloads as CSV what shows in the visible columns: every row, already filtered and sorted, not just the page. With `serverSide` it only exports the loaded rows.
exportXlsxboolean | TableExportOptions<PivotRowData>Adds a button that downloads to Excel (.xlsx) the same as the CSV, with each value with its type: numbers as numbers and dates as dates. The writer is loaded on pressing, not with the table. `separator` is not used here.
filtersReadonly<Record<string, string | readonly string[]>>Filters controlled from outside. Without them, the table keeps them.
format(value: number) => stringHow a figure is written: in euros, in hours. By default, with the numbers of the language.
groupTotalsbooleanWith `groupBy`, a totals row at the end of each group with each column's `footer` computed over the rows of THAT group (all of them, even if the page only shows part).
initialCollapsedGroupsreadonly string[]Groups collapsed at the start (by the value of their key).
initialExpandedKeysreadonly string[]The rows expanded at the start, without controlling them from outside.
initiallySwappedbooleanStarts with rows and columns swapped.
initialSortTableSort | null—
loadingboolean—
maxHeightstringMaximum height of the table (`'24rem'`, `'60vh'`): beyond it, the body scrolls and the header and footer stay fixed. It is the `--amset-table-height` variable made a prop.
multiSortbooleanWith `true`, Shift+click on another header adds it as a tie-breaking sort criterion.
onAdvancedFilterChange(filter: AdvancedFilter) => voidNotifies of the new advanced filter. It only notifies: it controls nothing.
onCellRangeChange(range: CellRange | null) => voidNotifies of the active cell range (the last one started), or of `null` when they are cleared.
onCellRangesChange(ranges: readonly CellRange[]) => voidNotifies of all the loose ranges at once, from first to last, every time they change; empty when they are cleared.
onExpandedChange(keys: readonly string[]) => voidReports with the expanded keys. It only reports: it does not control or turn anything off.
onFilter(filters: TableFilters) => voidNotifies of the new filters. It only notifies: it does not control or turn anything off.
onRetry() => voidRequests the data again after an `error`.
onRowPress(row: PivotRowData) => voidNotifies of the row pressed. Expanding the detail does not count.
onSearchChange(text: string) => voidReports every change of the search text. With `serverSide` or server pagination the table cannot filter what it does not have, so whoever provides the data runs the search: without this, the box does not show in those modes.
onSelectionChange(keys: readonly string[]) => voidNotifies with the selected keys. It only notifies: it controls nothing.
onSort(sort: TableSort | null) => voidAnnounces the new order, or `null` when it is removed. It only announces: it does not control or turn anything off.
onSortsChange(sorts: readonly TableSort[]) => voidNotifies with the full list of sort criteria (the first one is the one `onSort` reports).
onSwap(swapped: boolean) => voidReports that the rows and columns have been swapped.
onVisibleColumnsChange(keys: readonly string[]) => voidReports the visible columns, in their order. It only reports: it controls nothing.
paginationfalse | TablePagination—
persistKeystringRemembers between visits which columns are shown, in what order and at what width, in the browser, under this key. With a controlled `visibleColumns` it does not remember the visible ones.
pinnedRowsTablePinnedRowsRows that stay on top or below, on every page and when scrolling: a total, the most important. They are given by key. They stay out of everything else: they are not sorted, filtered, counted in pages or the footer, and cannot be selected.
renderGroupLabel(value: CellValue, rows: readonly PivotRowData[]) => ReactNodeWhat the header of a group says. By default, the value and the count.
reorderableColumnsbooleanColumns are reordered by dragging their header, or with Control and the arrows with focus on it. A `sticky` column stays in place. The order arrives through `onVisibleColumnsChange` and is stored with `persistKey`.
resizableColumnsbooleanThe edge of each header can be dragged to change the width of the column.
responsive"scroll" | "cards"What the table does when it does not fit. `scroll` (the default) scrolls it sideways; `cards`, on a screen of 40 rem or less, draws each row as a card with stacked “Column: value”. The headers move to a strip of buttons above, so sorting, filtering and choosing columns stay within reach.
rowClassName(row: PivotRowData, index: number) => string | undefinedA class to mark a specific row (an overdue invoice in red) without touching the global CSS. It is ADDED to those of the table. `index` is the position in the page being shown.
rowExpandable(row: PivotRowData) => booleanWhich rows can be expanded. All by default.
searchablebooleanPuts a search box above the table that filters by the text of any visible column.
selectedKeysreadonly string[]Selected keys, controlled from outside. Without them, the table keeps them.
selectionActions(keys: readonly string[]) => ReactNodeBar that appears when rows are selected.
selectionControl"row" | "radio"With `selection="single"`, a radio button at the start of each row (`radio`) instead of choosing by pressing the row. With `multiple` there are always checkboxes. Default: `row`, the row is chosen.
serverSidebooleanSorting and filtering are done by the server: the table paints `rows` as they arrive and only notifies through `onSort` and `onFilter`. With `pagination.total` it is so without asking, because sorting a single page locally makes no sense.
size"sm" | "md" | "lg"The size of the table: it moves the cell padding and the body font size. Not to be confused with `pagination.size`, which is the rows per page.
sortTableSort | nullSort order controlled from outside. Without it, the table manages it, starting at `initialSort`.
swappablebooleanThe button to swap rows and columns. By default, with it.
totalsbooleanThe totals column and row. By default, with them.
virtualizedboolean | TableVirtualizationPaints only the rows that are visible, for thousands of rows. It needs `maxHeight` (without a height there is no scrolling) and does not combine with `renderExpandedRow`, whose rows have different heights.
visibleColumnsreadonly string[]Visible columns and their order, controlled from outside. Without them, the table keeps track of them.

It also accepts the attributes of the element it renders: className, style, data-*, aria-* and its events.

In the test bench

Every state and variant, to try one at a time, on its page at storybook.amsetui.com: