코드로 셀 값 쓰기Write cell values from code
setCellValue·fillColumn·updateCells로 셀 하나, 열 전체, 여러 줄의 여러 열을 코드에서 한 번에 써요. 한 번 부르면 되돌리기 한 단계이고, 값이 그대로면 아무 일도 하지 않아요.setCellValue, fillColumn and updateCells write one cell, a whole column or several columns on several rows from code. Each call is one undo step and does nothing when the values are already equal.
코드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="setOne">셀 하나 쓰기</button>
<button type="button" id="setInvalid">검증하며 쓰기</button>
<button type="button" id="fillPriority">열 채우기</button>
<button type="button" id="updateBlock">여러 줄 쓰기</button>
<button type="button" id="undo">되돌리기</button>
<button type="button" id="redo">다시 하기</button>
</div>
<div id="grid" style="height: 280px"></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('setOne').addEventListener('click', () => actions.setOne(api));
document.getElementById('setInvalid').addEventListener('click', () => actions.setInvalid(api));
document.getElementById('fillPriority').addEventListener('click', () => actions.fillPriority(api));
document.getElementById('updateBlock').addEventListener('click', () => actions.updateBlock(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
document.getElementById('redo').addEventListener('click', () => actions.redo(api));const tickets = [
'로그인 이동 오류',
'푸터 링크 정리',
'검색 결과가 느려요',
'가격표 오타',
'알림 설정 저장 안 됨',
];
export const options = {
columns: [
{ id: 'ticket', title: '티켓', width: 190 },
{ id: 'owner', title: '담당자', width: 100 },
{ id: 'priority', title: '우선순위', width: 90 },
{
id: 'hours',
title: '시간',
kind: 'number',
width: 80,
align: 'end',
validation: { min: 0, max: 40 },
},
],
rows: tickets.map((ticket, i) => ({
id: i + 1,
ticket,
owner: '',
priority: i % 2 ? 'high' : 'low',
hours: i,
})),
rowKey: 'id',
saveMode: 'batch',
};
let say = () => { };
export function setup(api, log) {
say = log;
const off = [
api.on('validationFailed', ({ message }) => log({
ko: `검증에 걸려 쓰지 않았어요: ${message}`,
en: `Rejected by validation: ${message}`,
})),
api.on('historyChanged', ({ action }) => action === 'undo' && log({ ko: '한 단계 되돌렸어요.', en: 'Undid one step.' })),
];
return () => off.forEach((fn) => fn());
}
// 셋 다 한 번 부르면 되돌리기 한 단계, rowsChanged 한 번이에요. 값이 그대로면 아무 일도 일어나지 않고 0/false를 돌려줘요.
export const actions = {
setOne: (api) => {
const done = api.setCellValue(2, 'owner', '김서연');
say({
ko: done ? '2번 줄 담당자를 "김서연"으로 썼어요.' : '값이 같아서 쓰지 않았어요.',
en: done ? 'Wrote owner "김서연" on row 2.' : 'Same value, nothing written.',
});
},
setInvalid: (api) => {
const done = api.setCellValue(3, 'hours', 99, { validate: true });
if (done)
say({ ko: '썼어요.', en: 'Written.' });
},
fillPriority: (api) => {
const n = api.fillColumn('priority', 'high');
say({
ko: `우선순위 열을 "high"로 채웠어요: ${n}줄 바뀜.`,
en: `Filled priority with "high": ${n} rows changed.`,
});
},
updateBlock: (api) => {
const n = api.updateCells([1, 2, 3], { owner: '박도윤', hours: 8 });
say({
ko: `1~3번 줄의 담당자·시간을 한 번에 썼어요: ${n}줄 바뀜.`,
en: `Wrote owner and hours on rows 1-3 at once: ${n} rows changed.`,
});
},
undo: (api) => {
if (!api.undo())
say({ ko: '되돌릴 단계가 없어요.', en: 'Nothing to undo.' });
},
redo: (api) => {
if (!api.redo())
say({ ko: '다시 할 단계가 없어요.', en: 'Nothing to redo.' });
},
};<!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="setOne">셀 하나 쓰기</button>
<button type="button" id="setInvalid">검증하며 쓰기</button>
<button type="button" id="fillPriority">열 채우기</button>
<button type="button" id="updateBlock">여러 줄 쓰기</button>
<button type="button" id="undo">되돌리기</button>
<button type="button" id="redo">다시 하기</button>
</div>
<div id="grid" style="height: 280px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const tickets = [
'로그인 이동 오류',
'푸터 링크 정리',
'검색 결과가 느려요',
'가격표 오타',
'알림 설정 저장 안 됨',
];
const options = {
columns: [
{ id: 'ticket', title: '티켓', width: 190 },
{ id: 'owner', title: '담당자', width: 100 },
{ id: 'priority', title: '우선순위', width: 90 },
{
id: 'hours',
title: '시간',
kind: 'number',
width: 80,
align: 'end',
validation: { min: 0, max: 40 },
},
],
rows: tickets.map((ticket, i) => ({
id: i + 1,
ticket,
owner: '',
priority: i % 2 ? 'high' : 'low',
hours: i,
})),
rowKey: 'id',
saveMode: 'batch',
};
let say = () => { };
function setup(api, log) {
say = log;
const off = [
api.on('validationFailed', ({ message }) => log({
ko: `검증에 걸려 쓰지 않았어요: ${message}`,
en: `Rejected by validation: ${message}`,
})),
api.on('historyChanged', ({ action }) => action === 'undo' && log({ ko: '한 단계 되돌렸어요.', en: 'Undid one step.' })),
];
return () => off.forEach((fn) => fn());
}
// 셋 다 한 번 부르면 되돌리기 한 단계, rowsChanged 한 번이에요. 값이 그대로면 아무 일도 일어나지 않고 0/false를 돌려줘요.
const actions = {
setOne: (api) => {
const done = api.setCellValue(2, 'owner', '김서연');
say({
ko: done ? '2번 줄 담당자를 "김서연"으로 썼어요.' : '값이 같아서 쓰지 않았어요.',
en: done ? 'Wrote owner "김서연" on row 2.' : 'Same value, nothing written.',
});
},
setInvalid: (api) => {
const done = api.setCellValue(3, 'hours', 99, { validate: true });
if (done)
say({ ko: '썼어요.', en: 'Written.' });
},
fillPriority: (api) => {
const n = api.fillColumn('priority', 'high');
say({
ko: `우선순위 열을 "high"로 채웠어요: ${n}줄 바뀜.`,
en: `Filled priority with "high": ${n} rows changed.`,
});
},
updateBlock: (api) => {
const n = api.updateCells([1, 2, 3], { owner: '박도윤', hours: 8 });
say({
ko: `1~3번 줄의 담당자·시간을 한 번에 썼어요: ${n}줄 바뀜.`,
en: `Wrote owner and hours on rows 1-3 at once: ${n} rows changed.`,
});
},
undo: (api) => {
if (!api.undo())
say({ ko: '되돌릴 단계가 없어요.', en: 'Nothing to undo.' });
},
redo: (api) => {
if (!api.redo())
say({ ko: '다시 할 단계가 없어요.', en: 'Nothing to redo.' });
},
};
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('setOne').addEventListener('click', () => actions.setOne(api));
document.getElementById('setInvalid').addEventListener('click', () => actions.setInvalid(api));
document.getElementById('fillPriority').addEventListener('click', () => actions.fillPriority(api));
document.getElementById('updateBlock').addEventListener('click', () => actions.updateBlock(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
document.getElementById('redo').addEventListener('click', () => actions.redo(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.setOne(api)}
>셀 하나 쓰기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.setInvalid(api)}
>검증하며 쓰기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.fillPriority(api)}
>열 채우기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.updateBlock(api)}
>여러 줄 쓰기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.undo(api)}
>되돌리기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.redo(api)}
>다시 하기</button>
</div>
<Grid style={{ height: 280 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
ticket: string;
owner: string;
priority: string;
hours: number;
}
const tickets = [
'로그인 이동 오류',
'푸터 링크 정리',
'검색 결과가 느려요',
'가격표 오타',
'알림 설정 저장 안 됨',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'ticket', title: '티켓', width: 190 },
{ id: 'owner', title: '담당자', width: 100 },
{ id: 'priority', title: '우선순위', width: 90 },
{
id: 'hours',
title: '시간',
kind: 'number',
width: 80,
align: 'end',
validation: { min: 0, max: 40 },
},
],
rows: tickets.map((ticket, i) => ({
id: i + 1,
ticket,
owner: '',
priority: i % 2 ? 'high' : 'low',
hours: i,
})),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = [
api.on('validationFailed', ({ message }) =>
log({
ko: `검증에 걸려 쓰지 않았어요: ${message}`,
en: `Rejected by validation: ${message}`,
}),
),
api.on(
'historyChanged',
({ action }) =>
action === 'undo' && log({ ko: '한 단계 되돌렸어요.', en: 'Undid one step.' }),
),
];
return () => off.forEach((fn) => fn());
}
// 셋 다 한 번 부르면 되돌리기 한 단계, rowsChanged 한 번이에요. 값이 그대로면 아무 일도 일어나지 않고 0/false를 돌려줘요.
export const actions = {
setOne: (api: GridApi<Row>) => {
const done = api.setCellValue(2, 'owner', '김서연');
say({
ko: done ? '2번 줄 담당자를 "김서연"으로 썼어요.' : '값이 같아서 쓰지 않았어요.',
en: done ? 'Wrote owner "김서연" on row 2.' : 'Same value, nothing written.',
});
},
setInvalid: (api: GridApi<Row>) => {
const done = api.setCellValue(3, 'hours', 99, { validate: true });
if (done) say({ ko: '썼어요.', en: 'Written.' });
},
fillPriority: (api: GridApi<Row>) => {
const n = api.fillColumn('priority', 'high');
say({
ko: `우선순위 열을 "high"로 채웠어요: ${n}줄 바뀜.`,
en: `Filled priority with "high": ${n} rows changed.`,
});
},
updateBlock: (api: GridApi<Row>) => {
const n = api.updateCells([1, 2, 3], { owner: '박도윤', hours: 8 });
say({
ko: `1~3번 줄의 담당자·시간을 한 번에 썼어요: ${n}줄 바뀜.`,
en: `Wrote owner and hours on rows 1-3 at once: ${n} rows changed.`,
});
},
undo: (api: GridApi<Row>) => {
if (!api.undo()) say({ ko: '되돌릴 단계가 없어요.', en: 'Nothing to undo.' });
},
redo: (api: GridApi<Row>) => {
if (!api.redo()) say({ ko: '다시 할 단계가 없어요.', en: 'Nothing to redo.' });
},
};버튼을 누르면 이 예제의 코드가 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.setOne(api)">셀 하나 쓰기</button>
<button
type="button"
:disabled="!api"
@click="api && actions.setInvalid(api)"
>검증하며 쓰기</button>
<button
type="button"
:disabled="!api"
@click="api && actions.fillPriority(api)"
>열 채우기</button>
<button
type="button"
:disabled="!api"
@click="api && actions.updateBlock(api)"
>여러 줄 쓰기</button>
<button type="button" :disabled="!api" @click="api && actions.undo(api)">되돌리기</button>
<button type="button" :disabled="!api" @click="api && actions.redo(api)">다시 하기</button>
</div>
<Grid style="height: 280px" :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;
ticket: string;
owner: string;
priority: string;
hours: number;
}
const tickets = [
'로그인 이동 오류',
'푸터 링크 정리',
'검색 결과가 느려요',
'가격표 오타',
'알림 설정 저장 안 됨',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'ticket', title: '티켓', width: 190 },
{ id: 'owner', title: '담당자', width: 100 },
{ id: 'priority', title: '우선순위', width: 90 },
{
id: 'hours',
title: '시간',
kind: 'number',
width: 80,
align: 'end',
validation: { min: 0, max: 40 },
},
],
rows: tickets.map((ticket, i) => ({
id: i + 1,
ticket,
owner: '',
priority: i % 2 ? 'high' : 'low',
hours: i,
})),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = [
api.on('validationFailed', ({ message }) =>
log({
ko: `검증에 걸려 쓰지 않았어요: ${message}`,
en: `Rejected by validation: ${message}`,
}),
),
api.on(
'historyChanged',
({ action }) =>
action === 'undo' && log({ ko: '한 단계 되돌렸어요.', en: 'Undid one step.' }),
),
];
return () => off.forEach((fn) => fn());
}
// 셋 다 한 번 부르면 되돌리기 한 단계, rowsChanged 한 번이에요. 값이 그대로면 아무 일도 일어나지 않고 0/false를 돌려줘요.
export const actions = {
setOne: (api: GridApi<Row>) => {
const done = api.setCellValue(2, 'owner', '김서연');
say({
ko: done ? '2번 줄 담당자를 "김서연"으로 썼어요.' : '값이 같아서 쓰지 않았어요.',
en: done ? 'Wrote owner "김서연" on row 2.' : 'Same value, nothing written.',
});
},
setInvalid: (api: GridApi<Row>) => {
const done = api.setCellValue(3, 'hours', 99, { validate: true });
if (done) say({ ko: '썼어요.', en: 'Written.' });
},
fillPriority: (api: GridApi<Row>) => {
const n = api.fillColumn('priority', 'high');
say({
ko: `우선순위 열을 "high"로 채웠어요: ${n}줄 바뀜.`,
en: `Filled priority with "high": ${n} rows changed.`,
});
},
updateBlock: (api: GridApi<Row>) => {
const n = api.updateCells([1, 2, 3], { owner: '박도윤', hours: 8 });
say({
ko: `1~3번 줄의 담당자·시간을 한 번에 썼어요: ${n}줄 바뀜.`,
en: `Wrote owner and hours on rows 1-3 at once: ${n} rows changed.`,
});
},
undo: (api: GridApi<Row>) => {
if (!api.undo()) say({ ko: '되돌릴 단계가 없어요.', en: 'Nothing to undo.' });
},
redo: (api: GridApi<Row>) => {
if (!api.redo()) say({ ko: '다시 할 단계가 없어요.', en: 'Nothing to redo.' });
},
};버튼을 누르면 이 예제의 코드가 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.setOne(api)"
>셀 하나 쓰기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.setInvalid(api)"
>검증하며 쓰기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.fillPriority(api)"
>열 채우기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.updateBlock(api)"
>여러 줄 쓰기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.undo(api)"
>되돌리기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.redo(api)"
>다시 하기</button>
</div>
<mx-grid style="height: 280px" [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;
ticket: string;
owner: string;
priority: string;
hours: number;
}
const tickets = [
'로그인 이동 오류',
'푸터 링크 정리',
'검색 결과가 느려요',
'가격표 오타',
'알림 설정 저장 안 됨',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'ticket', title: '티켓', width: 190 },
{ id: 'owner', title: '담당자', width: 100 },
{ id: 'priority', title: '우선순위', width: 90 },
{
id: 'hours',
title: '시간',
kind: 'number',
width: 80,
align: 'end',
validation: { min: 0, max: 40 },
},
],
rows: tickets.map((ticket, i) => ({
id: i + 1,
ticket,
owner: '',
priority: i % 2 ? 'high' : 'low',
hours: i,
})),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = [
api.on('validationFailed', ({ message }) =>
log({
ko: `검증에 걸려 쓰지 않았어요: ${message}`,
en: `Rejected by validation: ${message}`,
}),
),
api.on(
'historyChanged',
({ action }) =>
action === 'undo' && log({ ko: '한 단계 되돌렸어요.', en: 'Undid one step.' }),
),
];
return () => off.forEach((fn) => fn());
}
// 셋 다 한 번 부르면 되돌리기 한 단계, rowsChanged 한 번이에요. 값이 그대로면 아무 일도 일어나지 않고 0/false를 돌려줘요.
export const actions = {
setOne: (api: GridApi<Row>) => {
const done = api.setCellValue(2, 'owner', '김서연');
say({
ko: done ? '2번 줄 담당자를 "김서연"으로 썼어요.' : '값이 같아서 쓰지 않았어요.',
en: done ? 'Wrote owner "김서연" on row 2.' : 'Same value, nothing written.',
});
},
setInvalid: (api: GridApi<Row>) => {
const done = api.setCellValue(3, 'hours', 99, { validate: true });
if (done) say({ ko: '썼어요.', en: 'Written.' });
},
fillPriority: (api: GridApi<Row>) => {
const n = api.fillColumn('priority', 'high');
say({
ko: `우선순위 열을 "high"로 채웠어요: ${n}줄 바뀜.`,
en: `Filled priority with "high": ${n} rows changed.`,
});
},
updateBlock: (api: GridApi<Row>) => {
const n = api.updateCells([1, 2, 3], { owner: '박도윤', hours: 8 });
say({
ko: `1~3번 줄의 담당자·시간을 한 번에 썼어요: ${n}줄 바뀜.`,
en: `Wrote owner and hours on rows 1-3 at once: ${n} rows changed.`,
});
},
undo: (api: GridApi<Row>) => {
if (!api.undo()) say({ ko: '되돌릴 단계가 없어요.', en: 'Nothing to undo.' });
},
redo: (api: GridApi<Row>) => {
if (!api.redo()) say({ ko: '다시 할 단계가 없어요.', en: 'Nothing to redo.' });
},
};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="setOne">Write one cell</button>
<button type="button" id="setInvalid">Write with validation</button>
<button type="button" id="fillPriority">Fill column</button>
<button type="button" id="updateBlock">Write rows</button>
<button type="button" id="undo">Undo</button>
<button type="button" id="redo">Redo</button>
</div>
<div id="grid" style="height: 280px"></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('setOne').addEventListener('click', () => actions.setOne(api));
document.getElementById('setInvalid').addEventListener('click', () => actions.setInvalid(api));
document.getElementById('fillPriority').addEventListener('click', () => actions.fillPriority(api));
document.getElementById('updateBlock').addEventListener('click', () => actions.updateBlock(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
document.getElementById('redo').addEventListener('click', () => actions.redo(api));const tickets = [
'Login redirect error',
'Clean up footer links',
'Search results are slow',
'Typo in price table',
'Notification settings not saved',
];
export const options = {
columns: [
{ id: 'ticket', title: 'Ticket', width: 190 },
{ id: 'owner', title: 'Owner', width: 100 },
{ id: 'priority', title: 'Priority', width: 90 },
{
id: 'hours',
title: 'Hours',
kind: 'number',
width: 80,
align: 'end',
validation: { min: 0, max: 40 },
},
],
rows: tickets.map((ticket, i) => ({
id: i + 1,
ticket,
owner: '',
priority: i % 2 ? 'high' : 'low',
hours: i,
})),
rowKey: 'id',
saveMode: 'batch',
};
let say = () => { };
export function setup(api, log) {
say = log;
const off = [
api.on('validationFailed', ({ message }) => log(`Rejected by validation: ${message}`)),
api.on('historyChanged', ({ action }) => action === 'undo' && log('Undid one step.')),
];
return () => off.forEach((fn) => fn());
}
// Each call is one undo step and one rowsChanged. If the value is unchanged nothing happens and it returns 0/false.
export const actions = {
setOne: (api) => {
const done = api.setCellValue(2, 'owner', 'Emma Stone');
say(done ? 'Wrote owner "Emma Stone" on row 2.' : 'Same value, nothing written.');
},
setInvalid: (api) => {
const done = api.setCellValue(3, 'hours', 99, { validate: true });
if (done)
say('Written.');
},
fillPriority: (api) => {
const n = api.fillColumn('priority', 'high');
say(`Filled priority with "high": ${n} rows changed.`);
},
updateBlock: (api) => {
const n = api.updateCells([1, 2, 3], { owner: 'Noah Kim', hours: 8 });
say(`Wrote owner and hours on rows 1-3 at once: ${n} rows changed.`);
},
undo: (api) => {
if (!api.undo())
say('Nothing to undo.');
},
redo: (api) => {
if (!api.redo())
say('Nothing to redo.');
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Write cell values from code</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="setOne">Write one cell</button>
<button type="button" id="setInvalid">Write with validation</button>
<button type="button" id="fillPriority">Fill column</button>
<button type="button" id="updateBlock">Write rows</button>
<button type="button" id="undo">Undo</button>
<button type="button" id="redo">Redo</button>
</div>
<div id="grid" style="height: 280px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const tickets = [
'Login redirect error',
'Clean up footer links',
'Search results are slow',
'Typo in price table',
'Notification settings not saved',
];
const options = {
columns: [
{ id: 'ticket', title: 'Ticket', width: 190 },
{ id: 'owner', title: 'Owner', width: 100 },
{ id: 'priority', title: 'Priority', width: 90 },
{
id: 'hours',
title: 'Hours',
kind: 'number',
width: 80,
align: 'end',
validation: { min: 0, max: 40 },
},
],
rows: tickets.map((ticket, i) => ({
id: i + 1,
ticket,
owner: '',
priority: i % 2 ? 'high' : 'low',
hours: i,
})),
rowKey: 'id',
saveMode: 'batch',
};
let say = () => { };
function setup(api, log) {
say = log;
const off = [
api.on('validationFailed', ({ message }) => log(`Rejected by validation: ${message}`)),
api.on('historyChanged', ({ action }) => action === 'undo' && log('Undid one step.')),
];
return () => off.forEach((fn) => fn());
}
// Each call is one undo step and one rowsChanged. If the value is unchanged nothing happens and it returns 0/false.
const actions = {
setOne: (api) => {
const done = api.setCellValue(2, 'owner', 'Emma Stone');
say(done ? 'Wrote owner "Emma Stone" on row 2.' : 'Same value, nothing written.');
},
setInvalid: (api) => {
const done = api.setCellValue(3, 'hours', 99, { validate: true });
if (done)
say('Written.');
},
fillPriority: (api) => {
const n = api.fillColumn('priority', 'high');
say(`Filled priority with "high": ${n} rows changed.`);
},
updateBlock: (api) => {
const n = api.updateCells([1, 2, 3], { owner: 'Noah Kim', hours: 8 });
say(`Wrote owner and hours on rows 1-3 at once: ${n} rows changed.`);
},
undo: (api) => {
if (!api.undo())
say('Nothing to undo.');
},
redo: (api) => {
if (!api.redo())
say('Nothing to redo.');
},
};
options.ariaLabel = "Write cell values from code";
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('setOne').addEventListener('click', () => actions.setOne(api));
document.getElementById('setInvalid').addEventListener('click', () => actions.setInvalid(api));
document.getElementById('fillPriority').addEventListener('click', () => actions.fillPriority(api));
document.getElementById('updateBlock').addEventListener('click', () => actions.updateBlock(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
document.getElementById('redo').addEventListener('click', () => actions.redo(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.setOne(api)}
>Write one cell</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.setInvalid(api)}
>Write with validation</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.fillPriority(api)}
>Fill column</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.updateBlock(api)}
>Write rows</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.undo(api)}
>Undo</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.redo(api)}
>Redo</button>
</div>
<Grid style={{ height: 280 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
ticket: string;
owner: string;
priority: string;
hours: number;
}
const tickets = [
'Login redirect error',
'Clean up footer links',
'Search results are slow',
'Typo in price table',
'Notification settings not saved',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'ticket', title: 'Ticket', width: 190 },
{ id: 'owner', title: 'Owner', width: 100 },
{ id: 'priority', title: 'Priority', width: 90 },
{
id: 'hours',
title: 'Hours',
kind: 'number',
width: 80,
align: 'end',
validation: { min: 0, max: 40 },
},
],
rows: tickets.map((ticket, i) => ({
id: i + 1,
ticket,
owner: '',
priority: i % 2 ? 'high' : 'low',
hours: i,
})),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = [
api.on('validationFailed', ({ message }) => log(`Rejected by validation: ${message}`)),
api.on('historyChanged', ({ action }) => action === 'undo' && log('Undid one step.')),
];
return () => off.forEach((fn) => fn());
}
// Each call is one undo step and one rowsChanged. If the value is unchanged nothing happens and it returns 0/false.
export const actions = {
setOne: (api: GridApi<Row>) => {
const done = api.setCellValue(2, 'owner', 'Emma Stone');
say(done ? 'Wrote owner "Emma Stone" on row 2.' : 'Same value, nothing written.');
},
setInvalid: (api: GridApi<Row>) => {
const done = api.setCellValue(3, 'hours', 99, { validate: true });
if (done) say('Written.');
},
fillPriority: (api: GridApi<Row>) => {
const n = api.fillColumn('priority', 'high');
say(`Filled priority with "high": ${n} rows changed.`);
},
updateBlock: (api: GridApi<Row>) => {
const n = api.updateCells([1, 2, 3], { owner: 'Noah Kim', hours: 8 });
say(`Wrote owner and hours on rows 1-3 at once: ${n} rows changed.`);
},
undo: (api: GridApi<Row>) => {
if (!api.undo()) say('Nothing to undo.');
},
redo: (api: GridApi<Row>) => {
if (!api.redo()) say('Nothing to redo.');
},
};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.setOne(api)">Write one cell</button>
<button
type="button"
:disabled="!api"
@click="api && actions.setInvalid(api)"
>Write with validation</button>
<button
type="button"
:disabled="!api"
@click="api && actions.fillPriority(api)"
>Fill column</button>
<button
type="button"
:disabled="!api"
@click="api && actions.updateBlock(api)"
>Write rows</button>
<button type="button" :disabled="!api" @click="api && actions.undo(api)">Undo</button>
<button type="button" :disabled="!api" @click="api && actions.redo(api)">Redo</button>
</div>
<Grid style="height: 280px" :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;
ticket: string;
owner: string;
priority: string;
hours: number;
}
const tickets = [
'Login redirect error',
'Clean up footer links',
'Search results are slow',
'Typo in price table',
'Notification settings not saved',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'ticket', title: 'Ticket', width: 190 },
{ id: 'owner', title: 'Owner', width: 100 },
{ id: 'priority', title: 'Priority', width: 90 },
{
id: 'hours',
title: 'Hours',
kind: 'number',
width: 80,
align: 'end',
validation: { min: 0, max: 40 },
},
],
rows: tickets.map((ticket, i) => ({
id: i + 1,
ticket,
owner: '',
priority: i % 2 ? 'high' : 'low',
hours: i,
})),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = [
api.on('validationFailed', ({ message }) => log(`Rejected by validation: ${message}`)),
api.on('historyChanged', ({ action }) => action === 'undo' && log('Undid one step.')),
];
return () => off.forEach((fn) => fn());
}
// Each call is one undo step and one rowsChanged. If the value is unchanged nothing happens and it returns 0/false.
export const actions = {
setOne: (api: GridApi<Row>) => {
const done = api.setCellValue(2, 'owner', 'Emma Stone');
say(done ? 'Wrote owner "Emma Stone" on row 2.' : 'Same value, nothing written.');
},
setInvalid: (api: GridApi<Row>) => {
const done = api.setCellValue(3, 'hours', 99, { validate: true });
if (done) say('Written.');
},
fillPriority: (api: GridApi<Row>) => {
const n = api.fillColumn('priority', 'high');
say(`Filled priority with "high": ${n} rows changed.`);
},
updateBlock: (api: GridApi<Row>) => {
const n = api.updateCells([1, 2, 3], { owner: 'Noah Kim', hours: 8 });
say(`Wrote owner and hours on rows 1-3 at once: ${n} rows changed.`);
},
undo: (api: GridApi<Row>) => {
if (!api.undo()) say('Nothing to undo.');
},
redo: (api: GridApi<Row>) => {
if (!api.redo()) say('Nothing to redo.');
},
};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.setOne(api)"
>Write one cell</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.setInvalid(api)"
>Write with validation</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.fillPriority(api)"
>Fill column</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.updateBlock(api)"
>Write rows</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.undo(api)"
>Undo</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.redo(api)"
>Redo</button>
</div>
<mx-grid style="height: 280px" [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;
ticket: string;
owner: string;
priority: string;
hours: number;
}
const tickets = [
'Login redirect error',
'Clean up footer links',
'Search results are slow',
'Typo in price table',
'Notification settings not saved',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'ticket', title: 'Ticket', width: 190 },
{ id: 'owner', title: 'Owner', width: 100 },
{ id: 'priority', title: 'Priority', width: 90 },
{
id: 'hours',
title: 'Hours',
kind: 'number',
width: 80,
align: 'end',
validation: { min: 0, max: 40 },
},
],
rows: tickets.map((ticket, i) => ({
id: i + 1,
ticket,
owner: '',
priority: i % 2 ? 'high' : 'low',
hours: i,
})),
rowKey: 'id',
saveMode: 'batch',
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = [
api.on('validationFailed', ({ message }) => log(`Rejected by validation: ${message}`)),
api.on('historyChanged', ({ action }) => action === 'undo' && log('Undid one step.')),
];
return () => off.forEach((fn) => fn());
}
// Each call is one undo step and one rowsChanged. If the value is unchanged nothing happens and it returns 0/false.
export const actions = {
setOne: (api: GridApi<Row>) => {
const done = api.setCellValue(2, 'owner', 'Emma Stone');
say(done ? 'Wrote owner "Emma Stone" on row 2.' : 'Same value, nothing written.');
},
setInvalid: (api: GridApi<Row>) => {
const done = api.setCellValue(3, 'hours', 99, { validate: true });
if (done) say('Written.');
},
fillPriority: (api: GridApi<Row>) => {
const n = api.fillColumn('priority', 'high');
say(`Filled priority with "high": ${n} rows changed.`);
},
updateBlock: (api: GridApi<Row>) => {
const n = api.updateCells([1, 2, 3], { owner: 'Noah Kim', hours: 8 });
say(`Wrote owner and hours on rows 1-3 at once: ${n} rows changed.`);
},
undo: (api: GridApi<Row>) => {
if (!api.undo()) say('Nothing to undo.');
},
redo: (api: GridApi<Row>) => {
if (!api.redo()) say('Nothing to redo.');
},
};