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

트리에서 행을 다른 부모 아래로 옮기기Move rows under another parent in a tree

moveRows의 childOf·before·index로 조직을 다른 부모 아래나 원하는 자리로 옮겨요. 가지째 움직이고, 자기 자손 안으로는 옮길 수 없어요.moveRows with childOf, before or index moves units under another parent or to a place you choose. The branch goes along, and a move into your own descendants is refused.

활용Intermediate
직접 써 보기Live preview트리에서 행을 다른 부모 아래로 옮기기Move rows under another parent in 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="underMobile">모바일팀 아래로</button>
    <button type="button" id="firstUnderDesign">디자인본부 맨 앞 자식으로</button>
    <button type="button" id="beforeDesign">디자인본부 앞으로</button>
    <button type="button" id="toTop">맨 위로</button>
    <button type="button" id="intoSelf">인프라파트 아래로(자손이면 막힘)</button>
    <button type="button" id="undo">되돌리기</button>
  </div>
  <div id="grid" style="height: 380px"></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('underMobile').addEventListener('click', () => actions.underMobile(api));
document.getElementById('firstUnderDesign').addEventListener('click', () => actions.firstUnderDesign(api));
document.getElementById('beforeDesign').addEventListener('click', () => actions.beforeDesign(api));
document.getElementById('toTop').addEventListener('click', () => actions.toTop(api));
document.getElementById('intoSelf').addEventListener('click', () => actions.intoSelf(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
example.js
const rows = [
  { id: 1, parentId: null, name: '개발본부' },
  { id: 2, parentId: 1, name: '플랫폼팀' },
  { id: 3, parentId: 2, name: '인프라파트' },
  { id: 4, parentId: 2, name: '데이터파트' },
  { id: 5, parentId: 1, name: '모바일팀' },
  { id: 6, parentId: null, name: '디자인본부' },
  { id: 7, parentId: 6, name: '브랜드팀' },
];
export const options = {
  columns: [
    { id: 'name', title: '조직', width: 180 },
    { id: 'parentId', title: '부모', width: 80, align: 'end' },
  ],
  rows,
  rowKey: 'id',
  selection: 'multiple',
  tree: { parentKey: 'parentId', column: 'name', expandDepth: 'all' },
};
let say = () => { };
export function setup(api, log) {
  say = log;
  const off = api.on('rowsMoved', (e) => say({
    ko: `옮김 ${e.keys.join(', ')} → 부모 ${e.parentKey ?? '없음'}, ${e.index + 1}번째`,
    en: `Moved ${e.keys.join(', ')} → parent ${e.parentKey ?? 'none'}, position ${e.index + 1}`,
  }));
  return () => off();
}
function run(api, move) {
  const keys = api.getSelectedKeys();
  if (keys.length === 0) {
    say({ ko: '먼저 옮길 조직을 골라 주세요', en: 'Select the units to move first' });
    return;
  }
  try {
    move(keys);
  }
  catch (error) {
    say({ ko: error.message, en: error.message });
  }
}
export const actions = {
  underMobile: (api) => run(api, (k) => api.moveRows(k, { childOf: 5 })),
  firstUnderDesign: (api) => run(api, (k) => api.moveRows(k, { childOf: 6, index: 0 })),
  beforeDesign: (api) => run(api, (k) => api.moveRows(k, { before: 6 })),
  toTop: (api) => run(api, (k) => api.moveRows(k, { index: 0 })),
  intoSelf: (api) => run(api, (k) => api.moveRows(k, { childOf: 3 })),
  undo: (api) => {
    api.undo();
  },
};

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="underMobile">Under Mobile</button>
    <button type="button" id="firstUnderDesign">First child of Design</button>
    <button type="button" id="beforeDesign">Before Design</button>
    <button type="button" id="toTop">To top</button>
    <button type="button" id="intoSelf">Under Infra (refused for ancestors)</button>
    <button type="button" id="undo">Undo</button>
  </div>
  <div id="grid" style="height: 380px"></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('underMobile').addEventListener('click', () => actions.underMobile(api));
document.getElementById('firstUnderDesign').addEventListener('click', () => actions.firstUnderDesign(api));
document.getElementById('beforeDesign').addEventListener('click', () => actions.beforeDesign(api));
document.getElementById('toTop').addEventListener('click', () => actions.toTop(api));
document.getElementById('intoSelf').addEventListener('click', () => actions.intoSelf(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
example.js
const rows = [
  { id: 1, parentId: null, name: 'Dev Division' },
  { id: 2, parentId: 1, name: 'Platform Team' },
  { id: 3, parentId: 2, name: 'Infra Part' },
  { id: 4, parentId: 2, name: 'Data Part' },
  { id: 5, parentId: 1, name: 'Mobile Team' },
  { id: 6, parentId: null, name: 'Design Division' },
  { id: 7, parentId: 6, name: 'Brand Team' },
];
export const options = {
  columns: [
    { id: 'name', title: 'Unit', width: 180 },
    { id: 'parentId', title: 'Parent', width: 80, align: 'end' },
  ],
  rows,
  rowKey: 'id',
  selection: 'multiple',
  tree: { parentKey: 'parentId', column: 'name', expandDepth: 'all' },
};
let say = () => { };
export function setup(api, log) {
  say = log;
  const off = api.on('rowsMoved', (e) => say(`Moved ${e.keys.join(', ')} → parent ${e.parentKey ?? 'none'}, position ${e.index + 1}`));
  return () => off();
}
function run(api, move) {
  const keys = api.getSelectedKeys();
  if (keys.length === 0) {
    say('Select the units to move first');
    return;
  }
  try {
    move(keys);
  }
  catch (error) {
    say(error.message);
  }
}
export const actions = {
  underMobile: (api) => run(api, (k) => api.moveRows(k, { childOf: 5 })),
  firstUnderDesign: (api) => run(api, (k) => api.moveRows(k, { childOf: 6, index: 0 })),
  beforeDesign: (api) => run(api, (k) => api.moveRows(k, { before: 6 })),
  toTop: (api) => run(api, (k) => api.moveRows(k, { index: 0 })),
  intoSelf: (api) => run(api, (k) => api.moveRows(k, { childOf: 3 })),
  undo: (api) => {
    api.undo();
  },
};