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

보이는 행을 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.

입문Beginner
직접 써 보기Live preview보이는 행을 JSON으로 내보내기Export the visible rows as JSON

코드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="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>
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('value').addEventListener('click', () => actions.value(api));
document.getElementById('shown').addEventListener('click', () => actions.shown(api));
document.getElementById('selected').addEventListener('click', () => actions.selected(api));
example.js
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).',
    });
  },
};

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="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>
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('value').addEventListener('click', () => actions.value(api));
document.getElementById('shown').addEventListener('click', () => actions.shown(api));
document.getElementById('selected').addEventListener('click', () => actions.selected(api));
example.js
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).');
  },
};