AdaptTable for Element Plus

Pivot tables in Element Plus

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

<script setup lang="ts">
import { computed } from "vue";
import {
  pivot, pivotTableModel, usePivotUrlState, type PivotField,
} from "@adapttable/vue/pivot";
import { DataTable } from "@adapttable/element-plus";
import { pinnedSummaryRows } from "@adapttable/element-plus/pinned-summary-rows";
import { PivotPanel } from "@adapttable/element-plus/pivot";

const props = defineProps<{ rows: readonly Person[] }>();

const fields: readonly PivotField[] = [
  { key: "team", label: "Team" }, { key: "status", label: "Status" },
  { key: "budget", label: "Budget" },
];
const state = usePivotUrlState({
  urlKey: "p",
  defaultConfig: {
    rows: ["team"], columns: ["status"],
    measures: [{ key: "budget", agg: "sum" }],
  },
});
const model = computed(() => pivotTableModel(
  pivot(props.rows, state.config.value, { collapsed: state.collapsed.value }),
  { fields },
));
</script>

<template>
  <PivotPanel
    :fields="fields" :config="state.config.value"
    :on-change="state.onConfigChange"
  />
  <DataTable
    :data="model.rows" :columns="model.columns" :row-key="model.rowKey"
    :features="[pinnedSummaryRows(model.pinnedRows ?? {})]"
  />
</template>

Install

pnpm add @adapttable/element-plus @adapttable/vue @adapttable/core element-plus

The same feature in the other kits

More Element Plus features

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