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

그룹을 접고 펼 때 컬럼 보이기Show columns when a group is open or closed

showWhen으로 헤더 그룹을 펼쳤을 때만(groupOpen), 접었을 때만(groupClosed) 보이는 컬럼을 정해요. 급여 그룹을 접으면 세부 컬럼이 사라지고 합계 하나만 남아요.showWhen makes a column visible only while its header group is open (groupOpen) or closed (groupClosed). Closing the Pay group hides the detail columns and leaves one total.

활용Intermediate
직접 써 보기Live preview그룹을 접고 펼 때 컬럼 보이기Show columns when a group is open or closed

코드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="close">급여 접기</button>
    <button type="button" id="open">급여 펼치기</button>
  </div>
  <div id="grid" style="height: 240px"></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('close').addEventListener('click', () => actions.close(api));
document.getElementById('open').addEventListener('click', () => actions.open(api));
example.js
const ROWS = [
  { id: 1001, name: '김민준', team: '개발팀', city: '서울', salary: 6200, bonus: 620 },
  { id: 1002, name: '이서연', team: '디자인팀', city: '부산', salary: 4800, bonus: 480 },
  { id: 1003, name: '박지호', team: '영업팀', city: '대구', salary: 7100, bonus: 710 },
  { id: 1004, name: '최유나', team: '인사팀', city: '서울', salary: 3900, bonus: 390 },
];
let say = () => { };
export function setup(_api, log) {
  say = log;
  return () => { };
}
const COLUMNS = [
  { id: 'name', title: '이름', width: 120 },
  { id: 'team', title: '팀', width: 110 },
  {
    id: 'salary',
    title: '기본급',
    kind: 'number',
    width: 110,
    align: 'end',
    showWhen: 'groupOpen',
  },
  { id: 'bonus', title: '보너스', kind: 'number', width: 110, align: 'end', showWhen: 'groupOpen' },
  {
    id: 'total',
    title: '합계',
    kind: 'number',
    width: 110,
    align: 'end',
    showWhen: 'groupClosed',
    getValue: (row) => row.salary + row.bonus,
  },
];
export const options = {
  columns: COLUMNS,
  rows: ROWS,
  rowKey: 'id',
  headerGroups: [
    { id: 'pay', title: '급여', collapsible: true, children: ['salary', 'bonus', 'total'] },
  ],
};
export const actions = {
  close: (api) => {
    api.setHeaderGroupCollapsed('pay', true);
    say({ ko: '급여를 접었어요: 합계만 보여요', en: 'Closed Pay: only Total shows' });
  },
  open: (api) => {
    api.setHeaderGroupCollapsed('pay', false);
    say({ ko: '급여를 펼쳤어요: 기본급·보너스가 보여요', en: 'Opened Pay: Base and Bonus show' });
  },
};

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="close">Close Pay</button>
    <button type="button" id="open">Open Pay</button>
  </div>
  <div id="grid" style="height: 240px"></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('close').addEventListener('click', () => actions.close(api));
document.getElementById('open').addEventListener('click', () => actions.open(api));
example.js
const ROWS = [
  { id: 1001, name: 'Liam Carter', team: 'Dev', city: 'Seattle', salary: 62000, bonus: 6200 },
  { id: 1002, name: 'Emma Stone', team: 'Design', city: 'Boston', salary: 48000, bonus: 4800 },
  { id: 1003, name: 'Noah Hayes', team: 'Sales', city: 'Denver', salary: 71000, bonus: 7100 },
  { id: 1004, name: 'Olivia Reed', team: 'HR', city: 'Seattle', salary: 39000, bonus: 3900 },
];
let say = () => { };
export function setup(_api, log) {
  say = log;
  return () => { };
}
const COLUMNS = [
  { id: 'name', title: 'Name', width: 120 },
  { id: 'team', title: 'Team', width: 110 },
  {
    id: 'salary',
    title: 'Base',
    kind: 'number',
    width: 110,
    align: 'end',
    showWhen: 'groupOpen',
  },
  { id: 'bonus', title: 'Bonus', kind: 'number', width: 110, align: 'end', showWhen: 'groupOpen' },
  {
    id: 'total',
    title: 'Total',
    kind: 'number',
    width: 110,
    align: 'end',
    showWhen: 'groupClosed',
    getValue: (row) => row.salary + row.bonus,
  },
];
export const options = {
  columns: COLUMNS,
  rows: ROWS,
  rowKey: 'id',
  headerGroups: [
    { id: 'pay', title: 'Pay', collapsible: true, children: ['salary', 'bonus', 'total'] },
  ],
};
export const actions = {
  close: (api) => {
    api.setHeaderGroupCollapsed('pay', true);
    say('Closed Pay: only Total shows');
  },
  open: (api) => {
    api.setHeaderGroupCollapsed('pay', false);
    say('Opened Pay: Base and Bonus show');
  },
};