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

보이는 행을 XML로 내보내기Export the visible rows as XML

exportXml()은 UTF-8 선언 아래 뿌리 요소와 행 요소를 만들고 컬럼 하나를 요소 하나로 써요. 요소 이름은 컬럼 id이고 쓸 수 없는 이름이면 <cell id="…">예요. 배열은 <item>, 없는 값은 nil="true"이며 글자는 모두 이스케이프하고 쓸 수 없는 제어 문자는 빼요. root·rowTag·pretty로 모양을 바꿔요.exportXml() writes a root element and row elements under a UTF-8 declaration, one element per column. The element name is the column id, or <cell id="…"> when it cannot be a name. Arrays become <item>, missing values nil="true", all text is escaped and invalid control characters are dropped. root, rowTag and pretty change the shape.

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

코드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="xml">XML 보기</button>
    <button type="button" id="custom">요소 이름 바꾸기</button>
    <button type="button" id="roundtrip">다시 읽기</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('xml').addEventListener('click', () => actions.xml(api));
document.getElementById('custom').addEventListener('click', () => actions.custom(api));
document.getElementById('roundtrip').addEventListener('click', () => actions.roundtrip(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',
};
let say = () => { };
let ex;
export function setup(api, log) {
  say = log;
  ex = withExport(api);
  log({
    ko: 'api.exportXml()은 UTF-8 선언 아래 <rows><row>…</row></rows>를 만들어요. 글자는 모두 이스케이프돼요.',
    en: 'api.exportXml() writes <rows><row>…</row></rows> under a UTF-8 declaration. All text is escaped.',
  });
}
/** 기록은 위가 최신이라 거꾸로 적어야 위에서 아래로 읽혀요. */
function show(text) {
  for (const line of text.split('\n').reverse())
    say(line);
}
export const actions = {
  xml: () => {
    show(ex?.exportXml({ columns: ['id', 'name', 'tags'] }) ?? '');
    say({
      ko: '이름의 < & 는 &lt; &amp;로, 빈 태그는 <tags/>로 나와요.',
      en: 'The < and & in a name become &lt; and &amp;, and empty tags give <tags/>.',
    });
  },
  custom: () => {
    show(ex?.exportXml({ root: 'staff', rowTag: 'person', pretty: false, columns: ['id', 'name'] }) ??
      '');
    say({
      ko: 'root·rowTag로 요소 이름을 바꾸고 pretty: false로 한 줄이에요.',
      en: 'root and rowTag rename the elements and pretty: false gives one line.',
    });
  },
  roundtrip: (api) => {
    const text = ex?.exportXml() ?? '';
    const back = ex?.importXml(text);
    say({
      ko: `다시 읽으면 ${back?.rows.length}행, 건너뜀 ${back?.skipped}, 알림 ${back?.errors.length}건이에요. 내보낸 XML은 그대로 읽혀요.`,
      en: `Reading it back gives ${back?.rows.length} rows, ${back?.skipped} skipped and ${back?.errors.length} notices. The exported XML reads back as it is.`,
    });
    api.getRowCount();
  },
};

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="xml">Show XML</button>
    <button type="button" id="custom">Rename elements</button>
    <button type="button" id="roundtrip">Read it back</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('xml').addEventListener('click', () => actions.xml(api));
document.getElementById('custom').addEventListener('click', () => actions.custom(api));
document.getElementById('roundtrip').addEventListener('click', () => actions.roundtrip(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',
};
let say = () => { };
let ex;
export function setup(api, log) {
  say = log;
  ex = withExport(api);
  log('api.exportXml() writes <rows><row>…</row></rows> under a UTF-8 declaration. All text is escaped.');
}
/** 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 = {
  xml: () => {
    show(ex?.exportXml({ columns: ['id', 'name', 'tags'] }) ?? '');
    say('The < and & in a name become &lt; and &amp;, and empty tags give <tags/>.');
  },
  custom: () => {
    show(ex?.exportXml({ root: 'staff', rowTag: 'person', pretty: false, columns: ['id', 'name'] }) ??
      '');
    say('root and rowTag rename the elements and pretty: false gives one line.');
  },
  roundtrip: (api) => {
    const text = ex?.exportXml() ?? '';
    const back = ex?.importXml(text);
    say(`Reading it back gives ${back?.rows.length} rows, ${back?.skipped} skipped and ${back?.errors.length} notices. The exported XML reads back as it is.`);
    api.getRowCount();
  },
};