파일을 끌어다 놓아 가져오기Import by dropping a file
fileDrop 옵션을 켜면 csv·xlsx·json·xml 파일을 그리드 위에 놓아 가져와요. 키보드는 openFileDialog를 써요.Turn on the fileDrop option and drop a csv, xlsx, json or xml file onto the grid. Keyboard users call openFileDialog.
코드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="csv">샘플 CSV 받기</button>
<button type="button" id="xlsx">샘플 xlsx 받기</button>
<button type="button" id="choose">파일 고르기</button>
</div>
<div id="grid" style="height: 320px"></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('csv').addEventListener('click', () => actions.csv(api));
document.getElementById('xlsx').addEventListener('click', () => actions.xlsx(api));
document.getElementById('choose').addEventListener('click', () => actions.choose(api));import { exportXlsx, withExport } from '@maxgrid/export';
// fileDrop: true 로 켜면 파일을 그리드 위에 끌어다 놓아서 가져와요(csv·xlsx·json·xml). withExport(api)가 필요해요.
export const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 150 },
{ id: 'dept', title: '부서', width: 100 },
{ id: 'salary', title: '급여', kind: 'number', width: 100 },
],
rows: [{ id: 0, name: '(예전 행)', dept: '-', salary: 0 }],
rowKey: 'id',
fileDrop: true,
};
const SAMPLE = [
{ id: 1, name: '김하늘', dept: '개발', salary: 3200 },
{ id: 2, name: '이도윤', dept: '운영', salary: 3550 },
{ id: 3, name: '박서연', dept: '디자인', salary: 3900 },
];
function save(name, data, type) {
const a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([data], { type }));
a.download = name;
a.click();
setTimeout(() => URL.revokeObjectURL(a.href), 1000);
}
export function setup(api, log) {
withExport(api);
api.on('fileImported', (e) => log({
ko: `${e.name}: ${e.rowCount}행(${e.type})${e.warnings.length ? `, 알림 ${e.warnings.length}개` : ''}`,
en: `${e.name}: ${e.rowCount} rows (${e.type})${e.warnings.length ? `, ${e.warnings.length} warnings` : ''}`,
}));
api.on('error', (e) => log({ ko: `오류: ${e.message}`, en: `Error: ${e.message}` }));
log({
ko: '샘플 파일을 내려받아 그리드 위로 끌어다 놓아 보세요. 키보드로는 "파일 고르기"를 쓰세요.',
en: 'Download a sample file and drag it onto the grid. With the keyboard, use "Choose a file".',
});
}
export const actions = {
csv: () => save('people.csv', `번호,이름,부서,급여\r\n${SAMPLE.map((r) => `${r.id},${r.name},${r.dept},${r.salary}`).join('\r\n')}\r\n`, 'text/csv'),
xlsx: () => save('people.xlsx', exportXlsx({
columns: [
{ id: 'id', title: '번호', kind: 'number' },
{ id: 'name', title: '이름' },
{ id: 'dept', title: '부서' },
{ id: 'salary', title: '급여', kind: 'number' },
],
rows: SAMPLE,
}), 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
choose: (api) => api.openFileDialog(),
}; 이 HTML은 추가 번들(위 script 태그)도 함께 불러오고 전역 변수로 써요. 별도 설치는 필요 없어요.
<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>파일을 끌어다 놓아 가져오기</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="csv">샘플 CSV 받기</button>
<button type="button" id="xlsx">샘플 xlsx 받기</button>
<button type="button" id="choose">파일 고르기</button>
</div>
<div id="grid" style="height: 320px"></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 { exportXlsx, withExport } = MaxGridExport;
// fileDrop: true 로 켜면 파일을 그리드 위에 끌어다 놓아서 가져와요(csv·xlsx·json·xml). withExport(api)가 필요해요.
const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 150 },
{ id: 'dept', title: '부서', width: 100 },
{ id: 'salary', title: '급여', kind: 'number', width: 100 },
],
rows: [{ id: 0, name: '(예전 행)', dept: '-', salary: 0 }],
rowKey: 'id',
fileDrop: true,
};
const SAMPLE = [
{ id: 1, name: '김하늘', dept: '개발', salary: 3200 },
{ id: 2, name: '이도윤', dept: '운영', salary: 3550 },
{ id: 3, name: '박서연', dept: '디자인', salary: 3900 },
];
function save(name, data, type) {
const a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([data], { type }));
a.download = name;
a.click();
setTimeout(() => URL.revokeObjectURL(a.href), 1000);
}
function setup(api, log) {
withExport(api);
api.on('fileImported', (e) => log({
ko: `${e.name}: ${e.rowCount}행(${e.type})${e.warnings.length ? `, 알림 ${e.warnings.length}개` : ''}`,
en: `${e.name}: ${e.rowCount} rows (${e.type})${e.warnings.length ? `, ${e.warnings.length} warnings` : ''}`,
}));
api.on('error', (e) => log({ ko: `오류: ${e.message}`, en: `Error: ${e.message}` }));
log({
ko: '샘플 파일을 내려받아 그리드 위로 끌어다 놓아 보세요. 키보드로는 "파일 고르기"를 쓰세요.',
en: 'Download a sample file and drag it onto the grid. With the keyboard, use "Choose a file".',
});
}
const actions = {
csv: () => save('people.csv', `번호,이름,부서,급여\r\n${SAMPLE.map((r) => `${r.id},${r.name},${r.dept},${r.salary}`).join('\r\n')}\r\n`, 'text/csv'),
xlsx: () => save('people.xlsx', exportXlsx({
columns: [
{ id: 'id', title: '번호', kind: 'number' },
{ id: 'name', title: '이름' },
{ id: 'dept', title: '부서' },
{ id: 'salary', title: '급여', kind: 'number' },
],
rows: SAMPLE,
}), 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
choose: (api) => api.openFileDialog(),
};
options.ariaLabel = "파일을 끌어다 놓아 가져오기";
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('csv').addEventListener('click', () => actions.csv(api));
document.getElementById('xlsx').addEventListener('click', () => actions.xlsx(api));
document.getElementById('choose').addEventListener('click', () => actions.choose(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.csv(api)}
>샘플 CSV 받기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.xlsx(api)}
>샘플 xlsx 받기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.choose(api)}
>파일 고르기</button>
</div>
<Grid style={{ height: 320 }} ref={gridRef} options={options} />
</>
);
}import { exportXlsx, withExport } 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;
salary: number;
}
// fileDrop: true 로 켜면 파일을 그리드 위에 끌어다 놓아서 가져와요(csv·xlsx·json·xml). withExport(api)가 필요해요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 150 },
{ id: 'dept', title: '부서', width: 100 },
{ id: 'salary', title: '급여', kind: 'number', width: 100 },
],
rows: [{ id: 0, name: '(예전 행)', dept: '-', salary: 0 }],
rowKey: 'id',
fileDrop: true,
};
const SAMPLE = [
{ id: 1, name: '김하늘', dept: '개발', salary: 3200 },
{ id: 2, name: '이도윤', dept: '운영', salary: 3550 },
{ id: 3, name: '박서연', dept: '디자인', salary: 3900 },
];
function save(name: string, data: BlobPart, type: string): void {
const a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([data], { type }));
a.download = name;
a.click();
setTimeout(() => URL.revokeObjectURL(a.href), 1000);
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
withExport(api);
api.on('fileImported', (e) =>
log({
ko: `${e.name}: ${e.rowCount}행(${e.type})${e.warnings.length ? `, 알림 ${e.warnings.length}개` : ''}`,
en: `${e.name}: ${e.rowCount} rows (${e.type})${e.warnings.length ? `, ${e.warnings.length} warnings` : ''}`,
}),
);
api.on('error', (e) => log({ ko: `오류: ${e.message}`, en: `Error: ${e.message}` }));
log({
ko: '샘플 파일을 내려받아 그리드 위로 끌어다 놓아 보세요. 키보드로는 "파일 고르기"를 쓰세요.',
en: 'Download a sample file and drag it onto the grid. With the keyboard, use "Choose a file".',
});
}
export const actions = {
csv: () =>
save(
'people.csv',
`번호,이름,부서,급여\r\n${SAMPLE.map((r) => `${r.id},${r.name},${r.dept},${r.salary}`).join('\r\n')}\r\n`,
'text/csv',
),
xlsx: () =>
save(
'people.xlsx',
exportXlsx({
columns: [
{ id: 'id', title: '번호', kind: 'number' },
{ id: 'name', title: '이름' },
{ id: 'dept', title: '부서' },
{ id: 'salary', title: '급여', kind: 'number' },
],
rows: SAMPLE,
}) as BlobPart,
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
),
choose: (api: GridApi<Row>) => api.openFileDialog(),
}; 이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 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.csv(api)">샘플 CSV 받기</button>
<button type="button" :disabled="!api" @click="api && actions.xlsx(api)">샘플 xlsx 받기</button>
<button type="button" :disabled="!api" @click="api && actions.choose(api)">파일 고르기</button>
</div>
<Grid style="height: 320px" :options="options" @ready="onReady" />
</template>import { exportXlsx, withExport } 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;
salary: number;
}
// fileDrop: true 로 켜면 파일을 그리드 위에 끌어다 놓아서 가져와요(csv·xlsx·json·xml). withExport(api)가 필요해요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 150 },
{ id: 'dept', title: '부서', width: 100 },
{ id: 'salary', title: '급여', kind: 'number', width: 100 },
],
rows: [{ id: 0, name: '(예전 행)', dept: '-', salary: 0 }],
rowKey: 'id',
fileDrop: true,
};
const SAMPLE = [
{ id: 1, name: '김하늘', dept: '개발', salary: 3200 },
{ id: 2, name: '이도윤', dept: '운영', salary: 3550 },
{ id: 3, name: '박서연', dept: '디자인', salary: 3900 },
];
function save(name: string, data: BlobPart, type: string): void {
const a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([data], { type }));
a.download = name;
a.click();
setTimeout(() => URL.revokeObjectURL(a.href), 1000);
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
withExport(api);
api.on('fileImported', (e) =>
log({
ko: `${e.name}: ${e.rowCount}행(${e.type})${e.warnings.length ? `, 알림 ${e.warnings.length}개` : ''}`,
en: `${e.name}: ${e.rowCount} rows (${e.type})${e.warnings.length ? `, ${e.warnings.length} warnings` : ''}`,
}),
);
api.on('error', (e) => log({ ko: `오류: ${e.message}`, en: `Error: ${e.message}` }));
log({
ko: '샘플 파일을 내려받아 그리드 위로 끌어다 놓아 보세요. 키보드로는 "파일 고르기"를 쓰세요.',
en: 'Download a sample file and drag it onto the grid. With the keyboard, use "Choose a file".',
});
}
export const actions = {
csv: () =>
save(
'people.csv',
`번호,이름,부서,급여\r\n${SAMPLE.map((r) => `${r.id},${r.name},${r.dept},${r.salary}`).join('\r\n')}\r\n`,
'text/csv',
),
xlsx: () =>
save(
'people.xlsx',
exportXlsx({
columns: [
{ id: 'id', title: '번호', kind: 'number' },
{ id: 'name', title: '이름' },
{ id: 'dept', title: '부서' },
{ id: 'salary', title: '급여', kind: 'number' },
],
rows: SAMPLE,
}) as BlobPart,
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
),
choose: (api: GridApi<Row>) => api.openFileDialog(),
}; 이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 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.csv(api)"
>샘플 CSV 받기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.xlsx(api)"
>샘플 xlsx 받기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.choose(api)"
>파일 고르기</button>
</div>
<mx-grid style="height: 320px" [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 { exportXlsx, withExport } 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;
salary: number;
}
// fileDrop: true 로 켜면 파일을 그리드 위에 끌어다 놓아서 가져와요(csv·xlsx·json·xml). withExport(api)가 필요해요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 150 },
{ id: 'dept', title: '부서', width: 100 },
{ id: 'salary', title: '급여', kind: 'number', width: 100 },
],
rows: [{ id: 0, name: '(예전 행)', dept: '-', salary: 0 }],
rowKey: 'id',
fileDrop: true,
};
const SAMPLE = [
{ id: 1, name: '김하늘', dept: '개발', salary: 3200 },
{ id: 2, name: '이도윤', dept: '운영', salary: 3550 },
{ id: 3, name: '박서연', dept: '디자인', salary: 3900 },
];
function save(name: string, data: BlobPart, type: string): void {
const a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([data], { type }));
a.download = name;
a.click();
setTimeout(() => URL.revokeObjectURL(a.href), 1000);
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
withExport(api);
api.on('fileImported', (e) =>
log({
ko: `${e.name}: ${e.rowCount}행(${e.type})${e.warnings.length ? `, 알림 ${e.warnings.length}개` : ''}`,
en: `${e.name}: ${e.rowCount} rows (${e.type})${e.warnings.length ? `, ${e.warnings.length} warnings` : ''}`,
}),
);
api.on('error', (e) => log({ ko: `오류: ${e.message}`, en: `Error: ${e.message}` }));
log({
ko: '샘플 파일을 내려받아 그리드 위로 끌어다 놓아 보세요. 키보드로는 "파일 고르기"를 쓰세요.',
en: 'Download a sample file and drag it onto the grid. With the keyboard, use "Choose a file".',
});
}
export const actions = {
csv: () =>
save(
'people.csv',
`번호,이름,부서,급여\r\n${SAMPLE.map((r) => `${r.id},${r.name},${r.dept},${r.salary}`).join('\r\n')}\r\n`,
'text/csv',
),
xlsx: () =>
save(
'people.xlsx',
exportXlsx({
columns: [
{ id: 'id', title: '번호', kind: 'number' },
{ id: 'name', title: '이름' },
{ id: 'dept', title: '부서' },
{ id: 'salary', title: '급여', kind: 'number' },
],
rows: SAMPLE,
}) as BlobPart,
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
),
choose: (api: GridApi<Row>) => api.openFileDialog(),
}; 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="csv">Sample CSV</button>
<button type="button" id="xlsx">Sample xlsx</button>
<button type="button" id="choose">Choose a file</button>
</div>
<div id="grid" style="height: 320px"></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('csv').addEventListener('click', () => actions.csv(api));
document.getElementById('xlsx').addEventListener('click', () => actions.xlsx(api));
document.getElementById('choose').addEventListener('click', () => actions.choose(api));import { exportXlsx, withExport } from '@maxgrid/export';
// With fileDrop: true you drag a file onto the grid to import it (csv, xlsx, json, xml). It needs withExport(api).
export const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 150 },
{ id: 'dept', title: 'Dept', width: 100 },
{ id: 'salary', title: 'Salary', kind: 'number', width: 100 },
],
rows: [{ id: 0, name: '(old row)', dept: '-', salary: 0 }],
rowKey: 'id',
fileDrop: true,
};
const SAMPLE = [
{ id: 1, name: 'Liam Carter', dept: 'Dev', salary: 3200 },
{ id: 2, name: 'Emma Stone', dept: 'Ops', salary: 3550 },
{ id: 3, name: 'Noah Reed', dept: 'Design', salary: 3900 },
];
function save(name, data, type) {
const a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([data], { type }));
a.download = name;
a.click();
setTimeout(() => URL.revokeObjectURL(a.href), 1000);
}
export function setup(api, log) {
withExport(api);
api.on('fileImported', (e) => log(`${e.name}: ${e.rowCount} rows (${e.type})${e.warnings.length ? `, ${e.warnings.length} warnings` : ''}`));
api.on('error', (e) => log(`Error: ${e.message}`));
log('Download a sample file and drag it onto the grid. With the keyboard, use "Choose a file".');
}
export const actions = {
csv: () => save('people.csv', `No.,Name,Dept,Salary\r\n${SAMPLE.map((r) => `${r.id},${r.name},${r.dept},${r.salary}`).join('\r\n')}\r\n`, 'text/csv'),
xlsx: () => save('people.xlsx', exportXlsx({
columns: [
{ id: 'id', title: 'No.', kind: 'number' },
{ id: 'name', title: 'Name' },
{ id: 'dept', title: 'Dept' },
{ id: 'salary', title: 'Salary', kind: 'number' },
],
rows: SAMPLE,
}), 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
choose: (api) => api.openFileDialog(),
};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>Import by dropping a file</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="csv">Sample CSV</button>
<button type="button" id="xlsx">Sample xlsx</button>
<button type="button" id="choose">Choose a file</button>
</div>
<div id="grid" style="height: 320px"></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 { exportXlsx, withExport } = MaxGridExport;
// With fileDrop: true you drag a file onto the grid to import it (csv, xlsx, json, xml). It needs withExport(api).
const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 150 },
{ id: 'dept', title: 'Dept', width: 100 },
{ id: 'salary', title: 'Salary', kind: 'number', width: 100 },
],
rows: [{ id: 0, name: '(old row)', dept: '-', salary: 0 }],
rowKey: 'id',
fileDrop: true,
};
const SAMPLE = [
{ id: 1, name: 'Liam Carter', dept: 'Dev', salary: 3200 },
{ id: 2, name: 'Emma Stone', dept: 'Ops', salary: 3550 },
{ id: 3, name: 'Noah Reed', dept: 'Design', salary: 3900 },
];
function save(name, data, type) {
const a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([data], { type }));
a.download = name;
a.click();
setTimeout(() => URL.revokeObjectURL(a.href), 1000);
}
function setup(api, log) {
withExport(api);
api.on('fileImported', (e) => log(`${e.name}: ${e.rowCount} rows (${e.type})${e.warnings.length ? `, ${e.warnings.length} warnings` : ''}`));
api.on('error', (e) => log(`Error: ${e.message}`));
log('Download a sample file and drag it onto the grid. With the keyboard, use "Choose a file".');
}
const actions = {
csv: () => save('people.csv', `No.,Name,Dept,Salary\r\n${SAMPLE.map((r) => `${r.id},${r.name},${r.dept},${r.salary}`).join('\r\n')}\r\n`, 'text/csv'),
xlsx: () => save('people.xlsx', exportXlsx({
columns: [
{ id: 'id', title: 'No.', kind: 'number' },
{ id: 'name', title: 'Name' },
{ id: 'dept', title: 'Dept' },
{ id: 'salary', title: 'Salary', kind: 'number' },
],
rows: SAMPLE,
}), 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
choose: (api) => api.openFileDialog(),
};
options.ariaLabel = "Import by dropping a file";
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('csv').addEventListener('click', () => actions.csv(api));
document.getElementById('xlsx').addEventListener('click', () => actions.xlsx(api));
document.getElementById('choose').addEventListener('click', () => actions.choose(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.csv(api)}
>Sample CSV</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.xlsx(api)}
>Sample xlsx</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.choose(api)}
>Choose a file</button>
</div>
<Grid style={{ height: 320 }} ref={gridRef} options={options} />
</>
);
}import { exportXlsx, withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string;
salary: number;
}
// With fileDrop: true you drag a file onto the grid to import it (csv, xlsx, json, xml). It needs withExport(api).
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 150 },
{ id: 'dept', title: 'Dept', width: 100 },
{ id: 'salary', title: 'Salary', kind: 'number', width: 100 },
],
rows: [{ id: 0, name: '(old row)', dept: '-', salary: 0 }],
rowKey: 'id',
fileDrop: true,
};
const SAMPLE = [
{ id: 1, name: 'Liam Carter', dept: 'Dev', salary: 3200 },
{ id: 2, name: 'Emma Stone', dept: 'Ops', salary: 3550 },
{ id: 3, name: 'Noah Reed', dept: 'Design', salary: 3900 },
];
function save(name: string, data: BlobPart, type: string): void {
const a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([data], { type }));
a.download = name;
a.click();
setTimeout(() => URL.revokeObjectURL(a.href), 1000);
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
withExport(api);
api.on('fileImported', (e) =>
log(
`${e.name}: ${e.rowCount} rows (${e.type})${e.warnings.length ? `, ${e.warnings.length} warnings` : ''}`,
),
);
api.on('error', (e) => log(`Error: ${e.message}`));
log('Download a sample file and drag it onto the grid. With the keyboard, use "Choose a file".');
}
export const actions = {
csv: () =>
save(
'people.csv',
`No.,Name,Dept,Salary\r\n${SAMPLE.map((r) => `${r.id},${r.name},${r.dept},${r.salary}`).join('\r\n')}\r\n`,
'text/csv',
),
xlsx: () =>
save(
'people.xlsx',
exportXlsx({
columns: [
{ id: 'id', title: 'No.', kind: 'number' },
{ id: 'name', title: 'Name' },
{ id: 'dept', title: 'Dept' },
{ id: 'salary', title: 'Salary', kind: 'number' },
],
rows: SAMPLE,
}) as BlobPart,
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
),
choose: (api: GridApi<Row>) => api.openFileDialog(),
}; 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.csv(api)">Sample CSV</button>
<button type="button" :disabled="!api" @click="api && actions.xlsx(api)">Sample xlsx</button>
<button type="button" :disabled="!api" @click="api && actions.choose(api)">Choose a file</button>
</div>
<Grid style="height: 320px" :options="options" @ready="onReady" />
</template>import { exportXlsx, withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string;
salary: number;
}
// With fileDrop: true you drag a file onto the grid to import it (csv, xlsx, json, xml). It needs withExport(api).
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 150 },
{ id: 'dept', title: 'Dept', width: 100 },
{ id: 'salary', title: 'Salary', kind: 'number', width: 100 },
],
rows: [{ id: 0, name: '(old row)', dept: '-', salary: 0 }],
rowKey: 'id',
fileDrop: true,
};
const SAMPLE = [
{ id: 1, name: 'Liam Carter', dept: 'Dev', salary: 3200 },
{ id: 2, name: 'Emma Stone', dept: 'Ops', salary: 3550 },
{ id: 3, name: 'Noah Reed', dept: 'Design', salary: 3900 },
];
function save(name: string, data: BlobPart, type: string): void {
const a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([data], { type }));
a.download = name;
a.click();
setTimeout(() => URL.revokeObjectURL(a.href), 1000);
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
withExport(api);
api.on('fileImported', (e) =>
log(
`${e.name}: ${e.rowCount} rows (${e.type})${e.warnings.length ? `, ${e.warnings.length} warnings` : ''}`,
),
);
api.on('error', (e) => log(`Error: ${e.message}`));
log('Download a sample file and drag it onto the grid. With the keyboard, use "Choose a file".');
}
export const actions = {
csv: () =>
save(
'people.csv',
`No.,Name,Dept,Salary\r\n${SAMPLE.map((r) => `${r.id},${r.name},${r.dept},${r.salary}`).join('\r\n')}\r\n`,
'text/csv',
),
xlsx: () =>
save(
'people.xlsx',
exportXlsx({
columns: [
{ id: 'id', title: 'No.', kind: 'number' },
{ id: 'name', title: 'Name' },
{ id: 'dept', title: 'Dept' },
{ id: 'salary', title: 'Salary', kind: 'number' },
],
rows: SAMPLE,
}) as BlobPart,
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
),
choose: (api: GridApi<Row>) => api.openFileDialog(),
}; 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.csv(api)"
>Sample CSV</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.xlsx(api)"
>Sample xlsx</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.choose(api)"
>Choose a file</button>
</div>
<mx-grid style="height: 320px" [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 { exportXlsx, withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string;
salary: number;
}
// With fileDrop: true you drag a file onto the grid to import it (csv, xlsx, json, xml). It needs withExport(api).
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 150 },
{ id: 'dept', title: 'Dept', width: 100 },
{ id: 'salary', title: 'Salary', kind: 'number', width: 100 },
],
rows: [{ id: 0, name: '(old row)', dept: '-', salary: 0 }],
rowKey: 'id',
fileDrop: true,
};
const SAMPLE = [
{ id: 1, name: 'Liam Carter', dept: 'Dev', salary: 3200 },
{ id: 2, name: 'Emma Stone', dept: 'Ops', salary: 3550 },
{ id: 3, name: 'Noah Reed', dept: 'Design', salary: 3900 },
];
function save(name: string, data: BlobPart, type: string): void {
const a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([data], { type }));
a.download = name;
a.click();
setTimeout(() => URL.revokeObjectURL(a.href), 1000);
}
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
withExport(api);
api.on('fileImported', (e) =>
log(
`${e.name}: ${e.rowCount} rows (${e.type})${e.warnings.length ? `, ${e.warnings.length} warnings` : ''}`,
),
);
api.on('error', (e) => log(`Error: ${e.message}`));
log('Download a sample file and drag it onto the grid. With the keyboard, use "Choose a file".');
}
export const actions = {
csv: () =>
save(
'people.csv',
`No.,Name,Dept,Salary\r\n${SAMPLE.map((r) => `${r.id},${r.name},${r.dept},${r.salary}`).join('\r\n')}\r\n`,
'text/csv',
),
xlsx: () =>
save(
'people.xlsx',
exportXlsx({
columns: [
{ id: 'id', title: 'No.', kind: 'number' },
{ id: 'name', title: 'Name' },
{ id: 'dept', title: 'Dept' },
{ id: 'salary', title: 'Salary', kind: 'number' },
],
rows: SAMPLE,
}) as BlobPart,
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
),
choose: (api: GridApi<Row>) => api.openFileDialog(),
};