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

트리에 자식 행 끼워 넣기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.

활용Intermediate
직접 써 보기Live preview트리에 자식 행 끼워 넣기Insert a child row into a tree

코드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="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>
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('first').addEventListener('click', () => actions.first(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
document.getElementById('top').addEventListener('click', () => actions.top(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: '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),
};

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="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>
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('first').addEventListener('click', () => actions.first(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));
document.getElementById('top').addEventListener('click', () => actions.top(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 () => { };
}
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),
};