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

부모를 고르면 자식도 함께 고르기Select a parent and its children together

tree.checkCascade를 켜면 행을 선택할 때 후손도, 풀 때도 후손이 함께 움직여요. 체크박스에서 후손이 일부만 선택되면 상위 행이 어중간한 표시로 바뀌어요. tree.expandRecursive는 펼칠 때 후손도 펼쳐요.With tree.checkCascade, selecting a row selects its descendants and deselecting does the same. With checkboxes a parent shows a mixed state when only some descendants are selected. tree.expandRecursive expands all descendants at once.

활용Intermediate
직접 써 보기Live preview부모를 고르면 자식도 함께 고르기Select a parent and its children together

코드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="all">모두 접기</button>
    <button type="button" id="log">선택 키 보기</button>
  </div>
  <div id="grid" style="height: 320px"></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('all').addEventListener('click', () => actions.all(api));
document.getElementById('log').addEventListener('click', () => actions.log(api));
example.js
const ROWS = [
  { id: 1, parent: null, name: '대표', title: '대표이사' },
  { id: 2, parent: 1, name: '개발본부', title: '본부장' },
  { id: 3, parent: 2, name: '플랫폼팀', title: '팀장' },
  { id: 4, parent: 3, name: '김민준', title: '선임' },
  { id: 5, parent: 3, name: '이서연', title: '사원' },
  { id: 6, parent: 2, name: '앱팀', title: '팀장' },
  { id: 7, parent: 6, name: '박지호', title: '선임' },
  { id: 8, parent: 1, name: '경영지원', title: '본부장' },
  { id: 9, parent: 8, name: '인사팀', title: '팀장' },
  { id: 10, parent: 9, name: '최유나', title: '사원' },
];
const base = {
  columns: [
    { id: 'name', title: '조직·이름', width: 180 },
    { id: 'title', title: '직함', width: 120 },
  ],
  rows: ROWS,
  rowKey: 'id',
};
let say = () => { };
export function setup(_api, log) {
  say = log;
  return () => { };
}
export const options = {
  ...base,
  selection: { mode: 'multiple', checkbox: true },
  tree: {
    parentKey: 'parent',
    column: 'name',
    expandDepth: 0,
    checkCascade: true,
    expandRecursive: true,
    // 본부장급 아래만 처음부터 펼쳐 둬요(그 밖의 행은 expandDepth를 따라요).
    expandWhen: (row) => (row.title === '대표이사' ? true : undefined),
  },
};
export const actions = {
  all: (api) => api.collapseAll(),
  log: (api) => say({
    ko: `선택된 키: ${api.getSelectedKeys().join(', ') || '없음'}`,
    en: `Selected keys: ${api.getSelectedKeys().join(', ') || 'none'}`,
  }),
};

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="all">Collapse all</button>
    <button type="button" id="log">Show selected keys</button>
  </div>
  <div id="grid" style="height: 320px"></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('all').addEventListener('click', () => actions.all(api));
document.getElementById('log').addEventListener('click', () => actions.log(api));
example.js
const ROWS = [
  { id: 1, parent: null, name: 'CEO', title: 'Chief Executive' },
  { id: 2, parent: 1, name: 'Dev', title: 'Director' },
  { id: 3, parent: 2, name: 'Platform Team', title: 'Team Lead' },
  { id: 4, parent: 3, name: 'Liam Carter', title: 'Senior' },
  { id: 5, parent: 3, name: 'Emma Stone', title: 'Staff' },
  { id: 6, parent: 2, name: 'App Team', title: 'Team Lead' },
  { id: 7, parent: 6, name: 'Noah Davis', title: 'Senior' },
  { id: 8, parent: 1, name: 'Management Support', title: 'Director' },
  { id: 9, parent: 8, name: 'HR Team', title: 'Team Lead' },
  { id: 10, parent: 9, name: 'Mia Parker', title: 'Staff' },
];
const base = {
  columns: [
    { id: 'name', title: 'Unit / Name', width: 180 },
    { id: 'title', title: 'Title', width: 140 },
  ],
  rows: ROWS,
  rowKey: 'id',
};
let say = () => { };
export function setup(_api, log) {
  say = log;
  return () => { };
}
export const options = {
  ...base,
  selection: { mode: 'multiple', checkbox: true },
  tree: {
    parentKey: 'parent',
    column: 'name',
    expandDepth: 0,
    checkCascade: true,
    expandRecursive: true,
    // Only the top row starts open (other rows follow expandDepth).
    expandWhen: (row) => (row.title === 'Chief Executive' ? true : undefined),
  },
};
export const actions = {
  all: (api) => api.collapseAll(),
  log: (api) => say(`Selected keys: ${api.getSelectedKeys().join(', ') || 'none'}`),
};