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

iframe 안에 그리드 그리기Render a grid inside an iframe

다른 문서(iframe)의 요소에 그리드를 만들어요. 스크립트는 부모 창에 있어도 돼요. 스타일은 그 문서의 head에 들어가고 편집기와 메뉴, 바깥 클릭도 그 문서에서 동작해요.Create a grid in an element of another document (an iframe). The script can stay in the parent window. Styles go into that document's head, and the editor, menus and outside clicks work inside it.

심화Advanced
직접 써 보기Live previewiframe 안에 그리드 그리기Render a grid inside an iframe

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

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

index.html
<div id="app">
  <div class="toolbar">
    <button type="button" id="create">iframe에 그리드 만들기</button>
    <button type="button" id="remove">iframe 지우기</button>
  </div>
  <div id="grid" style="height: 150px"></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('create').addEventListener('click', () => actions.create(api));
document.getElementById('remove').addEventListener('click', () => actions.remove(api));
example.js
const rows = [
  { id: 1, item: '노트북', qty: 3 },
  { id: 2, item: '모니터', qty: 10 },
  { id: 3, item: '키보드', qty: 25 },
];
// 위쪽 그리드는 평범한 그리드예요. 버튼으로 만드는 두 번째 그리드는 iframe 안의 요소에 그려요.
export const options = {
  columns: [
    { id: 'item', title: '품목', width: 130 },
    { id: 'qty', title: '수량', kind: 'number', width: 90 },
  ],
  rows,
  rowKey: 'id',
};
let say = () => undefined;
let frame = null;
let inner = null;
export function setup(_api, log) {
  say = log;
  return () => remove();
}
function remove() {
  inner?.destroy();
  inner = null;
  frame?.remove();
  frame = null;
}
export const actions = {
  create: () => {
    if (frame !== null)
      return;
    frame = document.createElement('iframe');
    frame.title = 'grid frame';
    frame.style.cssText = 'width:100%;height:230px;border:1px solid #c8ced6;margin-top:8px';
    document.body.append(frame);
    const doc = frame.contentDocument;
    doc.body.style.margin = '0';
    doc.body.innerHTML = '<div id="g" style="height:220px"></div>';
    // document 옵션은 생략해도 컨테이너가 속한 문서를 쓴다. 여기서는 일부러 적어 보여 줘요.
    inner = MaxGrid.createGrid(doc.getElementById('g'), {
      columns: [
        { id: 'item', title: '품목', width: 130, editable: true },
        { id: 'qty', title: '수량', kind: 'number', width: 90, editable: true },
      ],
      rows,
      rowKey: 'id',
      columnMenu: true,
      ariaLabel: 'iframe grid',
      document: doc,
    });
    inner.on('editCommitted', (e) => say(`iframe: ${e.columnId} = ${String(e.value)}`));
    say({
      ko: 'iframe 안에 그리드를 만들었어요. 스타일도 그 문서에 들어갔어요.',
      en: 'Created the grid inside the iframe. The styles went into that document too.',
    });
  },
  remove: () => {
    remove();
    say({ ko: 'iframe을 지웠어요.', en: 'Removed the iframe.' });
  },
};

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

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="create">Create grid in iframe</button>
    <button type="button" id="remove">Remove iframe</button>
  </div>
  <div id="grid" style="height: 150px"></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('create').addEventListener('click', () => actions.create(api));
document.getElementById('remove').addEventListener('click', () => actions.remove(api));
example.js
const rows = [
  { id: 1, item: 'Laptop', qty: 3 },
  { id: 2, item: 'Monitor', qty: 10 },
  { id: 3, item: 'Keyboard', qty: 25 },
];
// The top grid is an ordinary grid. The second grid made by the button is drawn in an element inside the iframe.
export const options = {
  columns: [
    { id: 'item', title: 'Item', width: 130 },
    { id: 'qty', title: 'Qty', kind: 'number', width: 90 },
  ],
  rows,
  rowKey: 'id',
};
let say = () => undefined;
let frame = null;
let inner = null;
export function setup(_api, log) {
  say = log;
  return () => remove();
}
function remove() {
  inner?.destroy();
  inner = null;
  frame?.remove();
  frame = null;
}
export const actions = {
  create: () => {
    if (frame !== null)
      return;
    frame = document.createElement('iframe');
    frame.title = 'grid frame';
    frame.style.cssText = 'width:100%;height:230px;border:1px solid #c8ced6;margin-top:8px';
    document.body.append(frame);
    const doc = frame.contentDocument;
    doc.body.style.margin = '0';
    doc.body.innerHTML = '<div id="g" style="height:220px"></div>';
    // The document option falls back to the document that holds the container, but it is written out here on purpose.
    inner = MaxGrid.createGrid(doc.getElementById('g'), {
      columns: [
        { id: 'item', title: 'Item', width: 130, editable: true },
        { id: 'qty', title: 'Qty', kind: 'number', width: 90, editable: true },
      ],
      rows,
      rowKey: 'id',
      columnMenu: true,
      ariaLabel: 'iframe grid',
      document: doc,
    });
    inner.on('editCommitted', (e) => say(`iframe: ${e.columnId} = ${String(e.value)}`));
    say('Created the grid inside the iframe. The styles went into that document too.');
  },
  remove: () => {
    remove();
    say('Removed the iframe.');
  },
};