보이는 행을 JSON으로 내보내기Export the visible rows as JSON
withExport(api)가 붙이는 exportJson()은 보이는 행(정렬·필터 반영)을 컬럼 id를 열쇠로 하는 객체 배열 JSON으로 만들어요. 기본은 저장된 값 그대로이고 text: 'shown'이면 화면 글자예요. scope로 전체·보이는 행·선택한 행을, columns로 컬럼과 순서를 고르고 pretty로 들여써요. downloadJson()은 파일로 저장해요.exportJson(), added by withExport(api), turns the visible rows (sort and filter applied) into a JSON array of objects keyed by column id. By default it writes the stored values; text: 'shown' writes the displayed text. scope picks all, visible or selected rows, columns picks the columns and their order, and pretty indents. downloadJson() saves a file.
코드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="value">저장된 값</button>
<button type="button" id="shown">화면 글자</button>
<button type="button" id="selected">선택한 행만</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('value').addEventListener('click', () => actions.value(api));
document.getElementById('shown').addEventListener('click', () => actions.shown(api));
document.getElementById('selected').addEventListener('click', () => actions.selected(api));import { withExport } from '@maxgrid/export';
const rows = [
{ id: 1, name: '김하늘', dept: 'dev', joined: '2024-03-02', active: true, tags: ['a', 'b'] },
{ id: 2, name: '이도윤 <팀장>', dept: 'ops', joined: '2023-11-20', active: false, tags: [] },
{ id: 3, name: '박서연 & 최지우', dept: 'dev', joined: '2025-01-15', active: true, tags: ['b'] },
];
export const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 150 },
{
id: 'dept',
title: '부서',
kind: 'select',
width: 110,
editorOptions: [
{ value: 'dev', label: '개발' },
{ value: 'ops', label: '운영' },
],
optionLabel: (v) => (v === 'dev' ? '개발' : '운영'),
},
{ id: 'joined', title: '입사일', kind: 'date', width: 110 },
{ id: 'active', title: '재직', kind: 'boolean', width: 70 },
{
id: 'tags',
title: '태그',
width: 110,
cellEditor: 'multiselect',
editorOptions: [
{ value: 'a', label: '알파' },
{ value: 'b', label: '베타' },
],
},
],
rows,
rowKey: 'id',
selection: 'multiple',
};
let say = () => { };
let ex;
export function setup(api, log) {
say = log;
ex = withExport(api);
log({
ko: 'withExport로 붙인 api.exportJson()은 보이는 행을 JSON 글자로 만들어요. 버튼을 눌러 보세요.',
en: 'api.exportJson(), attached by withExport, turns the visible rows into JSON text. Press a button.',
});
}
/** 기록은 위가 최신이라 거꾸로 적어야 위에서 아래로 읽혀요. */
function show(text) {
for (const line of text.split('\n').reverse())
say(line);
}
export const actions = {
value: () => {
show(ex?.exportJson({ pretty: true, space: 2 }) ?? '');
say({
ko: '저장된 값 그대로예요(부서는 dev, 태그는 배열).',
en: 'The stored values as they are (dept is dev, tags is an array).',
});
},
shown: () => {
show(ex?.exportJson({ text: 'shown', pretty: true }) ?? '');
say({
ko: "text: 'shown'은 화면 글자예요(부서는 개발).",
en: "text: 'shown' gives the displayed text (dept is shown as its label).",
});
},
selected: (api) => {
api.selectRows([1, 3]);
show(ex?.exportJson({ scope: 'selected', columns: ['id', 'name'] }) ?? '');
say({
ko: '선택한 행(1, 3)의 번호·이름만이에요.',
en: 'Only id and name of the selected rows (1 and 3).',
});
},
}; 이 HTML은 추가 번들(위 script 태그)도 함께 불러오고 전역 변수로 써요. 별도 설치는 필요 없어요.
<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>보이는 행을 JSON으로 내보내기</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="value">저장된 값</button>
<button type="button" id="shown">화면 글자</button>
<button type="button" id="selected">선택한 행만</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 { withExport } = MaxGridExport;
const rows = [
{ id: 1, name: '김하늘', dept: 'dev', joined: '2024-03-02', active: true, tags: ['a', 'b'] },
{ id: 2, name: '이도윤 <팀장>', dept: 'ops', joined: '2023-11-20', active: false, tags: [] },
{ id: 3, name: '박서연 & 최지우', dept: 'dev', joined: '2025-01-15', active: true, tags: ['b'] },
];
const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 150 },
{
id: 'dept',
title: '부서',
kind: 'select',
width: 110,
editorOptions: [
{ value: 'dev', label: '개발' },
{ value: 'ops', label: '운영' },
],
optionLabel: (v) => (v === 'dev' ? '개발' : '운영'),
},
{ id: 'joined', title: '입사일', kind: 'date', width: 110 },
{ id: 'active', title: '재직', kind: 'boolean', width: 70 },
{
id: 'tags',
title: '태그',
width: 110,
cellEditor: 'multiselect',
editorOptions: [
{ value: 'a', label: '알파' },
{ value: 'b', label: '베타' },
],
},
],
rows,
rowKey: 'id',
selection: 'multiple',
};
let say = () => { };
let ex;
function setup(api, log) {
say = log;
ex = withExport(api);
log({
ko: 'withExport로 붙인 api.exportJson()은 보이는 행을 JSON 글자로 만들어요. 버튼을 눌러 보세요.',
en: 'api.exportJson(), attached by withExport, turns the visible rows into JSON text. Press a button.',
});
}
/** 기록은 위가 최신이라 거꾸로 적어야 위에서 아래로 읽혀요. */
function show(text) {
for (const line of text.split('\n').reverse())
say(line);
}
const actions = {
value: () => {
show(ex?.exportJson({ pretty: true, space: 2 }) ?? '');
say({
ko: '저장된 값 그대로예요(부서는 dev, 태그는 배열).',
en: 'The stored values as they are (dept is dev, tags is an array).',
});
},
shown: () => {
show(ex?.exportJson({ text: 'shown', pretty: true }) ?? '');
say({
ko: "text: 'shown'은 화면 글자예요(부서는 개발).",
en: "text: 'shown' gives the displayed text (dept is shown as its label).",
});
},
selected: (api) => {
api.selectRows([1, 3]);
show(ex?.exportJson({ scope: 'selected', columns: ['id', 'name'] }) ?? '');
say({
ko: '선택한 행(1, 3)의 번호·이름만이에요.',
en: 'Only id and name of the selected rows (1 and 3).',
});
},
};
options.ariaLabel = "보이는 행을 JSON으로 내보내기";
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('value').addEventListener('click', () => actions.value(api));
document.getElementById('shown').addEventListener('click', () => actions.shown(api));
document.getElementById('selected').addEventListener('click', () => actions.selected(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.value(api)}
>저장된 값</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.shown(api)}
>화면 글자</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.selected(api)}
>선택한 행만</button>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import { withExport, type ExportableGridApi } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
dept: string;
joined: string;
active: boolean;
tags: string[];
}
const rows: Row[] = [
{ id: 1, name: '김하늘', dept: 'dev', joined: '2024-03-02', active: true, tags: ['a', 'b'] },
{ id: 2, name: '이도윤 <팀장>', dept: 'ops', joined: '2023-11-20', active: false, tags: [] },
{ id: 3, name: '박서연 & 최지우', dept: 'dev', joined: '2025-01-15', active: true, tags: ['b'] },
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 150 },
{
id: 'dept',
title: '부서',
kind: 'select',
width: 110,
editorOptions: [
{ value: 'dev', label: '개발' },
{ value: 'ops', label: '운영' },
],
optionLabel: (v) => (v === 'dev' ? '개발' : '운영'),
},
{ id: 'joined', title: '입사일', kind: 'date', width: 110 },
{ id: 'active', title: '재직', kind: 'boolean', width: 70 },
{
id: 'tags',
title: '태그',
width: 110,
cellEditor: 'multiselect',
editorOptions: [
{ value: 'a', label: '알파' },
{ value: 'b', label: '베타' },
],
},
],
rows,
rowKey: 'id',
selection: 'multiple',
};
let say: (message: LogText) => void = () => {};
let ex: ExportableGridApi<Row> | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
ex = withExport(api);
log({
ko: 'withExport로 붙인 api.exportJson()은 보이는 행을 JSON 글자로 만들어요. 버튼을 눌러 보세요.',
en: 'api.exportJson(), attached by withExport, turns the visible rows into JSON text. Press a button.',
});
}
/** 기록은 위가 최신이라 거꾸로 적어야 위에서 아래로 읽혀요. */
function show(text: string): void {
for (const line of text.split('\n').reverse()) say(line);
}
export const actions = {
value: () => {
show(ex?.exportJson({ pretty: true, space: 2 }) ?? '');
say({
ko: '저장된 값 그대로예요(부서는 dev, 태그는 배열).',
en: 'The stored values as they are (dept is dev, tags is an array).',
});
},
shown: () => {
show(ex?.exportJson({ text: 'shown', pretty: true }) ?? '');
say({
ko: "text: 'shown'은 화면 글자예요(부서는 개발).",
en: "text: 'shown' gives the displayed text (dept is shown as its label).",
});
},
selected: (api: GridApi<Row>) => {
api.selectRows([1, 3]);
show(ex?.exportJson({ scope: 'selected', columns: ['id', 'name'] }) ?? '');
say({
ko: '선택한 행(1, 3)의 번호·이름만이에요.',
en: 'Only id and name of the selected rows (1 and 3).',
});
},
}; 이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 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.value(api)">저장된 값</button>
<button type="button" :disabled="!api" @click="api && actions.shown(api)">화면 글자</button>
<button
type="button"
:disabled="!api"
@click="api && actions.selected(api)"
>선택한 행만</button>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import { withExport, type ExportableGridApi } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
dept: string;
joined: string;
active: boolean;
tags: string[];
}
const rows: Row[] = [
{ id: 1, name: '김하늘', dept: 'dev', joined: '2024-03-02', active: true, tags: ['a', 'b'] },
{ id: 2, name: '이도윤 <팀장>', dept: 'ops', joined: '2023-11-20', active: false, tags: [] },
{ id: 3, name: '박서연 & 최지우', dept: 'dev', joined: '2025-01-15', active: true, tags: ['b'] },
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 150 },
{
id: 'dept',
title: '부서',
kind: 'select',
width: 110,
editorOptions: [
{ value: 'dev', label: '개발' },
{ value: 'ops', label: '운영' },
],
optionLabel: (v) => (v === 'dev' ? '개발' : '운영'),
},
{ id: 'joined', title: '입사일', kind: 'date', width: 110 },
{ id: 'active', title: '재직', kind: 'boolean', width: 70 },
{
id: 'tags',
title: '태그',
width: 110,
cellEditor: 'multiselect',
editorOptions: [
{ value: 'a', label: '알파' },
{ value: 'b', label: '베타' },
],
},
],
rows,
rowKey: 'id',
selection: 'multiple',
};
let say: (message: LogText) => void = () => {};
let ex: ExportableGridApi<Row> | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
ex = withExport(api);
log({
ko: 'withExport로 붙인 api.exportJson()은 보이는 행을 JSON 글자로 만들어요. 버튼을 눌러 보세요.',
en: 'api.exportJson(), attached by withExport, turns the visible rows into JSON text. Press a button.',
});
}
/** 기록은 위가 최신이라 거꾸로 적어야 위에서 아래로 읽혀요. */
function show(text: string): void {
for (const line of text.split('\n').reverse()) say(line);
}
export const actions = {
value: () => {
show(ex?.exportJson({ pretty: true, space: 2 }) ?? '');
say({
ko: '저장된 값 그대로예요(부서는 dev, 태그는 배열).',
en: 'The stored values as they are (dept is dev, tags is an array).',
});
},
shown: () => {
show(ex?.exportJson({ text: 'shown', pretty: true }) ?? '');
say({
ko: "text: 'shown'은 화면 글자예요(부서는 개발).",
en: "text: 'shown' gives the displayed text (dept is shown as its label).",
});
},
selected: (api: GridApi<Row>) => {
api.selectRows([1, 3]);
show(ex?.exportJson({ scope: 'selected', columns: ['id', 'name'] }) ?? '');
say({
ko: '선택한 행(1, 3)의 번호·이름만이에요.',
en: 'Only id and name of the selected rows (1 and 3).',
});
},
}; 이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 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.value(api)"
>저장된 값</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.shown(api)"
>화면 글자</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.selected(api)"
>선택한 행만</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 { withExport, type ExportableGridApi } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
dept: string;
joined: string;
active: boolean;
tags: string[];
}
const rows: Row[] = [
{ id: 1, name: '김하늘', dept: 'dev', joined: '2024-03-02', active: true, tags: ['a', 'b'] },
{ id: 2, name: '이도윤 <팀장>', dept: 'ops', joined: '2023-11-20', active: false, tags: [] },
{ id: 3, name: '박서연 & 최지우', dept: 'dev', joined: '2025-01-15', active: true, tags: ['b'] },
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 150 },
{
id: 'dept',
title: '부서',
kind: 'select',
width: 110,
editorOptions: [
{ value: 'dev', label: '개발' },
{ value: 'ops', label: '운영' },
],
optionLabel: (v) => (v === 'dev' ? '개발' : '운영'),
},
{ id: 'joined', title: '입사일', kind: 'date', width: 110 },
{ id: 'active', title: '재직', kind: 'boolean', width: 70 },
{
id: 'tags',
title: '태그',
width: 110,
cellEditor: 'multiselect',
editorOptions: [
{ value: 'a', label: '알파' },
{ value: 'b', label: '베타' },
],
},
],
rows,
rowKey: 'id',
selection: 'multiple',
};
let say: (message: LogText) => void = () => {};
let ex: ExportableGridApi<Row> | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
ex = withExport(api);
log({
ko: 'withExport로 붙인 api.exportJson()은 보이는 행을 JSON 글자로 만들어요. 버튼을 눌러 보세요.',
en: 'api.exportJson(), attached by withExport, turns the visible rows into JSON text. Press a button.',
});
}
/** 기록은 위가 최신이라 거꾸로 적어야 위에서 아래로 읽혀요. */
function show(text: string): void {
for (const line of text.split('\n').reverse()) say(line);
}
export const actions = {
value: () => {
show(ex?.exportJson({ pretty: true, space: 2 }) ?? '');
say({
ko: '저장된 값 그대로예요(부서는 dev, 태그는 배열).',
en: 'The stored values as they are (dept is dev, tags is an array).',
});
},
shown: () => {
show(ex?.exportJson({ text: 'shown', pretty: true }) ?? '');
say({
ko: "text: 'shown'은 화면 글자예요(부서는 개발).",
en: "text: 'shown' gives the displayed text (dept is shown as its label).",
});
},
selected: (api: GridApi<Row>) => {
api.selectRows([1, 3]);
show(ex?.exportJson({ scope: 'selected', columns: ['id', 'name'] }) ?? '');
say({
ko: '선택한 행(1, 3)의 번호·이름만이에요.',
en: 'Only id and name of the selected rows (1 and 3).',
});
},
}; 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="value">Stored values</button>
<button type="button" id="shown">Displayed text</button>
<button type="button" id="selected">Selected rows only</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('value').addEventListener('click', () => actions.value(api));
document.getElementById('shown').addEventListener('click', () => actions.shown(api));
document.getElementById('selected').addEventListener('click', () => actions.selected(api));import { withExport } from '@maxgrid/export';
const rows = [
{ id: 1, name: 'Liam Carter', dept: 'dev', joined: '2024-03-02', active: true, tags: ['a', 'b'] },
{ id: 2, name: 'Noah Kim <Lead>', dept: 'ops', joined: '2023-11-20', active: false, tags: [] },
{
id: 3,
name: 'Ava Brooks & Mia Young',
dept: 'dev',
joined: '2025-01-15',
active: true,
tags: ['b'],
},
];
export const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 170 },
{
id: 'dept',
title: 'Department',
kind: 'select',
width: 130,
editorOptions: [
{ value: 'dev', label: 'Development' },
{ value: 'ops', label: 'Operations' },
],
optionLabel: (v) => (v === 'dev' ? 'Development' : 'Operations'),
},
{ id: 'joined', title: 'Joined', kind: 'date', width: 110 },
{ id: 'active', title: 'Active', kind: 'boolean', width: 70 },
{
id: 'tags',
title: 'Tags',
width: 110,
cellEditor: 'multiselect',
editorOptions: [
{ value: 'a', label: 'Alpha' },
{ value: 'b', label: 'Beta' },
],
},
],
rows,
rowKey: 'id',
selection: 'multiple',
};
let say = () => { };
let ex;
export function setup(api, log) {
say = log;
ex = withExport(api);
log('api.exportJson(), attached by withExport, turns the visible rows into JSON text. Press a button.');
}
/** The log shows the newest line on top, so write in reverse to read top to bottom. */
function show(text) {
for (const line of text.split('\n').reverse())
say(line);
}
export const actions = {
value: () => {
show(ex?.exportJson({ pretty: true, space: 2 }) ?? '');
say('The stored values as they are (dept is dev, tags is an array).');
},
shown: () => {
show(ex?.exportJson({ text: 'shown', pretty: true }) ?? '');
say("text: 'shown' gives the displayed text (dept is shown as its label).");
},
selected: (api) => {
api.selectRows([1, 3]);
show(ex?.exportJson({ scope: 'selected', columns: ['id', 'name'] }) ?? '');
say('Only id and name of the selected rows (1 and 3).');
},
};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 the visible rows as JSON</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="value">Stored values</button>
<button type="button" id="shown">Displayed text</button>
<button type="button" id="selected">Selected rows only</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 { withExport } = MaxGridExport;
const rows = [
{ id: 1, name: 'Liam Carter', dept: 'dev', joined: '2024-03-02', active: true, tags: ['a', 'b'] },
{ id: 2, name: 'Noah Kim <Lead>', dept: 'ops', joined: '2023-11-20', active: false, tags: [] },
{
id: 3,
name: 'Ava Brooks & Mia Young',
dept: 'dev',
joined: '2025-01-15',
active: true,
tags: ['b'],
},
];
const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 170 },
{
id: 'dept',
title: 'Department',
kind: 'select',
width: 130,
editorOptions: [
{ value: 'dev', label: 'Development' },
{ value: 'ops', label: 'Operations' },
],
optionLabel: (v) => (v === 'dev' ? 'Development' : 'Operations'),
},
{ id: 'joined', title: 'Joined', kind: 'date', width: 110 },
{ id: 'active', title: 'Active', kind: 'boolean', width: 70 },
{
id: 'tags',
title: 'Tags',
width: 110,
cellEditor: 'multiselect',
editorOptions: [
{ value: 'a', label: 'Alpha' },
{ value: 'b', label: 'Beta' },
],
},
],
rows,
rowKey: 'id',
selection: 'multiple',
};
let say = () => { };
let ex;
function setup(api, log) {
say = log;
ex = withExport(api);
log('api.exportJson(), attached by withExport, turns the visible rows into JSON text. Press a button.');
}
/** The log shows the newest line on top, so write in reverse to read top to bottom. */
function show(text) {
for (const line of text.split('\n').reverse())
say(line);
}
const actions = {
value: () => {
show(ex?.exportJson({ pretty: true, space: 2 }) ?? '');
say('The stored values as they are (dept is dev, tags is an array).');
},
shown: () => {
show(ex?.exportJson({ text: 'shown', pretty: true }) ?? '');
say("text: 'shown' gives the displayed text (dept is shown as its label).");
},
selected: (api) => {
api.selectRows([1, 3]);
show(ex?.exportJson({ scope: 'selected', columns: ['id', 'name'] }) ?? '');
say('Only id and name of the selected rows (1 and 3).');
},
};
options.ariaLabel = "Export the visible rows as JSON";
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('value').addEventListener('click', () => actions.value(api));
document.getElementById('shown').addEventListener('click', () => actions.shown(api));
document.getElementById('selected').addEventListener('click', () => actions.selected(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.value(api)}
>Stored values</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.shown(api)}
>Displayed text</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.selected(api)}
>Selected rows only</button>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import { withExport, type ExportableGridApi } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string;
joined: string;
active: boolean;
tags: string[];
}
const rows: Row[] = [
{ id: 1, name: 'Liam Carter', dept: 'dev', joined: '2024-03-02', active: true, tags: ['a', 'b'] },
{ id: 2, name: 'Noah Kim <Lead>', dept: 'ops', joined: '2023-11-20', active: false, tags: [] },
{
id: 3,
name: 'Ava Brooks & Mia Young',
dept: 'dev',
joined: '2025-01-15',
active: true,
tags: ['b'],
},
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 170 },
{
id: 'dept',
title: 'Department',
kind: 'select',
width: 130,
editorOptions: [
{ value: 'dev', label: 'Development' },
{ value: 'ops', label: 'Operations' },
],
optionLabel: (v) => (v === 'dev' ? 'Development' : 'Operations'),
},
{ id: 'joined', title: 'Joined', kind: 'date', width: 110 },
{ id: 'active', title: 'Active', kind: 'boolean', width: 70 },
{
id: 'tags',
title: 'Tags',
width: 110,
cellEditor: 'multiselect',
editorOptions: [
{ value: 'a', label: 'Alpha' },
{ value: 'b', label: 'Beta' },
],
},
],
rows,
rowKey: 'id',
selection: 'multiple',
};
let say: (message: LogText) => void = () => {};
let ex: ExportableGridApi<Row> | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
ex = withExport(api);
log(
'api.exportJson(), attached by withExport, turns the visible rows into JSON text. Press a button.',
);
}
/** The log shows the newest line on top, so write in reverse to read top to bottom. */
function show(text: string): void {
for (const line of text.split('\n').reverse()) say(line);
}
export const actions = {
value: () => {
show(ex?.exportJson({ pretty: true, space: 2 }) ?? '');
say('The stored values as they are (dept is dev, tags is an array).');
},
shown: () => {
show(ex?.exportJson({ text: 'shown', pretty: true }) ?? '');
say("text: 'shown' gives the displayed text (dept is shown as its label).");
},
selected: (api: GridApi<Row>) => {
api.selectRows([1, 3]);
show(ex?.exportJson({ scope: 'selected', columns: ['id', 'name'] }) ?? '');
say('Only id and name of the selected rows (1 and 3).');
},
}; 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.value(api)">Stored values</button>
<button type="button" :disabled="!api" @click="api && actions.shown(api)">Displayed text</button>
<button
type="button"
:disabled="!api"
@click="api && actions.selected(api)"
>Selected rows only</button>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import { withExport, type ExportableGridApi } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string;
joined: string;
active: boolean;
tags: string[];
}
const rows: Row[] = [
{ id: 1, name: 'Liam Carter', dept: 'dev', joined: '2024-03-02', active: true, tags: ['a', 'b'] },
{ id: 2, name: 'Noah Kim <Lead>', dept: 'ops', joined: '2023-11-20', active: false, tags: [] },
{
id: 3,
name: 'Ava Brooks & Mia Young',
dept: 'dev',
joined: '2025-01-15',
active: true,
tags: ['b'],
},
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 170 },
{
id: 'dept',
title: 'Department',
kind: 'select',
width: 130,
editorOptions: [
{ value: 'dev', label: 'Development' },
{ value: 'ops', label: 'Operations' },
],
optionLabel: (v) => (v === 'dev' ? 'Development' : 'Operations'),
},
{ id: 'joined', title: 'Joined', kind: 'date', width: 110 },
{ id: 'active', title: 'Active', kind: 'boolean', width: 70 },
{
id: 'tags',
title: 'Tags',
width: 110,
cellEditor: 'multiselect',
editorOptions: [
{ value: 'a', label: 'Alpha' },
{ value: 'b', label: 'Beta' },
],
},
],
rows,
rowKey: 'id',
selection: 'multiple',
};
let say: (message: LogText) => void = () => {};
let ex: ExportableGridApi<Row> | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
ex = withExport(api);
log(
'api.exportJson(), attached by withExport, turns the visible rows into JSON text. Press a button.',
);
}
/** The log shows the newest line on top, so write in reverse to read top to bottom. */
function show(text: string): void {
for (const line of text.split('\n').reverse()) say(line);
}
export const actions = {
value: () => {
show(ex?.exportJson({ pretty: true, space: 2 }) ?? '');
say('The stored values as they are (dept is dev, tags is an array).');
},
shown: () => {
show(ex?.exportJson({ text: 'shown', pretty: true }) ?? '');
say("text: 'shown' gives the displayed text (dept is shown as its label).");
},
selected: (api: GridApi<Row>) => {
api.selectRows([1, 3]);
show(ex?.exportJson({ scope: 'selected', columns: ['id', 'name'] }) ?? '');
say('Only id and name of the selected rows (1 and 3).');
},
}; 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.value(api)"
>Stored values</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.shown(api)"
>Displayed text</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.selected(api)"
>Selected rows only</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 { withExport, type ExportableGridApi } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string;
joined: string;
active: boolean;
tags: string[];
}
const rows: Row[] = [
{ id: 1, name: 'Liam Carter', dept: 'dev', joined: '2024-03-02', active: true, tags: ['a', 'b'] },
{ id: 2, name: 'Noah Kim <Lead>', dept: 'ops', joined: '2023-11-20', active: false, tags: [] },
{
id: 3,
name: 'Ava Brooks & Mia Young',
dept: 'dev',
joined: '2025-01-15',
active: true,
tags: ['b'],
},
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 170 },
{
id: 'dept',
title: 'Department',
kind: 'select',
width: 130,
editorOptions: [
{ value: 'dev', label: 'Development' },
{ value: 'ops', label: 'Operations' },
],
optionLabel: (v) => (v === 'dev' ? 'Development' : 'Operations'),
},
{ id: 'joined', title: 'Joined', kind: 'date', width: 110 },
{ id: 'active', title: 'Active', kind: 'boolean', width: 70 },
{
id: 'tags',
title: 'Tags',
width: 110,
cellEditor: 'multiselect',
editorOptions: [
{ value: 'a', label: 'Alpha' },
{ value: 'b', label: 'Beta' },
],
},
],
rows,
rowKey: 'id',
selection: 'multiple',
};
let say: (message: LogText) => void = () => {};
let ex: ExportableGridApi<Row> | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
ex = withExport(api);
log(
'api.exportJson(), attached by withExport, turns the visible rows into JSON text. Press a button.',
);
}
/** The log shows the newest line on top, so write in reverse to read top to bottom. */
function show(text: string): void {
for (const line of text.split('\n').reverse()) say(line);
}
export const actions = {
value: () => {
show(ex?.exportJson({ pretty: true, space: 2 }) ?? '');
say('The stored values as they are (dept is dev, tags is an array).');
},
shown: () => {
show(ex?.exportJson({ text: 'shown', pretty: true }) ?? '');
say("text: 'shown' gives the displayed text (dept is shown as its label).");
},
selected: (api: GridApi<Row>) => {
api.selectRows([1, 3]);
show(ex?.exportJson({ scope: 'selected', columns: ['id', 'name'] }) ?? '');
say('Only id and name of the selected rows (1 and 3).');
},
};