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

도구 모음과 헤더 그룹·고정 컬럼 함께 쓰기Use the toolbar with header groups and a pinned column

컬럼 도구 모음은 접히는 헤더 그룹, 고정 컬럼과 함께 써요. 앞에 고정한 번호는 칩으로 옮겨도 고정 영역을 벗어나지 않고, 접은 그룹 안의 컬럼은 그룹을 펴기 전까지 접힌 채로 있어요.The column toolbar works with collapsible header groups and a pinned column. The pinned No. column never leaves its zone when you move chips, and a column inside a collapsed group stays folded until you expand the group.

입문Beginner
직접 써 보기Live preview도구 모음과 헤더 그룹·고정 컬럼 함께 쓰기Use the toolbar with header groups and a pinned column

코드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">
    <button type="button" id="collapseAll">그룹 모두 접기</button>
    <button type="button" id="expandAll">그룹 모두 펴기</button>
    <button type="button" id="hideQ2">2분기 숨기기</button>
    <button type="button" id="state">저장본 순서</button>
  </div>
  <div id="grid" style="height: 360px"></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('collapseAll').addEventListener('click', () => actions.collapseAll(api));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('hideQ2').addEventListener('click', () => actions.hideQ2(api));
document.getElementById('state').addEventListener('click', () => actions.state(api));
example.js
const names = ['서울', '부산', '대구', '인천', '광주', '대전', '울산', '세종'];
// 컬럼 도구 모음과 접히는 헤더 그룹, 고정 컬럼을 함께 써요. 번호는 앞에 고정이라 칩으로 옮겨도 고정 영역을 벗어나지 않고,
// 접힌 그룹의 컬럼은 칩을 눌러도 접힘이 먼저예요(그룹을 펴면 칩 상태대로 보여요).
export const options = {
  columns: [
    { id: 'id', title: '번호', width: 70, align: 'end', pinned: 'start' },
    { id: 'name', title: '지점', width: 110 },
    { id: 'q1', title: '1분기', kind: 'number', width: 100, align: 'end' },
    { id: 'q2', title: '2분기', kind: 'number', width: 100, align: 'end' },
    { id: 'q3', title: '3분기', kind: 'number', width: 100, align: 'end' },
    { id: 'q4', title: '4분기', kind: 'number', width: 100, align: 'end' },
  ],
  headerGroups: [
    { id: 'h1', title: '상반기', collapsible: true, children: ['q1', 'q2'] },
    { id: 'h2', title: '하반기', collapsible: true, children: ['q3', 'q4'] },
  ],
  rows: names.map((name, i) => ({
    id: i + 1,
    name,
    q1: 100 + ((i * 37) % 90),
    q2: 120 + ((i * 53) % 80),
    q3: 90 + ((i * 29) % 100),
    q4: 140 + ((i * 41) % 70),
  })),
  rowKey: 'id',
  showRowNumbers: false,
  columnToolbar: { position: 'top', showLabels: true },
};
let say = () => { };
export function setup(api, log) {
  say = log;
  log({
    ko: '칩으로 분기를 숨기거나 옮기고, 그룹 제목의 ▾로 접어 보세요.',
    en: 'Hide or move quarters with the chips, and fold groups with the arrow in the group title.',
  });
  const off = api.on('headerGroupToggled', ({ groupId, collapsed }) => log({
    ko: `${groupId} 그룹을 ${collapsed ? '접었어요' : '펼쳤어요'}`,
    en: `Group ${groupId} ${collapsed ? 'collapsed' : 'expanded'}`,
  }));
  return () => off();
}
export const actions = {
  collapseAll: (api) => {
    api.collapseHeaderGroup('h1');
    api.collapseHeaderGroup('h2');
  },
  expandAll: (api) => {
    api.expandHeaderGroup('h1');
    api.expandHeaderGroup('h2');
  },
  hideQ2: (api) => api.setColumnVisible('q2', false),
  state: (api) => say({
    ko: `저장본 순서: ${api
      .getColumnState()
      .map((s) => `${s.id}${s.hidden ? '(숨김)' : ''}`)
      .join(' · ')}`,
    en: `Saved order: ${api
      .getColumnState()
      .map((s) => `${s.id}${s.hidden ? ' (hidden)' : ''}`)
      .join(' · ')}`,
  }),
};

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">
    <button type="button" id="collapseAll">Collapse all groups</button>
    <button type="button" id="expandAll">Expand all groups</button>
    <button type="button" id="hideQ2">Hide Q2</button>
    <button type="button" id="state">Show saved order</button>
  </div>
  <div id="grid" style="height: 360px"></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('collapseAll').addEventListener('click', () => actions.collapseAll(api));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('hideQ2').addEventListener('click', () => actions.hideQ2(api));
document.getElementById('state').addEventListener('click', () => actions.state(api));
example.js
const names = ['Austin', 'Boston', 'Denver', 'Seattle', 'Portland', 'Dallas', 'Miami', 'Phoenix'];
// The column toolbar works together with collapsible header groups and a pinned column. No. is pinned at the start,
// so a chip can never move it out of the pinned zone, and a column inside a collapsed group stays folded
// until you expand the group (then it follows its chip).
export const options = {
  columns: [
    { id: 'id', title: 'No.', width: 70, align: 'end', pinned: 'start' },
    { id: 'name', title: 'Branch', width: 120 },
    { id: 'q1', title: 'Q1', kind: 'number', width: 100, align: 'end' },
    { id: 'q2', title: 'Q2', kind: 'number', width: 100, align: 'end' },
    { id: 'q3', title: 'Q3', kind: 'number', width: 100, align: 'end' },
    { id: 'q4', title: 'Q4', kind: 'number', width: 100, align: 'end' },
  ],
  headerGroups: [
    { id: 'h1', title: 'First half', collapsible: true, children: ['q1', 'q2'] },
    { id: 'h2', title: 'Second half', collapsible: true, children: ['q3', 'q4'] },
  ],
  rows: names.map((name, i) => ({
    id: i + 1,
    name,
    q1: 100 + ((i * 37) % 90),
    q2: 120 + ((i * 53) % 80),
    q3: 90 + ((i * 29) % 100),
    q4: 140 + ((i * 41) % 70),
  })),
  rowKey: 'id',
  showRowNumbers: false,
  columnToolbar: { position: 'top', showLabels: true },
};
let say = () => { };
export function setup(api, log) {
  say = log;
  log('Hide or move quarters with the chips, and fold groups with the arrow in the group title.');
  const off = api.on('headerGroupToggled', ({ groupId, collapsed }) => log(`Group ${groupId} ${collapsed ? 'collapsed' : 'expanded'}`));
  return () => off();
}
export const actions = {
  collapseAll: (api) => {
    api.collapseHeaderGroup('h1');
    api.collapseHeaderGroup('h2');
  },
  expandAll: (api) => {
    api.expandHeaderGroup('h1');
    api.expandHeaderGroup('h2');
  },
  hideQ2: (api) => api.setColumnVisible('q2', false),
  state: (api) => say(`Saved order: ${api
    .getColumnState()
    .map((s) => `${s.id}${s.hidden ? ' (hidden)' : ''}`)
    .join(' · ')}`),
};