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

트리에서는 형제끼리 번호를 매겨요Order numbers per sibling group in a tree

트리에서 orderIndex 번호는 같은 부모를 가진 형제끼리 따로 매겨요. 행을 위로 옮기거나 들여쓰면 새 형제들 사이에서 번호를 받고, renumberOrder는 부모마다 처음부터 다시 매겨요.In a tree the orderIndex numbers are kept per group of siblings. A row moved up or indented gets a number among its new siblings, and renumberOrder renumbers each parent from the start.

활용Intermediate
직접 써 보기Live preview트리에서는 형제끼리 번호를 매겨요Order numbers per sibling group 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="moveChildUp">자식 위로</button>
    <button type="button" id="indent">들여쓰기</button>
    <button type="button" id="renumber">다시 매기기</button>
  </div>
  <div id="grid" style="height: 400px"></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('moveChildUp').addEventListener('click', () => actions.moveChildUp(api));
document.getElementById('indent').addEventListener('click', () => actions.indent(api));
document.getElementById('renumber').addEventListener('click', () => actions.renumber(api));
example.js
// 순서 번호는 같은 부모를 가진 형제끼리 따로 매겨요.
export const options = {
  columns: [
    { id: 'name', title: '항목', width: 200 },
    { id: 'seq', title: '순서 번호', width: 90, align: 'end' },
  ],
  rows: [
    { id: 1, parent: null, seq: 10, name: '기획' },
    { id: 2, parent: 1, seq: 10, name: '요구사항 정리' },
    { id: 3, parent: 1, seq: 20, name: '범위 확정' },
    { id: 4, parent: 1, seq: 30, name: '일정 수립' },
    { id: 5, parent: null, seq: 20, name: '개발' },
    { id: 6, parent: 5, seq: 10, name: '화면' },
    { id: 7, parent: 5, seq: 20, name: '서버' },
    { id: 8, parent: null, seq: 30, name: '검수' },
    { id: 9, parent: 8, seq: 10, name: '기능 시험' },
    { id: 10, parent: 8, seq: 20, name: '성능 시험' },
  ],
  rowKey: 'id',
  rowDrag: true,
  tree: { parentKey: 'parent', column: 'name', expandDepth: 'all' },
  orderIndex: { field: 'seq' },
};
let say = () => { };
export function setup(api, log) {
  say = log;
  log({
    ko: '행을 옮기거나 들여쓰면 새 부모 아래 형제들 사이에서 번호가 매겨져요.',
    en: 'Moving or indenting a row numbers it among its new siblings.',
  });
  const off = api.on('orderIndexChanged', (e) => {
    const seqs = e.rowKeys.map((k) => `${k}번=${api.getRowByKey(k)?.seq}`).join(', ');
    say({
      ko: `번호 바뀜: ${seqs} (${e.renumbered ? '형제 전체를 다시 매김' : '옮긴 행만'})`,
      en: `Order changed: ${seqs} (${e.renumbered ? 'renumbered all siblings' : 'moved rows only'})`,
    });
  });
  return () => off();
}
export const actions = {
  moveChildUp: (api) => api.moveRowsBy([3], -1),
  indent: (api) => {
    try {
      api.indentRows([7]);
    }
    catch (error) {
      say({ ko: error.message, en: error.message });
    }
  },
  renumber: (api) => {
    const n = api.renumberOrder();
    say({ ko: `${n}개 행의 번호를 다시 매겼어요`, en: `Renumbered ${n} rows` });
  },
};

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="moveChildUp">Child up</button>
    <button type="button" id="indent">Indent</button>
    <button type="button" id="renumber">Renumber</button>
  </div>
  <div id="grid" style="height: 400px"></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('moveChildUp').addEventListener('click', () => actions.moveChildUp(api));
document.getElementById('indent').addEventListener('click', () => actions.indent(api));
document.getElementById('renumber').addEventListener('click', () => actions.renumber(api));
example.js
// Order numbers are kept separately for each group of siblings.
export const options = {
  columns: [
    { id: 'name', title: 'Item', width: 200 },
    { id: 'seq', title: 'Order', width: 80, align: 'end' },
  ],
  rows: [
    { id: 1, parent: null, seq: 10, name: 'Planning' },
    { id: 2, parent: 1, seq: 10, name: 'Gather requirements' },
    { id: 3, parent: 1, seq: 20, name: 'Confirm scope' },
    { id: 4, parent: 1, seq: 30, name: 'Set schedule' },
    { id: 5, parent: null, seq: 20, name: 'Development' },
    { id: 6, parent: 5, seq: 10, name: 'Screens' },
    { id: 7, parent: 5, seq: 20, name: 'Server' },
    { id: 8, parent: null, seq: 30, name: 'Acceptance' },
    { id: 9, parent: 8, seq: 10, name: 'Functional test' },
    { id: 10, parent: 8, seq: 20, name: 'Performance test' },
  ],
  rowKey: 'id',
  rowDrag: true,
  tree: { parentKey: 'parent', column: 'name', expandDepth: 'all' },
  orderIndex: { field: 'seq' },
};
let say = () => { };
export function setup(api, log) {
  say = log;
  log('Moving or indenting a row numbers it among its new siblings.');
  const off = api.on('orderIndexChanged', (e) => {
    const seqs = e.rowKeys.map((k) => `row ${k}=${api.getRowByKey(k)?.seq}`).join(', ');
    say(`Order changed: ${seqs} (${e.renumbered ? 'renumbered all siblings' : 'moved rows only'})`);
  });
  return () => off();
}
export const actions = {
  moveChildUp: (api) => api.moveRowsBy([3], -1),
  indent: (api) => {
    try {
      api.indentRows([7]);
    }
    catch (error) {
      say(error.message);
    }
  },
  renumber: (api) => {
    const n = api.renumberOrder();
    say(`Renumbered ${n} rows`);
  },
};