트리에 자식 행 끼워 넣기Insert a child row into a tree
addRows(rows, { parent, index })는 새 행의 부모 필드를 맞춰 주고 그 부모의 자식 중 index 자리에 넣어요. resetChildren은 지연 로딩한 자식을 비우고 다시 불러올 수 있게 해요.addRows(rows, { parent, index }) sets the parent field of the new row and inserts it at index among that parent's children. resetChildren clears lazily loaded children so they can load again.
코드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="first">개발본부 맨 앞에</button>
<button type="button" id="last">개발본부 끝에</button>
<button type="button" id="top">최상위로</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('first').addEventListener('click', () => actions.first(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
document.getElementById('top').addEventListener('click', () => actions.top(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: 'all' },
};
let next = 100;
const add = (api, parent, index) => {
const id = (next += 1);
api.addRows([{ id, name: `새 직원 ${id}`, title: '사원' }], {
parent,
...(index === undefined ? {} : { index }),
});
say({
ko: `${id}번을 ${parent === null ? '최상위' : `${nameOf(api, parent)} 아래`}에 넣었어요 (부모 필드 ${api.getRowByKey(id)?.parent ?? 'null'})`,
en: `Added ${id} ${parent === null ? 'at the top level' : `under ${nameOf(api, parent)}`} (parent field ${api.getRowByKey(id)?.parent ?? 'null'})`,
});
};
export const actions = {
first: (api) => add(api, 2, 0),
last: (api) => add(api, 2),
top: (api) => add(api, null),
};<!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="first">개발본부 맨 앞에</button>
<button type="button" id="last">개발본부 끝에</button>
<button type="button" id="top">최상위로</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: 'all' },
};
let next = 100;
const add = (api, parent, index) => {
const id = (next += 1);
api.addRows([{ id, name: `새 직원 ${id}`, title: '사원' }], {
parent,
...(index === undefined ? {} : { index }),
});
say({
ko: `${id}번을 ${parent === null ? '최상위' : `${nameOf(api, parent)} 아래`}에 넣었어요 (부모 필드 ${api.getRowByKey(id)?.parent ?? 'null'})`,
en: `Added ${id} ${parent === null ? 'at the top level' : `under ${nameOf(api, parent)}`} (parent field ${api.getRowByKey(id)?.parent ?? 'null'})`,
});
};
const actions = {
first: (api) => add(api, 2, 0),
last: (api) => add(api, 2),
top: (api) => add(api, null),
};
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('first').addEventListener('click', () => actions.first(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
document.getElementById('top').addEventListener('click', () => actions.top(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.first(api)}
>개발본부 맨 앞에</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.last(api)}
>개발본부 끝에</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.top(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: 'all' },
};
let next = 100;
const add = (api: GridApi<Row>, parent: number | null, index?: number): void => {
const id = (next += 1);
api.addRows([{ id, name: `새 직원 ${id}`, title: '사원' } as Row], {
parent,
...(index === undefined ? {} : { index }),
});
say({
ko: `${id}번을 ${parent === null ? '최상위' : `${nameOf(api, parent)} 아래`}에 넣었어요 (부모 필드 ${api.getRowByKey(id)?.parent ?? 'null'})`,
en: `Added ${id} ${parent === null ? 'at the top level' : `under ${nameOf(api, parent)}`} (parent field ${api.getRowByKey(id)?.parent ?? 'null'})`,
});
};
export const actions = {
first: (api: GridApi<Row>) => add(api, 2, 0),
last: (api: GridApi<Row>) => add(api, 2),
top: (api: GridApi<Row>) => add(api, null),
};버튼을 누르면 이 예제의 코드가 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.first(api)">개발본부 맨 앞에</button>
<button type="button" :disabled="!api" @click="api && actions.last(api)">개발본부 끝에</button>
<button type="button" :disabled="!api" @click="api && actions.top(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: 'all' },
};
let next = 100;
const add = (api: GridApi<Row>, parent: number | null, index?: number): void => {
const id = (next += 1);
api.addRows([{ id, name: `새 직원 ${id}`, title: '사원' } as Row], {
parent,
...(index === undefined ? {} : { index }),
});
say({
ko: `${id}번을 ${parent === null ? '최상위' : `${nameOf(api, parent)} 아래`}에 넣었어요 (부모 필드 ${api.getRowByKey(id)?.parent ?? 'null'})`,
en: `Added ${id} ${parent === null ? 'at the top level' : `under ${nameOf(api, parent)}`} (parent field ${api.getRowByKey(id)?.parent ?? 'null'})`,
});
};
export const actions = {
first: (api: GridApi<Row>) => add(api, 2, 0),
last: (api: GridApi<Row>) => add(api, 2),
top: (api: GridApi<Row>) => add(api, null),
};버튼을 누르면 이 예제의 코드가 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.first(api)"
>개발본부 맨 앞에</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.last(api)"
>개발본부 끝에</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.top(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: 'all' },
};
let next = 100;
const add = (api: GridApi<Row>, parent: number | null, index?: number): void => {
const id = (next += 1);
api.addRows([{ id, name: `새 직원 ${id}`, title: '사원' } as Row], {
parent,
...(index === undefined ? {} : { index }),
});
say({
ko: `${id}번을 ${parent === null ? '최상위' : `${nameOf(api, parent)} 아래`}에 넣었어요 (부모 필드 ${api.getRowByKey(id)?.parent ?? 'null'})`,
en: `Added ${id} ${parent === null ? 'at the top level' : `under ${nameOf(api, parent)}`} (parent field ${api.getRowByKey(id)?.parent ?? 'null'})`,
});
};
export const actions = {
first: (api: GridApi<Row>) => add(api, 2, 0),
last: (api: GridApi<Row>) => add(api, 2),
top: (api: GridApi<Row>) => add(api, null),
};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="first">First under Dev</button>
<button type="button" id="last">Last under Dev</button>
<button type="button" id="top">To top level</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('first').addEventListener('click', () => actions.first(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
document.getElementById('top').addEventListener('click', () => actions.top(api));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 () => { };
}
const nameOf = (api, key) => api.getRowByKey(key)?.name ?? String(key);
export const options = {
...base,
tree: { parentKey: 'parent', column: 'name', expandDepth: 'all' },
};
let next = 100;
const add = (api, parent, index) => {
const id = (next += 1);
api.addRows([{ id, name: `New employee ${id}`, title: 'Staff' }], {
parent,
...(index === undefined ? {} : { index }),
});
say(`Added ${id} ${parent === null ? 'at the top level' : `under ${nameOf(api, parent)}`} (parent field ${api.getRowByKey(id)?.parent ?? 'null'})`);
};
export const actions = {
first: (api) => add(api, 2, 0),
last: (api) => add(api, 2),
top: (api) => add(api, null),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Insert a child row into a tree</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="first">First under Dev</button>
<button type="button" id="last">Last under Dev</button>
<button type="button" id="top">To top level</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', 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 = () => { };
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: 'all' },
};
let next = 100;
const add = (api, parent, index) => {
const id = (next += 1);
api.addRows([{ id, name: `New employee ${id}`, title: 'Staff' }], {
parent,
...(index === undefined ? {} : { index }),
});
say(`Added ${id} ${parent === null ? 'at the top level' : `under ${nameOf(api, parent)}`} (parent field ${api.getRowByKey(id)?.parent ?? 'null'})`);
};
const actions = {
first: (api) => add(api, 2, 0),
last: (api) => add(api, 2),
top: (api) => add(api, null),
};
options.ariaLabel = "Insert a child row into a tree";
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('first').addEventListener('click', () => actions.first(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
document.getElementById('top').addEventListener('click', () => actions.top(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.first(api)}
>First under Dev</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.last(api)}
>Last under Dev</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.top(api)}
>To top level</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', 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: (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: 'all' },
};
let next = 100;
const add = (api: GridApi<Row>, parent: number | null, index?: number): void => {
const id = (next += 1);
api.addRows([{ id, name: `New employee ${id}`, title: 'Staff' } as Row], {
parent,
...(index === undefined ? {} : { index }),
});
say(
`Added ${id} ${parent === null ? 'at the top level' : `under ${nameOf(api, parent)}`} (parent field ${api.getRowByKey(id)?.parent ?? 'null'})`,
);
};
export const actions = {
first: (api: GridApi<Row>) => add(api, 2, 0),
last: (api: GridApi<Row>) => add(api, 2),
top: (api: GridApi<Row>) => add(api, null),
};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.first(api)">First under Dev</button>
<button type="button" :disabled="!api" @click="api && actions.last(api)">Last under Dev</button>
<button type="button" :disabled="!api" @click="api && actions.top(api)">To top level</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', 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: (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: 'all' },
};
let next = 100;
const add = (api: GridApi<Row>, parent: number | null, index?: number): void => {
const id = (next += 1);
api.addRows([{ id, name: `New employee ${id}`, title: 'Staff' } as Row], {
parent,
...(index === undefined ? {} : { index }),
});
say(
`Added ${id} ${parent === null ? 'at the top level' : `under ${nameOf(api, parent)}`} (parent field ${api.getRowByKey(id)?.parent ?? 'null'})`,
);
};
export const actions = {
first: (api: GridApi<Row>) => add(api, 2, 0),
last: (api: GridApi<Row>) => add(api, 2),
top: (api: GridApi<Row>) => add(api, null),
};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.first(api)"
>First under Dev</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.last(api)"
>Last under Dev</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.top(api)"
>To top level</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', 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: (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: 'all' },
};
let next = 100;
const add = (api: GridApi<Row>, parent: number | null, index?: number): void => {
const id = (next += 1);
api.addRows([{ id, name: `New employee ${id}`, title: 'Staff' } as Row], {
parent,
...(index === undefined ? {} : { index }),
});
say(
`Added ${id} ${parent === null ? 'at the top level' : `under ${nameOf(api, parent)}`} (parent field ${api.getRowByKey(id)?.parent ?? 'null'})`,
);
};
export const actions = {
first: (api: GridApi<Row>) => add(api, 2, 0),
last: (api: GridApi<Row>) => add(api, 2),
top: (api: GridApi<Row>) => add(api, null),
};