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

트리를 정렬해도 가지는 그대로Sorting keeps the branches intact

트리에서 정렬하면 같은 부모를 둔 줄끼리만 순서가 바뀌고, 부모는 자기 자식들을 데리고 함께 움직여요. 그래서 정렬해도 트리 모양은 깨지지 않아요.Sorting a tree reorders only the rows that share a parent, and each parent moves together with its children, so the tree shape never breaks.

입문Beginner
직접 써 보기Live preview트리를 정렬해도 가지는 그대로Sorting keeps the branches intact

코드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">
    <select aria-label="정렬" id="sort">
      <option value="none" selected>정렬 안 함</option>
      <option value="task">작업 이름순</option>
      <option value="end">종료일 늦은 순</option>
    </select>
    <button type="button" id="expandAll">모두 열기</button>
    <button type="button" id="collapseAll">모두 닫기</button>
  </div>
  <div id="grid" style="height: 490px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('sort').addEventListener('change', (event) => actions.sort(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));
example.js
const rows = [
  {
    id: 'T1',
    parentId: null,
    task: '출시 준비',
    owner: '김서연',
    start: '2026-11-02',
    end: '2026-12-18',
  },
  {
    id: 'T10',
    parentId: 'T1',
    task: '기획',
    owner: '이준호',
    start: '2026-11-02',
    end: '2026-11-13',
  },
  {
    id: 'T11',
    parentId: 'T10',
    task: '요구사항 정리',
    owner: '이준호',
    start: '2026-11-02',
    end: '2026-11-06',
  },
  {
    id: 'T12',
    parentId: 'T10',
    task: '화면 설계',
    owner: '박민지',
    start: '2026-11-09',
    end: '2026-11-13',
  },
  {
    id: 'T20',
    parentId: 'T1',
    task: '개발',
    owner: '최도윤',
    start: '2026-11-16',
    end: '2026-12-04',
  },
  {
    id: 'T21',
    parentId: 'T20',
    task: '프런트엔드',
    owner: '정하은',
    start: '2026-11-16',
    end: '2026-11-27',
  },
  {
    id: 'T211',
    parentId: 'T21',
    task: '로그인 화면',
    owner: '정하은',
    start: '2026-11-16',
    end: '2026-11-20',
  },
  {
    id: 'T212',
    parentId: 'T21',
    task: '목록 화면',
    owner: '강지후',
    start: '2026-11-23',
    end: '2026-11-27',
  },
  {
    id: 'T22',
    parentId: 'T20',
    task: '백엔드',
    owner: '최도윤',
    start: '2026-11-16',
    end: '2026-12-04',
  },
  {
    id: 'T221',
    parentId: 'T22',
    task: 'API 설계',
    owner: '최도윤',
    start: '2026-11-16',
    end: '2026-11-20',
  },
  {
    id: 'T222',
    parentId: 'T22',
    task: '데이터베이스 구성',
    owner: '윤서아',
    start: '2026-11-23',
    end: '2026-12-04',
  },
  {
    id: 'T30',
    parentId: 'T1',
    task: '테스트',
    owner: '박민지',
    start: '2026-12-07',
    end: '2026-12-18',
  },
  {
    id: 'T31',
    parentId: 'T30',
    task: '기능 테스트',
    owner: '박민지',
    start: '2026-12-07',
    end: '2026-12-11',
  },
  {
    id: 'T32',
    parentId: 'T30',
    task: '성능 테스트',
    owner: '임태민',
    start: '2026-12-14',
    end: '2026-12-18',
  },
  {
    id: 'T2',
    parentId: null,
    task: '운영 준비',
    owner: '김서연',
    start: '2026-12-07',
    end: '2026-12-23',
  },
  {
    id: 'T40',
    parentId: 'T2',
    task: '교육 자료 작성',
    owner: '한지우',
    start: '2026-12-07',
    end: '2026-12-11',
  },
  {
    id: 'T41',
    parentId: 'T2',
    task: '배포 점검',
    owner: '오세훈',
    start: '2026-12-14',
    end: '2026-12-23',
  },
  {
    id: 'T411',
    parentId: 'T41',
    task: '서버 점검',
    owner: '오세훈',
    start: '2026-12-14',
    end: '2026-12-18',
  },
];
export const options = {
  columns: [
    { id: 'task', title: '작업', width: 220 },
    { id: 'owner', title: '담당자', width: 100 },
    { id: 'start', title: '시작', width: 110 },
    { id: 'end', title: '종료', width: 110 },
  ],
  rows,
  rowKey: 'id',
  tree: { parentKey: 'parentId', column: 'task', expandDepth: 2 },
};
// 트리에서 정렬하면 같은 부모를 둔 줄끼리만 순서가 바뀌어요. 부모는 자식들을 데리고 함께 움직이고요.
export const actions = {
  sort: (api, value) => {
    api.setSortModel(value === 'task'
      ? [{ columnId: 'task', direction: 'asc' }]
      : value === 'end'
        ? [{ columnId: 'end', direction: 'desc' }]
        : []);
  },
  expandAll: (api) => api.expandAll(),
  collapseAll: (api) => api.collapseAll(),
};

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">
    <select aria-label="Sort" id="sort">
      <option value="none" selected>No sorting</option>
      <option value="task">Task name</option>
      <option value="end">End date, latest first</option>
    </select>
    <button type="button" id="expandAll">Expand all</button>
    <button type="button" id="collapseAll">Collapse all</button>
  </div>
  <div id="grid" style="height: 490px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('sort').addEventListener('change', (event) => actions.sort(api, event.target.value));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));
example.js
const rows = [
  {
    id: 'T1',
    parentId: null,
    task: 'Release',
    owner: 'Emma Stone',
    start: '2026-11-02',
    end: '2026-12-18',
  },
  {
    id: 'T10',
    parentId: 'T1',
    task: 'Planning',
    owner: 'Liam Carter',
    start: '2026-11-02',
    end: '2026-11-13',
  },
  {
    id: 'T11',
    parentId: 'T10',
    task: 'Gather requirements',
    owner: 'Liam Carter',
    start: '2026-11-02',
    end: '2026-11-06',
  },
  {
    id: 'T12',
    parentId: 'T10',
    task: 'Screen design',
    owner: 'Mia Parker',
    start: '2026-11-09',
    end: '2026-11-13',
  },
  {
    id: 'T20',
    parentId: 'T1',
    task: 'Development',
    owner: 'Noah Davis',
    start: '2026-11-16',
    end: '2026-12-04',
  },
  {
    id: 'T21',
    parentId: 'T20',
    task: 'Frontend',
    owner: 'Ava Moore',
    start: '2026-11-16',
    end: '2026-11-27',
  },
  {
    id: 'T211',
    parentId: 'T21',
    task: 'Login screen',
    owner: 'Ava Moore',
    start: '2026-11-16',
    end: '2026-11-20',
  },
  {
    id: 'T212',
    parentId: 'T21',
    task: 'List screen',
    owner: 'Jack Reed',
    start: '2026-11-23',
    end: '2026-11-27',
  },
  {
    id: 'T22',
    parentId: 'T20',
    task: 'Backend',
    owner: 'Noah Davis',
    start: '2026-11-16',
    end: '2026-12-04',
  },
  {
    id: 'T221',
    parentId: 'T22',
    task: 'API design',
    owner: 'Noah Davis',
    start: '2026-11-16',
    end: '2026-11-20',
  },
  {
    id: 'T222',
    parentId: 'T22',
    task: 'Database setup',
    owner: 'Chloe Young',
    start: '2026-11-23',
    end: '2026-12-04',
  },
  {
    id: 'T30',
    parentId: 'T1',
    task: 'Testing',
    owner: 'Mia Parker',
    start: '2026-12-07',
    end: '2026-12-18',
  },
  {
    id: 'T31',
    parentId: 'T30',
    task: 'Functional test',
    owner: 'Mia Parker',
    start: '2026-12-07',
    end: '2026-12-11',
  },
  {
    id: 'T32',
    parentId: 'T30',
    task: 'Performance test',
    owner: 'Ethan Lee',
    start: '2026-12-14',
    end: '2026-12-18',
  },
  {
    id: 'T2',
    parentId: null,
    task: 'Operations prep',
    owner: 'Emma Stone',
    start: '2026-12-07',
    end: '2026-12-23',
  },
  {
    id: 'T40',
    parentId: 'T2',
    task: 'Write training materials',
    owner: 'Grace Hall',
    start: '2026-12-07',
    end: '2026-12-11',
  },
  {
    id: 'T41',
    parentId: 'T2',
    task: 'Deployment check',
    owner: 'Henry Ford',
    start: '2026-12-14',
    end: '2026-12-23',
  },
  {
    id: 'T411',
    parentId: 'T41',
    task: 'Server check',
    owner: 'Henry Ford',
    start: '2026-12-14',
    end: '2026-12-18',
  },
];
export const options = {
  columns: [
    { id: 'task', title: 'Task', width: 220 },
    { id: 'owner', title: 'Owner', width: 120 },
    { id: 'start', title: 'Start', width: 110 },
    { id: 'end', title: 'End', width: 110 },
  ],
  rows,
  rowKey: 'id',
  tree: { parentKey: 'parentId', column: 'task', expandDepth: 2 },
};
// Sorting a tree reorders only the rows that share a parent, and each parent moves together with its children.
export const actions = {
  sort: (api, value) => {
    api.setSortModel(value === 'task'
      ? [{ columnId: 'task', direction: 'asc' }]
      : value === 'end'
        ? [{ columnId: 'end', direction: 'desc' }]
        : []);
  },
  expandAll: (api) => api.expandAll(),
  collapseAll: (api) => api.collapseAll(),
};