트리를 들여쓰기해서 내보내기Export a tree with indentation
펼친 트리를 깊이만큼 들여쓴 줄로 CSV·xlsx에 담아요. 접힌 가지는 파일에서 빠져요.Export the expanded tree as indented lines in CSV or xlsx. Collapsed branches are left out.
코드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)
이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i maxgrid @maxgrid/export로 설치하세요.
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<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>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));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 }),
}; 이 HTML은 추가 번들(위 script 태그)도 함께 불러오고 전역 변수로 써요. 별도 설치는 필요 없어요.
<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>트리를 들여쓰기해서 내보내기</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.export.min.js"></script>
<script>
const { exportCsv, withExport } = MaxGridExport;
// 트리는 화면에 펼친 줄 그대로, 깊이만큼 들여써서 내보내요. 접힌 가지는 빠져요.
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 = () => { };
function setup(api, log) {
say = log;
withExport(api);
log({
ko: '처음에는 맨 위 단계만 펼쳐져 있어요.',
en: 'At first only the top level is expanded.',
});
}
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 }),
};
options.ariaLabel = "트리를 들여쓰기해서 내보내기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
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));
</script>
</body>
</html> 이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i @maxgrid/react @maxgrid/export로 설치하세요.
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.expandAll(api)}
>모두 펼치기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.collapseAll(api)}
>모두 접기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.preview(api)}
>미리 보기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.download(api)}
>xlsx 내려받기</button>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import { exportCsv, withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
parent: number | null;
name: string;
headcount: number;
}
// 트리는 화면에 펼친 줄 그대로, 깊이만큼 들여써서 내보내요. 접힌 가지는 빠져요.
export const options: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withExport(api);
log({
ko: '처음에는 맨 위 단계만 펼쳐져 있어요.',
en: 'At first only the top level is expanded.',
});
}
export const actions = {
expandAll: (api: GridApi<Row>) => api.expandAll(),
collapseAll: (api: GridApi<Row>) => api.collapseAll(),
preview: (api: GridApi<Row>) => {
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: GridApi<Row>) =>
void withExport(api).downloadXlsx('조직도.xlsx', { hierarchy: true }),
}; 이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i @maxgrid/vue @maxgrid/export로 설치하세요.
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button
type="button"
:disabled="!api"
@click="api && actions.expandAll(api)"
>모두 펼치기</button>
<button
type="button"
:disabled="!api"
@click="api && actions.collapseAll(api)"
>모두 접기</button>
<button type="button" :disabled="!api" @click="api && actions.preview(api)">미리 보기</button>
<button
type="button"
:disabled="!api"
@click="api && actions.download(api)"
>xlsx 내려받기</button>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import { exportCsv, withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
parent: number | null;
name: string;
headcount: number;
}
// 트리는 화면에 펼친 줄 그대로, 깊이만큼 들여써서 내보내요. 접힌 가지는 빠져요.
export const options: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withExport(api);
log({
ko: '처음에는 맨 위 단계만 펼쳐져 있어요.',
en: 'At first only the top level is expanded.',
});
}
export const actions = {
expandAll: (api: GridApi<Row>) => api.expandAll(),
collapseAll: (api: GridApi<Row>) => api.collapseAll(),
preview: (api: GridApi<Row>) => {
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: GridApi<Row>) =>
void withExport(api).downloadXlsx('조직도.xlsx', { hierarchy: true }),
}; 이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i @maxgrid/angular @maxgrid/export로 설치하세요.
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.expandAll(api)"
>모두 펼치기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.collapseAll(api)"
>모두 접기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.preview(api)"
>미리 보기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.download(api)"
>xlsx 내려받기</button>
</div>
<mx-grid style="height: 300px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import { exportCsv, withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
parent: number | null;
name: string;
headcount: number;
}
// 트리는 화면에 펼친 줄 그대로, 깊이만큼 들여써서 내보내요. 접힌 가지는 빠져요.
export const options: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withExport(api);
log({
ko: '처음에는 맨 위 단계만 펼쳐져 있어요.',
en: 'At first only the top level is expanded.',
});
}
export const actions = {
expandAll: (api: GridApi<Row>) => api.expandAll(),
collapseAll: (api: GridApi<Row>) => api.collapseAll(),
preview: (api: GridApi<Row>) => {
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: GridApi<Row>) =>
void withExport(api).downloadXlsx('조직도.xlsx', { hierarchy: true }),
}; 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.
<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>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));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 }),
};This HTML also loads an extra bundle (the extra script tag above) and uses it as a global. No install is needed.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Export a tree with indentation</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.export.min.js"></script>
<script>
const { exportCsv, withExport } = MaxGridExport;
// A tree is exported as the expanded rows on screen, indented by depth. Collapsed branches are left out.
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 = () => { };
function setup(api, log) {
say = log;
withExport(api);
log('At first only the top level is expanded.');
}
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 }),
};
options.ariaLabel = "Export a tree with indentation";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
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));
</script>
</body>
</html> This example needs an extra package. To run it locally, install with npm i @maxgrid/react @maxgrid/export.
When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.expandAll(api)}
>Expand all</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.collapseAll(api)}
>Collapse all</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.preview(api)}
>Preview</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.download(api)}
>Download xlsx</button>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import { exportCsv, withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
parent: number | null;
name: string;
headcount: number;
}
// A tree is exported as the expanded rows on screen, indented by depth. Collapsed branches are left out.
export const options: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withExport(api);
log('At first only the top level is expanded.');
}
export const actions = {
expandAll: (api: GridApi<Row>) => api.expandAll(),
collapseAll: (api: GridApi<Row>) => api.collapseAll(),
preview: (api: GridApi<Row>) => {
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: GridApi<Row>) =>
void withExport(api).downloadXlsx('org-chart.xlsx', { hierarchy: true }),
}; This example needs an extra package. To run it locally, install with npm i @maxgrid/vue @maxgrid/export.
When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button
type="button"
:disabled="!api"
@click="api && actions.expandAll(api)"
>Expand all</button>
<button
type="button"
:disabled="!api"
@click="api && actions.collapseAll(api)"
>Collapse all</button>
<button type="button" :disabled="!api" @click="api && actions.preview(api)">Preview</button>
<button
type="button"
:disabled="!api"
@click="api && actions.download(api)"
>Download xlsx</button>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import { exportCsv, withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
parent: number | null;
name: string;
headcount: number;
}
// A tree is exported as the expanded rows on screen, indented by depth. Collapsed branches are left out.
export const options: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withExport(api);
log('At first only the top level is expanded.');
}
export const actions = {
expandAll: (api: GridApi<Row>) => api.expandAll(),
collapseAll: (api: GridApi<Row>) => api.collapseAll(),
preview: (api: GridApi<Row>) => {
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: GridApi<Row>) =>
void withExport(api).downloadXlsx('org-chart.xlsx', { hierarchy: true }),
}; This example needs an extra package. To run it locally, install with npm i @maxgrid/angular @maxgrid/export.
When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.expandAll(api)"
>Expand all</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.collapseAll(api)"
>Collapse all</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.preview(api)"
>Preview</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.download(api)"
>Download xlsx</button>
</div>
<mx-grid style="height: 300px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import { exportCsv, withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
parent: number | null;
name: string;
headcount: number;
}
// A tree is exported as the expanded rows on screen, indented by depth. Collapsed branches are left out.
export const options: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withExport(api);
log('At first only the top level is expanded.');
}
export const actions = {
expandAll: (api: GridApi<Row>) => api.expandAll(),
collapseAll: (api: GridApi<Row>) => api.collapseAll(),
preview: (api: GridApi<Row>) => {
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: GridApi<Row>) =>
void withExport(api).downloadXlsx('org-chart.xlsx', { hierarchy: true }),
};