행 그룹핑
groupBy로 행을 그룹 행 아래로 모아 건수와 aggFunc 합계를 보여 주고, 사용자가 헤더를 그룹 패널로 끌어와 직접 묶게 합니다.
유료 기능 · 행 그룹·집계. 새 도메인에서는 처음 30일 동안 무료로 쓸 수 있고, 그 뒤에는 무료 개인 키나 회사 키가 있어야 합니다. 요금 안내 보기
실행 예제행 그룹핑
원하는 컬럼으로 행을 묶어 건수와 합계를 보고, 헤더를 끌어 직접 묶게 합니다.
이 데모의 코드
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;
}groupBy에 컬럼 ID를 하나 이상 넣으면 같은 값을 가진 행이 그룹 행 아래로 모이고, 그룹 행에 값과 행 수가 표시됩니다. 컬럼에 aggFunc를 'sum', 'avg', 'min', 'max', 'count'나 직접 만든 함수로 지정하면 그룹 행마다 그 집계가 나옵니다. 그룹 순서는 그 컬럼의 정렬 방향을 따릅니다. 화살표를 누르거나 더블클릭하면 펼쳐지고, 키보드로는 Enter, Space, 좌우 방향키를 씁니다. 처음에 몇 단계까지 펼칠지는 groupDefaultExpanded로 정합니다.
groupPanel: true를 켜면 사용자가 열 머리글을 그리드 위 막대로 끌어와 직접 묶을 수 있고, 컬럼 메뉴에도 "이 열로 묶기"가 생깁니다. 코드에서는 setGroupBy()로 같은 일을 하고, groupChange와 groupToggle 이벤트로 바뀐 내용을 받습니다.
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));그룹 행에는 그룹 전체를 체크하는 체크박스가 있고, 일부만 체크되면 중간 상태로 보입니다. 그룹 키는 'country:Japan|city:Tokyo'처럼 값으로 만들어지며, setGroupExpanded, expandAll, collapseAll이 이 키를 씁니다. 어떤 그룹이 펼쳐져 있는지는 getState()에 저장됩니다.
이미 계층이 있는 데이터라면 트리 데이터를 쓰세요. 행이 서버에 있다면 서버 측 그룹핑을 보세요.
값을 비율이나 누계로 보기
valuesAs로 컬럼을 합계 대비·그룹 대비 비율이나 누계로 보여 줍니다. 자세한 내용: