트리에서 가족 관계 물어보기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.
코드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)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<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>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));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)`,
});
},
};<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>트리에서 가족 관계 물어보기</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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 = () => { };
function setup(_api, log) {
say = log;
return () => { };
}
const nameOf = (api, key) => api.getRowByKey(key)?.name ?? String(key);
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(' > ') || '없음';
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)`,
});
},
};
options.ariaLabel = "트리에서 가족 관계 물어보기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
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));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.anc(api)}
>조상</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.desc(api)}
>후손</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.depth(api)}
>깊이·가지</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.rows(api)}
>전체 트리 요약</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.filter(api)}
>김 검색</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.filtered(api)}
>필터된 트리</button>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
parent: number | null;
name: string;
title: string;
}
const ROWS: Row[] = [
{ 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: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
const nameOf = (api: GridApi<Row>, key: number): string =>
api.getRowByKey(key)?.name ?? String(key);
export const options: GridOptions<Row> = {
...base,
tree: { parentKey: 'parent', column: 'name', expandDepth: 1 },
};
const focused = (api: GridApi<Row>): number | null => {
const cell = api.getFocusedCell();
return cell === null ? null : (cell.rowKey as number);
};
const names = (api: GridApi<Row>, keys: readonly number[]): string =>
keys.map((k) => nameOf(api, k)).join(' > ') || '없음';
export const actions = {
anc: (api: GridApi<Row>) => {
const key = focused(api) ?? 4;
say({
ko: `조상: ${names(api, api.getAncestors(key) as number[])}`,
en: `Ancestors: ${names(api, api.getAncestors(key) as number[])}`,
});
},
desc: (api: GridApi<Row>) => {
const key = focused(api) ?? 1;
say({
ko: `후손 ${api.getDescendants(key).length}명: ${names(api, api.getDescendants(key) as number[])}`,
en: `${api.getDescendants(key).length} descendants: ${names(api, api.getDescendants(key) as number[])}`,
});
},
depth: (api: GridApi<Row>) => {
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: GridApi<Row>) => {
const walk = (nodes: ReturnType<GridApi<Row>['getTreeRows']>): number =>
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: GridApi<Row>) => api.setQuickFilter('김'),
filtered: (api: GridApi<Row>) => {
const f = api.getTreeRows({ filtered: true });
const walk = (nodes: typeof f): number =>
nodes.reduce((n, node) => n + 1 + walk(node.children), 0);
say({
ko: `필터된 트리: ${walk(f)}행(조상 포함)`,
en: `Filtered tree: ${walk(f)} rows (ancestors included)`,
});
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.anc(api)">조상</button>
<button type="button" :disabled="!api" @click="api && actions.desc(api)">후손</button>
<button type="button" :disabled="!api" @click="api && actions.depth(api)">깊이·가지</button>
<button type="button" :disabled="!api" @click="api && actions.rows(api)">전체 트리 요약</button>
<button type="button" :disabled="!api" @click="api && actions.filter(api)">김 검색</button>
<button
type="button"
:disabled="!api"
@click="api && actions.filtered(api)"
>필터된 트리</button>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
parent: number | null;
name: string;
title: string;
}
const ROWS: Row[] = [
{ 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: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
const nameOf = (api: GridApi<Row>, key: number): string =>
api.getRowByKey(key)?.name ?? String(key);
export const options: GridOptions<Row> = {
...base,
tree: { parentKey: 'parent', column: 'name', expandDepth: 1 },
};
const focused = (api: GridApi<Row>): number | null => {
const cell = api.getFocusedCell();
return cell === null ? null : (cell.rowKey as number);
};
const names = (api: GridApi<Row>, keys: readonly number[]): string =>
keys.map((k) => nameOf(api, k)).join(' > ') || '없음';
export const actions = {
anc: (api: GridApi<Row>) => {
const key = focused(api) ?? 4;
say({
ko: `조상: ${names(api, api.getAncestors(key) as number[])}`,
en: `Ancestors: ${names(api, api.getAncestors(key) as number[])}`,
});
},
desc: (api: GridApi<Row>) => {
const key = focused(api) ?? 1;
say({
ko: `후손 ${api.getDescendants(key).length}명: ${names(api, api.getDescendants(key) as number[])}`,
en: `${api.getDescendants(key).length} descendants: ${names(api, api.getDescendants(key) as number[])}`,
});
},
depth: (api: GridApi<Row>) => {
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: GridApi<Row>) => {
const walk = (nodes: ReturnType<GridApi<Row>['getTreeRows']>): number =>
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: GridApi<Row>) => api.setQuickFilter('김'),
filtered: (api: GridApi<Row>) => {
const f = api.getTreeRows({ filtered: true });
const walk = (nodes: typeof f): number =>
nodes.reduce((n, node) => n + 1 + walk(node.children), 0);
say({
ko: `필터된 트리: ${walk(f)}행(조상 포함)`,
en: `Filtered tree: ${walk(f)} rows (ancestors included)`,
});
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.anc(api)"
>조상</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.desc(api)"
>후손</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.depth(api)"
>깊이·가지</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.rows(api)"
>전체 트리 요약</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.filter(api)"
>김 검색</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.filtered(api)"
>필터된 트리</button>
</div>
<mx-grid style="height: 300px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
parent: number | null;
name: string;
title: string;
}
const ROWS: Row[] = [
{ 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: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
const nameOf = (api: GridApi<Row>, key: number): string =>
api.getRowByKey(key)?.name ?? String(key);
export const options: GridOptions<Row> = {
...base,
tree: { parentKey: 'parent', column: 'name', expandDepth: 1 },
};
const focused = (api: GridApi<Row>): number | null => {
const cell = api.getFocusedCell();
return cell === null ? null : (cell.rowKey as number);
};
const names = (api: GridApi<Row>, keys: readonly number[]): string =>
keys.map((k) => nameOf(api, k)).join(' > ') || '없음';
export const actions = {
anc: (api: GridApi<Row>) => {
const key = focused(api) ?? 4;
say({
ko: `조상: ${names(api, api.getAncestors(key) as number[])}`,
en: `Ancestors: ${names(api, api.getAncestors(key) as number[])}`,
});
},
desc: (api: GridApi<Row>) => {
const key = focused(api) ?? 1;
say({
ko: `후손 ${api.getDescendants(key).length}명: ${names(api, api.getDescendants(key) as number[])}`,
en: `${api.getDescendants(key).length} descendants: ${names(api, api.getDescendants(key) as number[])}`,
});
},
depth: (api: GridApi<Row>) => {
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: GridApi<Row>) => {
const walk = (nodes: ReturnType<GridApi<Row>['getTreeRows']>): number =>
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: GridApi<Row>) => api.setQuickFilter('김'),
filtered: (api: GridApi<Row>) => {
const f = api.getTreeRows({ filtered: true });
const walk = (nodes: typeof f): number =>
nodes.reduce((n, node) => n + 1 + walk(node.children), 0);
say({
ko: `필터된 트리: ${walk(f)}행(조상 포함)`,
en: `Filtered tree: ${walk(f)} rows (ancestors included)`,
});
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<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>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));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)`);
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Ask the family of a tree row</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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 = () => { };
function setup(_api, log) {
say = log;
return () => { };
}
const nameOf = (api, key) => api.getRowByKey(key)?.name ?? String(key);
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';
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)`);
},
};
options.ariaLabel = "Ask the family of a tree row";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
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));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.anc(api)}
>Ancestors</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.desc(api)}
>Descendants</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.depth(api)}
>Depth and branch</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.rows(api)}
>Whole tree summary</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.filter(api)}
>Search Kim</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.filtered(api)}
>Filtered tree</button>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
parent: number | null;
name: string;
title: string;
}
const ROWS: Row[] = [
{ 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: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
const nameOf = (api: GridApi<Row>, key: number): string =>
api.getRowByKey(key)?.name ?? String(key);
export const options: GridOptions<Row> = {
...base,
tree: { parentKey: 'parent', column: 'name', expandDepth: 1 },
};
const focused = (api: GridApi<Row>): number | null => {
const cell = api.getFocusedCell();
return cell === null ? null : (cell.rowKey as number);
};
const names = (api: GridApi<Row>, keys: readonly number[]): string =>
keys.map((k) => nameOf(api, k)).join(' > ') || 'none';
export const actions = {
anc: (api: GridApi<Row>) => {
const key = focused(api) ?? 4;
say(`Ancestors: ${names(api, api.getAncestors(key) as number[])}`);
},
desc: (api: GridApi<Row>) => {
const key = focused(api) ?? 1;
say(
`${api.getDescendants(key).length} descendants: ${names(api, api.getDescendants(key) as number[])}`,
);
},
depth: (api: GridApi<Row>) => {
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: GridApi<Row>) => {
const walk = (nodes: ReturnType<GridApi<Row>['getTreeRows']>): number =>
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: GridApi<Row>) => api.setQuickFilter('Kim'),
filtered: (api: GridApi<Row>) => {
const f = api.getTreeRows({ filtered: true });
const walk = (nodes: typeof f): number =>
nodes.reduce((n, node) => n + 1 + walk(node.children), 0);
say(`Filtered tree: ${walk(f)} rows (ancestors included)`);
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.anc(api)">Ancestors</button>
<button type="button" :disabled="!api" @click="api && actions.desc(api)">Descendants</button>
<button type="button" :disabled="!api" @click="api && actions.depth(api)">Depth and branch</button>
<button type="button" :disabled="!api" @click="api && actions.rows(api)">Whole tree summary</button>
<button type="button" :disabled="!api" @click="api && actions.filter(api)">Search Kim</button>
<button
type="button"
:disabled="!api"
@click="api && actions.filtered(api)"
>Filtered tree</button>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
parent: number | null;
name: string;
title: string;
}
const ROWS: Row[] = [
{ 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: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
const nameOf = (api: GridApi<Row>, key: number): string =>
api.getRowByKey(key)?.name ?? String(key);
export const options: GridOptions<Row> = {
...base,
tree: { parentKey: 'parent', column: 'name', expandDepth: 1 },
};
const focused = (api: GridApi<Row>): number | null => {
const cell = api.getFocusedCell();
return cell === null ? null : (cell.rowKey as number);
};
const names = (api: GridApi<Row>, keys: readonly number[]): string =>
keys.map((k) => nameOf(api, k)).join(' > ') || 'none';
export const actions = {
anc: (api: GridApi<Row>) => {
const key = focused(api) ?? 4;
say(`Ancestors: ${names(api, api.getAncestors(key) as number[])}`);
},
desc: (api: GridApi<Row>) => {
const key = focused(api) ?? 1;
say(
`${api.getDescendants(key).length} descendants: ${names(api, api.getDescendants(key) as number[])}`,
);
},
depth: (api: GridApi<Row>) => {
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: GridApi<Row>) => {
const walk = (nodes: ReturnType<GridApi<Row>['getTreeRows']>): number =>
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: GridApi<Row>) => api.setQuickFilter('Kim'),
filtered: (api: GridApi<Row>) => {
const f = api.getTreeRows({ filtered: true });
const walk = (nodes: typeof f): number =>
nodes.reduce((n, node) => n + 1 + walk(node.children), 0);
say(`Filtered tree: ${walk(f)} rows (ancestors included)`);
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.anc(api)"
>Ancestors</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.desc(api)"
>Descendants</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.depth(api)"
>Depth and branch</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.rows(api)"
>Whole tree summary</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.filter(api)"
>Search Kim</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.filtered(api)"
>Filtered tree</button>
</div>
<mx-grid style="height: 300px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
parent: number | null;
name: string;
title: string;
}
const ROWS: Row[] = [
{ 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: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
const nameOf = (api: GridApi<Row>, key: number): string =>
api.getRowByKey(key)?.name ?? String(key);
export const options: GridOptions<Row> = {
...base,
tree: { parentKey: 'parent', column: 'name', expandDepth: 1 },
};
const focused = (api: GridApi<Row>): number | null => {
const cell = api.getFocusedCell();
return cell === null ? null : (cell.rowKey as number);
};
const names = (api: GridApi<Row>, keys: readonly number[]): string =>
keys.map((k) => nameOf(api, k)).join(' > ') || 'none';
export const actions = {
anc: (api: GridApi<Row>) => {
const key = focused(api) ?? 4;
say(`Ancestors: ${names(api, api.getAncestors(key) as number[])}`);
},
desc: (api: GridApi<Row>) => {
const key = focused(api) ?? 1;
say(
`${api.getDescendants(key).length} descendants: ${names(api, api.getDescendants(key) as number[])}`,
);
},
depth: (api: GridApi<Row>) => {
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: GridApi<Row>) => {
const walk = (nodes: ReturnType<GridApi<Row>['getTreeRows']>): number =>
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: GridApi<Row>) => api.setQuickFilter('Kim'),
filtered: (api: GridApi<Row>) => {
const f = api.getTreeRows({ filtered: true });
const walk = (nodes: typeof f): number =>
nodes.reduce((n, node) => n + 1 + walk(node.children), 0);
say(`Filtered tree: ${walk(f)} rows (ancestors included)`);
},
};