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.
코드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)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<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>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));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.' });
},
};<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>iframe 안에 그리드 그리기</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const rows = [
{ id: 1, item: '노트북', qty: 3 },
{ id: 2, item: '모니터', qty: 10 },
{ id: 3, item: '키보드', qty: 25 },
];
// 위쪽 그리드는 평범한 그리드예요. 버튼으로 만드는 두 번째 그리드는 iframe 안의 요소에 그려요.
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;
function setup(_api, log) {
say = log;
return () => remove();
}
function remove() {
inner?.destroy();
inner = null;
frame?.remove();
frame = null;
}
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.' });
},
};
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('create').addEventListener('click', () => actions.create(api));
document.getElementById('remove').addEventListener('click', () => actions.remove(api));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 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.create(api)}
>iframe에 그리드 만들기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.remove(api)}
>iframe 지우기</button>
</div>
<Grid style={{ height: 150 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
qty: number;
}
// 갤러리 틀이 불러 둔 전역 MaxGrid. 그리드를 iframe의 요소에 직접 만들 때 쓴다.
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
};
const rows: Row[] = [
{ id: 1, item: '노트북', qty: 3 },
{ id: 2, item: '모니터', qty: 10 },
{ id: 3, item: '키보드', qty: 25 },
];
// 위쪽 그리드는 평범한 그리드예요. 버튼으로 만드는 두 번째 그리드는 iframe 안의 요소에 그려요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: '품목', width: 130 },
{ id: 'qty', title: '수량', kind: 'number', width: 90 },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
let frame: HTMLIFrameElement | null = null;
let inner: GridApi<Row> | null = null;
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => remove();
}
function remove(): void {
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 as Document;
doc.body.style.margin = '0';
doc.body.innerHTML = '<div id="g" style="height:220px"></div>';
// document 옵션은 생략해도 컨테이너가 속한 문서를 쓴다. 여기서는 일부러 적어 보여 줘요.
inner = MaxGrid.createGrid(doc.getElementById('g') as HTMLElement, {
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.' });
},
};버튼을 누르면 이 예제의 코드가 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.create(api)">iframe에 그리드 만들기</button>
<button type="button" :disabled="!api" @click="api && actions.remove(api)">iframe 지우기</button>
</div>
<Grid style="height: 150px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
qty: number;
}
// 갤러리 틀이 불러 둔 전역 MaxGrid. 그리드를 iframe의 요소에 직접 만들 때 쓴다.
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
};
const rows: Row[] = [
{ id: 1, item: '노트북', qty: 3 },
{ id: 2, item: '모니터', qty: 10 },
{ id: 3, item: '키보드', qty: 25 },
];
// 위쪽 그리드는 평범한 그리드예요. 버튼으로 만드는 두 번째 그리드는 iframe 안의 요소에 그려요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: '품목', width: 130 },
{ id: 'qty', title: '수량', kind: 'number', width: 90 },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
let frame: HTMLIFrameElement | null = null;
let inner: GridApi<Row> | null = null;
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => remove();
}
function remove(): void {
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 as Document;
doc.body.style.margin = '0';
doc.body.innerHTML = '<div id="g" style="height:220px"></div>';
// document 옵션은 생략해도 컨테이너가 속한 문서를 쓴다. 여기서는 일부러 적어 보여 줘요.
inner = MaxGrid.createGrid(doc.getElementById('g') as HTMLElement, {
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.' });
},
};버튼을 누르면 이 예제의 코드가 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.create(api)"
>iframe에 그리드 만들기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.remove(api)"
>iframe 지우기</button>
</div>
<mx-grid style="height: 150px" [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 type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
qty: number;
}
// 갤러리 틀이 불러 둔 전역 MaxGrid. 그리드를 iframe의 요소에 직접 만들 때 쓴다.
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
};
const rows: Row[] = [
{ id: 1, item: '노트북', qty: 3 },
{ id: 2, item: '모니터', qty: 10 },
{ id: 3, item: '키보드', qty: 25 },
];
// 위쪽 그리드는 평범한 그리드예요. 버튼으로 만드는 두 번째 그리드는 iframe 안의 요소에 그려요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: '품목', width: 130 },
{ id: 'qty', title: '수량', kind: 'number', width: 90 },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
let frame: HTMLIFrameElement | null = null;
let inner: GridApi<Row> | null = null;
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => remove();
}
function remove(): void {
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 as Document;
doc.body.style.margin = '0';
doc.body.innerHTML = '<div id="g" style="height:220px"></div>';
// document 옵션은 생략해도 컨테이너가 속한 문서를 쓴다. 여기서는 일부러 적어 보여 줘요.
inner = MaxGrid.createGrid(doc.getElementById('g') as HTMLElement, {
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.' });
},
};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="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>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));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.');
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Render a grid inside an iframe</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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.
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;
function setup(_api, log) {
say = log;
return () => remove();
}
function remove() {
inner?.destroy();
inner = null;
frame?.remove();
frame = null;
}
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.');
},
};
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('create').addEventListener('click', () => actions.create(api));
document.getElementById('remove').addEventListener('click', () => actions.remove(api));
</script>
</body>
</html>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.create(api)}
>Create grid in iframe</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.remove(api)}
>Remove iframe</button>
</div>
<Grid style={{ height: 150 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
item: string;
qty: number;
}
// The global MaxGrid that the gallery frame has loaded. Use it to create a grid directly in an element of the iframe.
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
};
const rows: Row[] = [
{ 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: GridOptions<Row> = {
columns: [
{ id: 'item', title: 'Item', width: 130 },
{ id: 'qty', title: 'Qty', kind: 'number', width: 90 },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
let frame: HTMLIFrameElement | null = null;
let inner: GridApi<Row> | null = null;
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => remove();
}
function remove(): void {
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 as Document;
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') as HTMLElement, {
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.');
},
};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.create(api)">Create grid in iframe</button>
<button type="button" :disabled="!api" @click="api && actions.remove(api)">Remove iframe</button>
</div>
<Grid style="height: 150px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
item: string;
qty: number;
}
// The global MaxGrid that the gallery frame has loaded. Use it to create a grid directly in an element of the iframe.
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
};
const rows: Row[] = [
{ 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: GridOptions<Row> = {
columns: [
{ id: 'item', title: 'Item', width: 130 },
{ id: 'qty', title: 'Qty', kind: 'number', width: 90 },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
let frame: HTMLIFrameElement | null = null;
let inner: GridApi<Row> | null = null;
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => remove();
}
function remove(): void {
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 as Document;
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') as HTMLElement, {
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.');
},
};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.create(api)"
>Create grid in iframe</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.remove(api)"
>Remove iframe</button>
</div>
<mx-grid style="height: 150px" [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 type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
item: string;
qty: number;
}
// The global MaxGrid that the gallery frame has loaded. Use it to create a grid directly in an element of the iframe.
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
};
const rows: Row[] = [
{ 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: GridOptions<Row> = {
columns: [
{ id: 'item', title: 'Item', width: 130 },
{ id: 'qty', title: 'Qty', kind: 'number', width: 90 },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
let frame: HTMLIFrameElement | null = null;
let inner: GridApi<Row> | null = null;
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => remove();
}
function remove(): void {
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 as Document;
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') as HTMLElement, {
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.');
},
};