FreeGridv1.15.1한국어

Row grouping

groupBy gathers rows under group rows with counts and aggFunc totals; people can group by dragging headers into the group panel.

Live demoRow grouping

Group rows by any columns with counts and totals, and let people drag headers to group.

Code for this demo
import { createGrid, type ColumnDef } from '@freegridjs/core';
import { makeRows } from '../data';
import { button, separator, text, tr, type DemoContext } from '../ui';

export default function mount(ctx: DemoContext) {
  const { lang } = ctx;
  const h = (en: string, ko: string) => tr(lang, { en, ko });
  const money = { type: 'number', format: '#,##0', width: 120, aggFunc: 'sum' } as const;
  const columns: ColumnDef[] = [
    { field: 'country', header: h('Country', '국가'), width: 150 },
    { field: 'company', header: h('Company', '회사'), width: 140 },
    { field: 'name', header: h('Name', '이름'), width: 170 },
    { field: 'q1', header: h('Q1 revenue', '1분기 매출'), ...money },
    { field: 'q2', header: h('Q2 revenue', '2분기 매출'), ...money },
    { field: 'total', header: h('Total', '합계'), ...money, width: 130 },
    { field: 'id', header: h('People', '인원'), type: 'number', width: 90, aggFunc: 'count' },
  ];
  const grid = createGrid(ctx.host, {
    columns, data: makeRows(1000), locale: lang, theme: 'board',
    rowSelection: 'multiple', // group rows get a checkbox for all of their rows
    groupBy: ['country'],
    groupPanel: true,
  });
  grid.on('groupToggle', e => ctx.log(`groupToggle → ${e.key}: ${e.expanded}`));
  grid.on('groupChange', e => ctx.log(`groupChange → [${e.groupBy.join(', ')}]`));

  text(ctx, h('Drag a column header into the bar above the grid to add a grouping level.',
    '열 머리글을 그리드 위 막대로 끌어오면 묶음 단계가 늘어납니다.'));
  separator(ctx);
  button(ctx, h('Expand all', '모두 펼치기'), () => grid.expandAll());
  button(ctx, h('Collapse all', '모두 접기'), () => grid.collapseAll());
  button(ctx, h('Group by country, then company', '국가 → 회사로 묶기'), () => grid.setGroupBy(['country', 'company']));
  button(ctx, h('No grouping', '그룹 해제'), () => grid.setGroupBy([]));
  return grid;
}

Pass groupBy with one or more column ids and rows are gathered under group rows that show the value and the number of rows. Give a column an aggFunc such as 'sum', 'avg', 'min', 'max', 'count' or your own function, and every group row shows that total. Groups follow the sort direction of their column. Open them with the arrow, a double-click, or Enter, Space and the left and right arrow keys; groupDefaultExpanded decides how many levels start open.

With groupPanel: true, people drag a header into the bar above the grid to group by it themselves, and the column menu gets "Group by this column". setGroupBy() does the same from code, and groupChange and groupToggle report what changed.

createGrid(el, {
  columns: [
    { field: 'country' },
    { field: 'city' },
    { field: 'name' },
    { field: 'sales', type: 'number', aggFunc: 'sum' },   // shown in each group row
    { field: 'id', aggFunc: 'count' },
  ],
  data: rows,
  rowSelection: 'multiple',  // group rows get a checkbox for all of their rows
  groupBy: ['country'],      // or grid.setGroupBy(['country', 'city'])
  groupPanel: true,          // drag headers above the grid to group
  groupDefaultExpanded: 1,   // 0 = collapsed, -1 = everything open
});

grid.expandAll();
grid.setGroupExpanded('country:Japan|city:Tokyo', true);
grid.on('groupChange', e => console.log(e.groupBy));

Group rows have a checkbox for all of their rows and show a partial state when only some are checked. A group's key is built from its values, such as 'country:Japan|city:Tokyo'; setGroupExpanded, expandAll and collapseAll use it, and getState() saves which groups are open.

For data that is already a hierarchy, use tree data instead. When the rows live on a server, see Server-side grouping.

Show values as a share or a running total

valuesAs shows a column as a share of the total or of its group, or as a running total. See: