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

트리에서 가족 관계 물어보기Ask the family of a tree row

getAncestors, getDescendants, getDepth, getParentRow, isBranch, getMaxDepth는 접힌 행까지 포함해 전체 트리를 읽어요. getTreeRows는 중첩 구조로, filtered를 켜면 필터를 통과한 행과 조상만 돌려줘요.getAncestors, getDescendants, getDepth, getParentRow, isBranch and getMaxDepth read the whole tree, collapsed rows included. getTreeRows returns a nested structure, and with filtered only the rows that pass the filter and their ancestors.

활용Intermediate
직접 써 보기Live preview트리에서 가족 관계 물어보기Ask the family of a tree row

코드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="anc">조상</button>
    <button type="button" id="desc">후손</button>
    <button type="button" id="depth">깊이·가지</button>
    <button type="button" id="rows">전체 트리 요약</button>
    <button type="button" id="filter">김 검색</button>
    <button type="button" id="filtered">필터된 트리</button>
  </div>
  <div id="grid" style="height: 300px"></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('anc').addEventListener('click', () => actions.anc(api));
document.getElementById('desc').addEventListener('click', () => actions.desc(api));
document.getElementById('depth').addEventListener('click', () => actions.depth(api));
document.getElementById('rows').addEventListener('click', () => actions.rows(api));
document.getElementById('filter').addEventListener('click', () => actions.filter(api));
document.getElementById('filtered').addEventListener('click', () => actions.filtered(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 () => { };
}
const nameOf = (api, key) => api.getRowByKey(key)?.name ?? String(key);
export const options = {
  ...base,
  tree: { parentKey: 'parent', column: 'name', expandDepth: 1 },
};
const focused = (api) => {
  const cell = api.getFocusedCell();
  return cell === null ? null : cell.rowKey;
};
const names = (api, keys) => keys.map((k) => nameOf(api, k)).join(' > ') || '없음';
export const actions = {
  anc: (api) => {
    const key = focused(api) ?? 4;
    say({
      ko: `조상: ${names(api, api.getAncestors(key))}`,
      en: `Ancestors: ${names(api, api.getAncestors(key))}`,
    });
  },
  desc: (api) => {
    const key = focused(api) ?? 1;
    say({
      ko: `후손 ${api.getDescendants(key).length}명: ${names(api, api.getDescendants(key))}`,
      en: `${api.getDescendants(key).length} descendants: ${names(api, api.getDescendants(key))}`,
    });
  },
  depth: (api) => {
    const key = focused(api) ?? 4;
    say({
      ko: `${nameOf(api, key)}: 깊이 ${api.getDepth(key)}, 가지 ${api.isBranch(key)}, 부모 ${api.getParentRow(key)?.name ?? '없음'}, 가장 깊은 곳 ${api.getMaxDepth()}`,
      en: `${nameOf(api, key)}: depth ${api.getDepth(key)}, branch ${api.isBranch(key)}, parent ${api.getParentRow(key)?.name ?? 'none'}, deepest ${api.getMaxDepth()}`,
    });
  },
  rows: (api) => {
    const walk = (nodes) => nodes.reduce((n, node) => n + 1 + walk(node.children), 0);
    say({
      ko: `getTreeRows: 최상위 ${api.getTreeRows().length}개, 전체 ${walk(api.getTreeRows())}행`,
      en: `getTreeRows: ${api.getTreeRows().length} top rows, ${walk(api.getTreeRows())} rows in all`,
    });
  },
  filter: (api) => api.setQuickFilter('김'),
  filtered: (api) => {
    const f = api.getTreeRows({ filtered: true });
    const walk = (nodes) => nodes.reduce((n, node) => n + 1 + walk(node.children), 0);
    say({
      ko: `필터된 트리: ${walk(f)}행(조상 포함)`,
      en: `Filtered tree: ${walk(f)} rows (ancestors included)`,
    });
  },
};

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="anc">Ancestors</button>
    <button type="button" id="desc">Descendants</button>
    <button type="button" id="depth">Depth and branch</button>
    <button type="button" id="rows">Whole tree summary</button>
    <button type="button" id="filter">Search Kim</button>
    <button type="button" id="filtered">Filtered tree</button>
  </div>
  <div id="grid" style="height: 300px"></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('anc').addEventListener('click', () => actions.anc(api));
document.getElementById('desc').addEventListener('click', () => actions.desc(api));
document.getElementById('depth').addEventListener('click', () => actions.depth(api));
document.getElementById('rows').addEventListener('click', () => actions.rows(api));
document.getElementById('filter').addEventListener('click', () => actions.filter(api));
document.getElementById('filtered').addEventListener('click', () => actions.filtered(api));
example.js
const ROWS = [
  { id: 1, parent: null, name: 'CEO', title: 'Chief Executive' },
  { id: 2, parent: 1, name: 'Dev Division', title: 'Director' },
  { id: 3, parent: 2, name: 'Platform Team', title: 'Team Lead' },
  { id: 4, parent: 3, name: 'Daniel Kim', 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: 'Jack Reed', 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 () => { };
}
const nameOf = (api, key) => api.getRowByKey(key)?.name ?? String(key);
export const options = {
  ...base,
  tree: { parentKey: 'parent', column: 'name', expandDepth: 1 },
};
const focused = (api) => {
  const cell = api.getFocusedCell();
  return cell === null ? null : cell.rowKey;
};
const names = (api, keys) => keys.map((k) => nameOf(api, k)).join(' > ') || 'none';
export const actions = {
  anc: (api) => {
    const key = focused(api) ?? 4;
    say(`Ancestors: ${names(api, api.getAncestors(key))}`);
  },
  desc: (api) => {
    const key = focused(api) ?? 1;
    say(`${api.getDescendants(key).length} descendants: ${names(api, api.getDescendants(key))}`);
  },
  depth: (api) => {
    const key = focused(api) ?? 4;
    say(`${nameOf(api, key)}: depth ${api.getDepth(key)}, branch ${api.isBranch(key)}, parent ${api.getParentRow(key)?.name ?? 'none'}, deepest ${api.getMaxDepth()}`);
  },
  rows: (api) => {
    const walk = (nodes) => nodes.reduce((n, node) => n + 1 + walk(node.children), 0);
    say(`getTreeRows: ${api.getTreeRows().length} top rows, ${walk(api.getTreeRows())} rows in all`);
  },
  filter: (api) => api.setQuickFilter('Kim'),
  filtered: (api) => {
    const f = api.getTreeRows({ filtered: true });
    const walk = (nodes) => nodes.reduce((n, node) => n + 1 + walk(node.children), 0);
    say(`Filtered tree: ${walk(f)} rows (ancestors included)`);
  },
};