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

트리를 들여쓰기해서 내보내기Export a tree with indentation

펼친 트리를 깊이만큼 들여쓴 줄로 CSV·xlsx에 담아요. 접힌 가지는 파일에서 빠져요.Export the expanded tree as indented lines in CSV or xlsx. Collapsed branches are left out.

활용Intermediate
직접 써 보기Live preview트리를 들여쓰기해서 내보내기Export a tree with indentation

코드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 탭이나 직접 해보기를 쓰세요.

이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i maxgrid @maxgrid/export로 설치하세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<div id="app">
  <div class="toolbar">
    <button type="button" id="expandAll">모두 펼치기</button>
    <button type="button" id="collapseAll">모두 접기</button>
    <button type="button" id="preview">미리 보기</button>
    <button type="button" id="download">xlsx 내려받기</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('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));
document.getElementById('preview').addEventListener('click', () => actions.preview(api));
document.getElementById('download').addEventListener('click', () => actions.download(api));
example.js
import { exportCsv, withExport } from '@maxgrid/export';
// 트리는 화면에 펼친 줄 그대로, 깊이만큼 들여써서 내보내요. 접힌 가지는 빠져요.
export const options = {
  columns: [
    { id: 'name', title: '조직', width: 220 },
    { id: 'headcount', title: '인원', kind: 'number', width: 90, align: 'end' },
  ],
  rows: [
    { id: 1, parent: null, name: '본사', headcount: 4 },
    { id: 2, parent: 1, name: '개발본부', headcount: 6 },
    { id: 3, parent: 2, name: '프런트엔드팀', headcount: 12 },
    { id: 4, parent: 2, name: '백엔드팀', headcount: 15 },
    { id: 5, parent: 1, name: '디자인본부', headcount: 7 },
    { id: 6, parent: 5, name: '브랜드팀', headcount: 3 },
  ],
  rowKey: 'id',
  tree: { parentKey: 'parent', column: 'name', expandDepth: 1 },
};
let say = () => { };
export function setup(api, log) {
  say = log;
  withExport(api);
  log({
    ko: '처음에는 맨 위 단계만 펼쳐져 있어요.',
    en: 'At first only the top level is expanded.',
  });
}
export const actions = {
  expandAll: (api) => api.expandAll(),
  collapseAll: (api) => api.collapseAll(),
  preview: (api) => {
    const text = exportCsv(api, { bom: false, hierarchy: true, newline: '\n' });
    // 들여쓴 공백이 화면에서 줄어들지 않게, 줄 앞의 공백을 줄바꿈 없는 공백으로 바꿔 보여 줘요.
    [...text.split('\n')]
      .reverse()
      .forEach((line) => say(line.replace(/^ +/, (spaces) => '\u00a0'.repeat(spaces.length))));
  },
  download: (api) => void withExport(api).downloadXlsx('조직도.xlsx', { hierarchy: true }),
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

This example needs an extra package. To run it locally, install with npm i maxgrid @maxgrid/export.

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="expandAll">Expand all</button>
    <button type="button" id="collapseAll">Collapse all</button>
    <button type="button" id="preview">Preview</button>
    <button type="button" id="download">Download xlsx</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('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));
document.getElementById('preview').addEventListener('click', () => actions.preview(api));
document.getElementById('download').addEventListener('click', () => actions.download(api));
example.js
import { exportCsv, withExport } from '@maxgrid/export';
// A tree is exported as the expanded rows on screen, indented by depth. Collapsed branches are left out.
export const options = {
  columns: [
    { id: 'name', title: 'Organization', width: 220 },
    { id: 'headcount', title: 'Headcount', kind: 'number', width: 100, align: 'end' },
  ],
  rows: [
    { id: 1, parent: null, name: 'Headquarters', headcount: 4 },
    { id: 2, parent: 1, name: 'Engineering', headcount: 6 },
    { id: 3, parent: 2, name: 'Frontend Team', headcount: 12 },
    { id: 4, parent: 2, name: 'Backend Team', headcount: 15 },
    { id: 5, parent: 1, name: 'Design', headcount: 7 },
    { id: 6, parent: 5, name: 'Brand Team', headcount: 3 },
  ],
  rowKey: 'id',
  tree: { parentKey: 'parent', column: 'name', expandDepth: 1 },
};
let say = () => { };
export function setup(api, log) {
  say = log;
  withExport(api);
  log('At first only the top level is expanded.');
}
export const actions = {
  expandAll: (api) => api.expandAll(),
  collapseAll: (api) => api.collapseAll(),
  preview: (api) => {
    const text = exportCsv(api, { bom: false, hierarchy: true, newline: '\n' });
    // So the indentation does not collapse on screen, turn leading spaces into non-breaking spaces.
    [...text.split('\n')]
      .reverse()
      .forEach((line) => say(line.replace(/^ +/, (spaces) => ' '.repeat(spaces.length))));
  },
  download: (api) => void withExport(api).downloadXlsx('org-chart.xlsx', { hierarchy: true }),
};