본문으로 건너뛰기Skip to content
예제 첫화면Examples home

10만 줄을 본부·팀으로 묶기Group 100,000 rows by division and team

직원 10만 명을 본부·팀으로 묶고 평균 월급을 보여 줘요. 묶는 기준을 바꾸거나 월급 필터를 걸어도 금방 다시 계산해요.Group 100,000 employees by division and team with average salary, then change the grouping or filter by salary.

심화Advanced
직접 써 보기Live preview10만 줄을 본부·팀으로 묶기Group 100,000 rows by division and team

코드Code

탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.

이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<div id="app">
  <div class="toolbar">
    <select aria-label="묶는 기준" id="by">
      <option value="both" selected>본부 → 팀</option>
      <option value="team">팀만</option>
    </select>
    <select aria-label="월급 필터" id="high">
      <option value="all" selected>전체</option>
      <option value="high">7,000만원 이상</option>
    </select>
    <button type="button" id="expandAll">모두 펼치기</button>
    <button type="button" id="collapseAll">모두 접기</button>
  </div>
  <div id="grid" style="height: 330px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('by').addEventListener('change', (event) => actions.by(api, event.target.value));
document.getElementById('high').addEventListener('change', (event) => actions.high(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));
example.js
const surnames = ['김', '이', '박', '최', '정', '강', '조', '윤', '장', '임'];
const given = ['서연', '준호', '민지', '도윤', '하은', '지후', '수빈', '예준', '지민', '시우'];
const departments = Array.from({ length: 40 }, (_, i) => `${i + 1}본부`);
const teams = ['핵심팀', '플랫폼팀', '성장팀', '지원팀', '데이터팀'];
// 부서 40곳 × 팀 5곳, 모두 10만 줄이에요. 그룹은 줄의 번호만 들고 있어서 열 때도 가벼워요.
const rows = Array.from({ length: 100_000 }, (_, i) => ({
  id: i + 1,
  department: departments[i % departments.length],
  team: teams[Math.floor(i / 40) % teams.length],
  employee: `${surnames[i % 10]}${given[Math.floor(i / 10) % 10]} (${String(i + 1).padStart(6, '0')})`,
  salary: 3000 + ((i * 7919) % 5000),
}));
export const options = {
  columns: [
    { id: 'department', title: '본부', width: 110 },
    { id: 'team', title: '팀', width: 110 },
    { id: 'employee', title: '직원(사번)', width: 170 },
    {
      id: 'salary',
      title: '월급(만원)',
      kind: 'number',
      width: 130,
      align: 'end',
      aggregate: { fn: 'avg', precision: 0 },
    },
  ],
  rows,
  rowKey: 'id',
  grouping: { by: ['department', 'team'], expandDepth: 0 },
  summary: true,
};
let say = () => { };
export function setup(_api, log) {
  say = log;
  log({
    ko: `${rows.length.toLocaleString('ko-KR')}줄을 본부·팀으로 묶었어요. 그룹 줄의 월급은 평균이에요.`,
    en: `${rows.length.toLocaleString('en-US')} rows grouped by division and team. Group rows show the average monthly pay.`,
  });
}
export const actions = {
  by: (api, value) => {
    api.setGroupBy(value === 'team' ? ['team'] : ['department', 'team']);
    say({
      ko: `묶는 기준: ${value === 'team' ? '팀만' : '본부 → 팀'}`,
      en: `Grouped by: ${value === 'team' ? 'team only' : 'division → team'}`,
    });
  },
  expandAll: (api) => {
    const started = performance.now();
    api.expandAll();
    say({
      ko: `모두 펼쳤어요. 보이는 줄 ${api.getVisibleRowCount().toLocaleString('ko-KR')}개 (${Math.round(performance.now() - started)}ms)`,
      en: `Expanded all. ${api.getVisibleRowCount().toLocaleString('en-US')} rows visible (${Math.round(performance.now() - started)}ms)`,
    });
  },
  collapseAll: (api) => api.collapseAll(),
  high: (api, value) => {
    api.setFilterModel(value === 'high'
      ? { match: 'all', items: [{ columnId: 'salary', operator: 'gte', value: 7000 }] }
      : null);
    say({
      ko: value === 'high'
        ? `월급 7,000만원 이상만 남겼어요. (${rows.filter((r) => r.salary >= 7000).length.toLocaleString('ko-KR')}줄이 조건에 맞아요)`
        : '필터를 풀었어요.',
      en: value === 'high'
        ? `Kept only monthly pay of 70 million KRW or more. (${rows.filter((r) => r.salary >= 7000).length.toLocaleString('en-US')} rows match)`
        : 'Filter cleared.',
    });
  },
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<div id="app">
  <div class="toolbar">
    <select aria-label="Group by" id="by">
      <option value="both" selected>Division → team</option>
      <option value="team">Team only</option>
    </select>
    <select aria-label="Salary filter" id="high">
      <option value="all" selected>All</option>
      <option value="high">70,000 or more</option>
    </select>
    <button type="button" id="expandAll">Expand all</button>
    <button type="button" id="collapseAll">Collapse all</button>
  </div>
  <div id="grid" style="height: 330px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('by').addEventListener('change', (event) => actions.by(api, event.target.value));
document.getElementById('high').addEventListener('change', (event) => actions.high(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));
example.js
const surnames = [
  'Carter',
  'Stone',
  'Kim',
  'Park',
  'Brown',
  'Johnson',
  'Lee',
  'Davis',
  'Miller',
  'Clark',
];
const given = ['Liam', 'Emma', 'Noah', 'Olivia', 'Mia', 'Ava', 'Ethan', 'Zoe', 'Jack', 'Lily'];
const departments = Array.from({ length: 40 }, (_, i) => `Division ${i + 1}`);
const teams = ['Core', 'Platform', 'Growth', 'Support', 'Data'];
// 40 departments x 5 teams, 100,000 rows in all. A group only holds row numbers, so opening one stays cheap.
const rows = Array.from({ length: 100_000 }, (_, i) => ({
  id: i + 1,
  department: departments[i % departments.length],
  team: teams[Math.floor(i / 40) % teams.length],
  employee: `${given[Math.floor(i / 10) % 10]} ${surnames[i % 10]} (${String(i + 1).padStart(6, '0')})`,
  salary: 30000 + ((i * 7919) % 5000) * 10,
}));
export const options = {
  columns: [
    { id: 'department', title: 'Division', width: 110 },
    { id: 'team', title: 'Team', width: 110 },
    { id: 'employee', title: 'Employee (ID)', width: 200 },
    {
      id: 'salary',
      title: 'Monthly pay ($)',
      kind: 'number',
      width: 180,
      align: 'end',
      aggregate: { fn: 'avg', precision: 0 },
    },
  ],
  rows,
  rowKey: 'id',
  grouping: { by: ['department', 'team'], expandDepth: 0 },
  summary: true,
};
let say = () => { };
export function setup(_api, log) {
  say = log;
  log(`${rows.length.toLocaleString('en-US')} rows grouped by division and team. Group rows show the average monthly pay.`);
}
export const actions = {
  by: (api, value) => {
    api.setGroupBy(value === 'team' ? ['team'] : ['department', 'team']);
    say(`Grouped by: ${value === 'team' ? 'team only' : 'division → team'}`);
  },
  expandAll: (api) => {
    const started = performance.now();
    api.expandAll();
    say(`Expanded all. ${api.getVisibleRowCount().toLocaleString('en-US')} rows visible (${Math.round(performance.now() - started)}ms)`);
  },
  collapseAll: (api) => api.collapseAll(),
  high: (api, value) => {
    api.setFilterModel(value === 'high'
      ? { match: 'all', items: [{ columnId: 'salary', operator: 'gte', value: 70000 }] }
      : null);
    say(value === 'high'
      ? `Kept only monthly pay of $70,000 or more. (${rows.filter((r) => r.salary >= 70000).length.toLocaleString('en-US')} rows match)`
      : 'Filter cleared.');
  },
};