원할 때 한꺼번에 검증하기Validate whenever you want
validateRows로 지금 데이터를 규칙에 맞춰 한 번에 검사해요. 편집하지 않아도 되고, 이벤트·포커스·스크롤은 그대로예요. unique 규칙은 같은 컬럼의 값이 겹치지 않게 하고, check로 행 단위 검사를 덧붙일 수 있어요.validateRows checks the current data against the rules in one call, without editing; events, focus and scroll are untouched. The unique rule keeps a column's values apart, and check adds row-level validation.
코드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="all">모두 검사</button>
<button type="button" id="changed">바뀐 행만 검사</button>
<button type="button" id="editCode">코드 편집</button>
</div>
<div id="grid" style="height: 270px"></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('all').addEventListener('click', () => actions.all(api));
document.getElementById('changed').addEventListener('click', () => actions.changed(api));
document.getElementById('editCode').addEventListener('click', () => actions.editCode(api));// 예제 틀이 알려 주는 페이지 언어(ko/en)에 맞는 문구를 골라요.
const L = (ko, en) => document.documentElement.dataset.lang === 'en' ? en : ko;
// 4번 줄은 코드가 1번과 같고, 5번 줄은 수량이 범위 밖이고, 3번 줄은 끝이 시작보다 빨라요.
const rows = [
{ id: 1, code: 'A-100', qty: 5, start: 9, end: 12 },
{ id: 2, code: 'A-200', qty: 8, start: 10, end: 14 },
{ id: 3, code: 'A-300', qty: 3, start: 15, end: 11 },
{ id: 4, code: 'A-100', qty: 7, start: 9, end: 13 },
{ id: 5, code: 'A-500', qty: 99, start: 8, end: 10 },
];
// unique는 같은 컬럼의 다른 행과 겹치지 않게 해요(고치는 칸 자신은 빼고 비교해요).
// 편집·붙여넣기·저장에서도 막히지만, validateRows로는 지금 데이터를 언제든 한 번에 검사할 수 있어요.
export const options = {
columns: [
{
id: 'code',
title: '코드',
width: 110,
editable: true,
validation: { required: true, unique: true },
},
{
id: 'qty',
title: '수량 (1~20)',
kind: 'number',
width: 110,
editable: true,
validation: { min: 1, max: 20 },
},
{ id: 'start', title: '시작', kind: 'number', width: 80, editable: true },
{ id: 'end', title: '끝', kind: 'number', width: 80, editable: true },
],
rows,
rowKey: 'id',
};
let say = () => undefined;
const report = (api, changedOnly) => {
const errors = api.validateRows({
changedOnly,
// 컬럼 규칙에 없는 행 단위 검사는 check로 덧붙여요.
check: (row) => row.end < row.start
? {
rowKey: row.id,
columnId: 'end',
message: L('끝은 시작보다 늦어야 해요.', 'End must be after the start.'),
}
: null,
});
if (errors.length === 0)
return say({ ko: '문제가 없어요.', en: 'No problems found.' });
say({ ko: `문제 ${errors.length}건`, en: `${errors.length} problem(s)` });
for (const e of errors)
say(`#${e.rowKey} ${e.columnId}: ${e.message}`);
};
export function setup(_api, log) {
say = log;
log({
ko: '"모두 검사"를 눌러 보세요. 검사해도 포커스와 스크롤은 그대로예요.',
en: 'Press "Validate all". Focus and scroll stay where they are.',
});
}
export const actions = {
all: (api) => report(api, false),
changed: (api) => report(api, true),
// 코드를 다른 행과 같은 값으로 바꾸려고 하면 unique 규칙이 이유와 함께 막아요.
editCode: (api) => api.startEdit(2, 'code'),
};<!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="all">모두 검사</button>
<button type="button" id="changed">바뀐 행만 검사</button>
<button type="button" id="editCode">코드 편집</button>
</div>
<div id="grid" style="height: 270px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// 예제 틀이 알려 주는 페이지 언어(ko/en)에 맞는 문구를 골라요.
const L = (ko, en) => document.documentElement.dataset.lang === 'en' ? en : ko;
// 4번 줄은 코드가 1번과 같고, 5번 줄은 수량이 범위 밖이고, 3번 줄은 끝이 시작보다 빨라요.
const rows = [
{ id: 1, code: 'A-100', qty: 5, start: 9, end: 12 },
{ id: 2, code: 'A-200', qty: 8, start: 10, end: 14 },
{ id: 3, code: 'A-300', qty: 3, start: 15, end: 11 },
{ id: 4, code: 'A-100', qty: 7, start: 9, end: 13 },
{ id: 5, code: 'A-500', qty: 99, start: 8, end: 10 },
];
// unique는 같은 컬럼의 다른 행과 겹치지 않게 해요(고치는 칸 자신은 빼고 비교해요).
// 편집·붙여넣기·저장에서도 막히지만, validateRows로는 지금 데이터를 언제든 한 번에 검사할 수 있어요.
const options = {
columns: [
{
id: 'code',
title: '코드',
width: 110,
editable: true,
validation: { required: true, unique: true },
},
{
id: 'qty',
title: '수량 (1~20)',
kind: 'number',
width: 110,
editable: true,
validation: { min: 1, max: 20 },
},
{ id: 'start', title: '시작', kind: 'number', width: 80, editable: true },
{ id: 'end', title: '끝', kind: 'number', width: 80, editable: true },
],
rows,
rowKey: 'id',
};
let say = () => undefined;
const report = (api, changedOnly) => {
const errors = api.validateRows({
changedOnly,
// 컬럼 규칙에 없는 행 단위 검사는 check로 덧붙여요.
check: (row) => row.end < row.start
? {
rowKey: row.id,
columnId: 'end',
message: L('끝은 시작보다 늦어야 해요.', 'End must be after the start.'),
}
: null,
});
if (errors.length === 0)
return say({ ko: '문제가 없어요.', en: 'No problems found.' });
say({ ko: `문제 ${errors.length}건`, en: `${errors.length} problem(s)` });
for (const e of errors)
say(`#${e.rowKey} ${e.columnId}: ${e.message}`);
};
function setup(_api, log) {
say = log;
log({
ko: '"모두 검사"를 눌러 보세요. 검사해도 포커스와 스크롤은 그대로예요.',
en: 'Press "Validate all". Focus and scroll stay where they are.',
});
}
const actions = {
all: (api) => report(api, false),
changed: (api) => report(api, true),
// 코드를 다른 행과 같은 값으로 바꾸려고 하면 unique 규칙이 이유와 함께 막아요.
editCode: (api) => api.startEdit(2, 'code'),
};
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('all').addEventListener('click', () => actions.all(api));
document.getElementById('changed').addEventListener('click', () => actions.changed(api));
document.getElementById('editCode').addEventListener('click', () => actions.editCode(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.all(api)}
>모두 검사</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.changed(api)}
>바뀐 행만 검사</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.editCode(api)}
>코드 편집</button>
</div>
<Grid style={{ height: 270 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
// 예제 틀이 알려 주는 페이지 언어(ko/en)에 맞는 문구를 골라요.
const L = (ko: string, en: string): string =>
document.documentElement.dataset.lang === 'en' ? en : ko;
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
code: string;
qty: number;
start: number;
end: number;
}
// 4번 줄은 코드가 1번과 같고, 5번 줄은 수량이 범위 밖이고, 3번 줄은 끝이 시작보다 빨라요.
const rows: Row[] = [
{ id: 1, code: 'A-100', qty: 5, start: 9, end: 12 },
{ id: 2, code: 'A-200', qty: 8, start: 10, end: 14 },
{ id: 3, code: 'A-300', qty: 3, start: 15, end: 11 },
{ id: 4, code: 'A-100', qty: 7, start: 9, end: 13 },
{ id: 5, code: 'A-500', qty: 99, start: 8, end: 10 },
];
// unique는 같은 컬럼의 다른 행과 겹치지 않게 해요(고치는 칸 자신은 빼고 비교해요).
// 편집·붙여넣기·저장에서도 막히지만, validateRows로는 지금 데이터를 언제든 한 번에 검사할 수 있어요.
export const options: GridOptions<Row> = {
columns: [
{
id: 'code',
title: '코드',
width: 110,
editable: true,
validation: { required: true, unique: true },
},
{
id: 'qty',
title: '수량 (1~20)',
kind: 'number',
width: 110,
editable: true,
validation: { min: 1, max: 20 },
},
{ id: 'start', title: '시작', kind: 'number', width: 80, editable: true },
{ id: 'end', title: '끝', kind: 'number', width: 80, editable: true },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
const report = (api: GridApi<Row>, changedOnly: boolean) => {
const errors = api.validateRows({
changedOnly,
// 컬럼 규칙에 없는 행 단위 검사는 check로 덧붙여요.
check: (row) =>
row.end < row.start
? {
rowKey: row.id,
columnId: 'end',
message: L('끝은 시작보다 늦어야 해요.', 'End must be after the start.'),
}
: null,
});
if (errors.length === 0) return say({ ko: '문제가 없어요.', en: 'No problems found.' });
say({ ko: `문제 ${errors.length}건`, en: `${errors.length} problem(s)` });
for (const e of errors) say(`#${e.rowKey} ${e.columnId}: ${e.message}`);
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log({
ko: '"모두 검사"를 눌러 보세요. 검사해도 포커스와 스크롤은 그대로예요.',
en: 'Press "Validate all". Focus and scroll stay where they are.',
});
}
export const actions = {
all: (api: GridApi<Row>) => report(api, false),
changed: (api: GridApi<Row>) => report(api, true),
// 코드를 다른 행과 같은 값으로 바꾸려고 하면 unique 규칙이 이유와 함께 막아요.
editCode: (api: GridApi<Row>) => api.startEdit(2, 'code'),
};버튼을 누르면 이 예제의 코드가 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.all(api)">모두 검사</button>
<button type="button" :disabled="!api" @click="api && actions.changed(api)">바뀐 행만 검사</button>
<button
type="button"
:disabled="!api"
@click="api && actions.editCode(api)"
>코드 편집</button>
</div>
<Grid style="height: 270px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
// 예제 틀이 알려 주는 페이지 언어(ko/en)에 맞는 문구를 골라요.
const L = (ko: string, en: string): string =>
document.documentElement.dataset.lang === 'en' ? en : ko;
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
code: string;
qty: number;
start: number;
end: number;
}
// 4번 줄은 코드가 1번과 같고, 5번 줄은 수량이 범위 밖이고, 3번 줄은 끝이 시작보다 빨라요.
const rows: Row[] = [
{ id: 1, code: 'A-100', qty: 5, start: 9, end: 12 },
{ id: 2, code: 'A-200', qty: 8, start: 10, end: 14 },
{ id: 3, code: 'A-300', qty: 3, start: 15, end: 11 },
{ id: 4, code: 'A-100', qty: 7, start: 9, end: 13 },
{ id: 5, code: 'A-500', qty: 99, start: 8, end: 10 },
];
// unique는 같은 컬럼의 다른 행과 겹치지 않게 해요(고치는 칸 자신은 빼고 비교해요).
// 편집·붙여넣기·저장에서도 막히지만, validateRows로는 지금 데이터를 언제든 한 번에 검사할 수 있어요.
export const options: GridOptions<Row> = {
columns: [
{
id: 'code',
title: '코드',
width: 110,
editable: true,
validation: { required: true, unique: true },
},
{
id: 'qty',
title: '수량 (1~20)',
kind: 'number',
width: 110,
editable: true,
validation: { min: 1, max: 20 },
},
{ id: 'start', title: '시작', kind: 'number', width: 80, editable: true },
{ id: 'end', title: '끝', kind: 'number', width: 80, editable: true },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
const report = (api: GridApi<Row>, changedOnly: boolean) => {
const errors = api.validateRows({
changedOnly,
// 컬럼 규칙에 없는 행 단위 검사는 check로 덧붙여요.
check: (row) =>
row.end < row.start
? {
rowKey: row.id,
columnId: 'end',
message: L('끝은 시작보다 늦어야 해요.', 'End must be after the start.'),
}
: null,
});
if (errors.length === 0) return say({ ko: '문제가 없어요.', en: 'No problems found.' });
say({ ko: `문제 ${errors.length}건`, en: `${errors.length} problem(s)` });
for (const e of errors) say(`#${e.rowKey} ${e.columnId}: ${e.message}`);
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log({
ko: '"모두 검사"를 눌러 보세요. 검사해도 포커스와 스크롤은 그대로예요.',
en: 'Press "Validate all". Focus and scroll stay where they are.',
});
}
export const actions = {
all: (api: GridApi<Row>) => report(api, false),
changed: (api: GridApi<Row>) => report(api, true),
// 코드를 다른 행과 같은 값으로 바꾸려고 하면 unique 규칙이 이유와 함께 막아요.
editCode: (api: GridApi<Row>) => api.startEdit(2, 'code'),
};버튼을 누르면 이 예제의 코드가 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.all(api)"
>모두 검사</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.changed(api)"
>바뀐 행만 검사</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.editCode(api)"
>코드 편집</button>
</div>
<mx-grid style="height: 270px" [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';
// 예제 틀이 알려 주는 페이지 언어(ko/en)에 맞는 문구를 골라요.
const L = (ko: string, en: string): string =>
document.documentElement.dataset.lang === 'en' ? en : ko;
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
code: string;
qty: number;
start: number;
end: number;
}
// 4번 줄은 코드가 1번과 같고, 5번 줄은 수량이 범위 밖이고, 3번 줄은 끝이 시작보다 빨라요.
const rows: Row[] = [
{ id: 1, code: 'A-100', qty: 5, start: 9, end: 12 },
{ id: 2, code: 'A-200', qty: 8, start: 10, end: 14 },
{ id: 3, code: 'A-300', qty: 3, start: 15, end: 11 },
{ id: 4, code: 'A-100', qty: 7, start: 9, end: 13 },
{ id: 5, code: 'A-500', qty: 99, start: 8, end: 10 },
];
// unique는 같은 컬럼의 다른 행과 겹치지 않게 해요(고치는 칸 자신은 빼고 비교해요).
// 편집·붙여넣기·저장에서도 막히지만, validateRows로는 지금 데이터를 언제든 한 번에 검사할 수 있어요.
export const options: GridOptions<Row> = {
columns: [
{
id: 'code',
title: '코드',
width: 110,
editable: true,
validation: { required: true, unique: true },
},
{
id: 'qty',
title: '수량 (1~20)',
kind: 'number',
width: 110,
editable: true,
validation: { min: 1, max: 20 },
},
{ id: 'start', title: '시작', kind: 'number', width: 80, editable: true },
{ id: 'end', title: '끝', kind: 'number', width: 80, editable: true },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
const report = (api: GridApi<Row>, changedOnly: boolean) => {
const errors = api.validateRows({
changedOnly,
// 컬럼 규칙에 없는 행 단위 검사는 check로 덧붙여요.
check: (row) =>
row.end < row.start
? {
rowKey: row.id,
columnId: 'end',
message: L('끝은 시작보다 늦어야 해요.', 'End must be after the start.'),
}
: null,
});
if (errors.length === 0) return say({ ko: '문제가 없어요.', en: 'No problems found.' });
say({ ko: `문제 ${errors.length}건`, en: `${errors.length} problem(s)` });
for (const e of errors) say(`#${e.rowKey} ${e.columnId}: ${e.message}`);
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log({
ko: '"모두 검사"를 눌러 보세요. 검사해도 포커스와 스크롤은 그대로예요.',
en: 'Press "Validate all". Focus and scroll stay where they are.',
});
}
export const actions = {
all: (api: GridApi<Row>) => report(api, false),
changed: (api: GridApi<Row>) => report(api, true),
// 코드를 다른 행과 같은 값으로 바꾸려고 하면 unique 규칙이 이유와 함께 막아요.
editCode: (api: GridApi<Row>) => api.startEdit(2, 'code'),
};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="all">Validate all</button>
<button type="button" id="changed">Validate changed rows</button>
<button type="button" id="editCode">Edit code</button>
</div>
<div id="grid" style="height: 270px"></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('all').addEventListener('click', () => actions.all(api));
document.getElementById('changed').addEventListener('click', () => actions.changed(api));
document.getElementById('editCode').addEventListener('click', () => actions.editCode(api));// Row 4 has the same code as row 1, row 5 has a qty out of range, and row 3 ends before it starts.
const rows = [
{ id: 1, code: 'A-100', qty: 5, start: 9, end: 12 },
{ id: 2, code: 'A-200', qty: 8, start: 10, end: 14 },
{ id: 3, code: 'A-300', qty: 3, start: 15, end: 11 },
{ id: 4, code: 'A-100', qty: 7, start: 9, end: 13 },
{ id: 5, code: 'A-500', qty: 99, start: 8, end: 10 },
];
// unique keeps a value from repeating in other rows of the same column (the cell being edited is excluded from the comparison).
// It also blocks on edit, paste and save, but validateRows can check the current data all at once at any time.
export const options = {
columns: [
{
id: 'code',
title: 'Code',
width: 110,
editable: true,
validation: { required: true, unique: true },
},
{
id: 'qty',
title: 'Qty (1-20)',
kind: 'number',
width: 110,
editable: true,
validation: { min: 1, max: 20 },
},
{ id: 'start', title: 'Start', kind: 'number', width: 80, editable: true },
{ id: 'end', title: 'End', kind: 'number', width: 80, editable: true },
],
rows,
rowKey: 'id',
};
let say = () => undefined;
const report = (api, changedOnly) => {
const errors = api.validateRows({
changedOnly,
// A row-level check that column rules cannot express is added with check.
check: (row) => row.end < row.start
? {
rowKey: row.id,
columnId: 'end',
message: 'End must be after the start.',
}
: null,
});
if (errors.length === 0)
return say('No problems found.');
say(`${errors.length} problem(s)`);
for (const e of errors)
say(`#${e.rowKey} ${e.columnId}: ${e.message}`);
};
export function setup(_api, log) {
say = log;
log('Press "Validate all". Focus and scroll stay where they are.');
}
export const actions = {
all: (api) => report(api, false),
changed: (api) => report(api, true),
// If you try to change a code to the same value as another row, the unique rule blocks it with a reason.
editCode: (api) => api.startEdit(2, 'code'),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Validate whenever you want</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="all">Validate all</button>
<button type="button" id="changed">Validate changed rows</button>
<button type="button" id="editCode">Edit code</button>
</div>
<div id="grid" style="height: 270px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// Row 4 has the same code as row 1, row 5 has a qty out of range, and row 3 ends before it starts.
const rows = [
{ id: 1, code: 'A-100', qty: 5, start: 9, end: 12 },
{ id: 2, code: 'A-200', qty: 8, start: 10, end: 14 },
{ id: 3, code: 'A-300', qty: 3, start: 15, end: 11 },
{ id: 4, code: 'A-100', qty: 7, start: 9, end: 13 },
{ id: 5, code: 'A-500', qty: 99, start: 8, end: 10 },
];
// unique keeps a value from repeating in other rows of the same column (the cell being edited is excluded from the comparison).
// It also blocks on edit, paste and save, but validateRows can check the current data all at once at any time.
const options = {
columns: [
{
id: 'code',
title: 'Code',
width: 110,
editable: true,
validation: { required: true, unique: true },
},
{
id: 'qty',
title: 'Qty (1-20)',
kind: 'number',
width: 110,
editable: true,
validation: { min: 1, max: 20 },
},
{ id: 'start', title: 'Start', kind: 'number', width: 80, editable: true },
{ id: 'end', title: 'End', kind: 'number', width: 80, editable: true },
],
rows,
rowKey: 'id',
};
let say = () => undefined;
const report = (api, changedOnly) => {
const errors = api.validateRows({
changedOnly,
// A row-level check that column rules cannot express is added with check.
check: (row) => row.end < row.start
? {
rowKey: row.id,
columnId: 'end',
message: 'End must be after the start.',
}
: null,
});
if (errors.length === 0)
return say('No problems found.');
say(`${errors.length} problem(s)`);
for (const e of errors)
say(`#${e.rowKey} ${e.columnId}: ${e.message}`);
};
function setup(_api, log) {
say = log;
log('Press "Validate all". Focus and scroll stay where they are.');
}
const actions = {
all: (api) => report(api, false),
changed: (api) => report(api, true),
// If you try to change a code to the same value as another row, the unique rule blocks it with a reason.
editCode: (api) => api.startEdit(2, 'code'),
};
options.ariaLabel = "Validate whenever you want";
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('all').addEventListener('click', () => actions.all(api));
document.getElementById('changed').addEventListener('click', () => actions.changed(api));
document.getElementById('editCode').addEventListener('click', () => actions.editCode(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.all(api)}
>Validate all</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.changed(api)}
>Validate changed rows</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.editCode(api)}
>Edit code</button>
</div>
<Grid style={{ height: 270 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
code: string;
qty: number;
start: number;
end: number;
}
// Row 4 has the same code as row 1, row 5 has a qty out of range, and row 3 ends before it starts.
const rows: Row[] = [
{ id: 1, code: 'A-100', qty: 5, start: 9, end: 12 },
{ id: 2, code: 'A-200', qty: 8, start: 10, end: 14 },
{ id: 3, code: 'A-300', qty: 3, start: 15, end: 11 },
{ id: 4, code: 'A-100', qty: 7, start: 9, end: 13 },
{ id: 5, code: 'A-500', qty: 99, start: 8, end: 10 },
];
// unique keeps a value from repeating in other rows of the same column (the cell being edited is excluded from the comparison).
// It also blocks on edit, paste and save, but validateRows can check the current data all at once at any time.
export const options: GridOptions<Row> = {
columns: [
{
id: 'code',
title: 'Code',
width: 110,
editable: true,
validation: { required: true, unique: true },
},
{
id: 'qty',
title: 'Qty (1-20)',
kind: 'number',
width: 110,
editable: true,
validation: { min: 1, max: 20 },
},
{ id: 'start', title: 'Start', kind: 'number', width: 80, editable: true },
{ id: 'end', title: 'End', kind: 'number', width: 80, editable: true },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
const report = (api: GridApi<Row>, changedOnly: boolean) => {
const errors = api.validateRows({
changedOnly,
// A row-level check that column rules cannot express is added with check.
check: (row) =>
row.end < row.start
? {
rowKey: row.id,
columnId: 'end',
message: 'End must be after the start.',
}
: null,
});
if (errors.length === 0) return say('No problems found.');
say(`${errors.length} problem(s)`);
for (const e of errors) say(`#${e.rowKey} ${e.columnId}: ${e.message}`);
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log('Press "Validate all". Focus and scroll stay where they are.');
}
export const actions = {
all: (api: GridApi<Row>) => report(api, false),
changed: (api: GridApi<Row>) => report(api, true),
// If you try to change a code to the same value as another row, the unique rule blocks it with a reason.
editCode: (api: GridApi<Row>) => api.startEdit(2, 'code'),
};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.all(api)">Validate all</button>
<button type="button" :disabled="!api" @click="api && actions.changed(api)">Validate changed rows</button>
<button
type="button"
:disabled="!api"
@click="api && actions.editCode(api)"
>Edit code</button>
</div>
<Grid style="height: 270px" :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;
code: string;
qty: number;
start: number;
end: number;
}
// Row 4 has the same code as row 1, row 5 has a qty out of range, and row 3 ends before it starts.
const rows: Row[] = [
{ id: 1, code: 'A-100', qty: 5, start: 9, end: 12 },
{ id: 2, code: 'A-200', qty: 8, start: 10, end: 14 },
{ id: 3, code: 'A-300', qty: 3, start: 15, end: 11 },
{ id: 4, code: 'A-100', qty: 7, start: 9, end: 13 },
{ id: 5, code: 'A-500', qty: 99, start: 8, end: 10 },
];
// unique keeps a value from repeating in other rows of the same column (the cell being edited is excluded from the comparison).
// It also blocks on edit, paste and save, but validateRows can check the current data all at once at any time.
export const options: GridOptions<Row> = {
columns: [
{
id: 'code',
title: 'Code',
width: 110,
editable: true,
validation: { required: true, unique: true },
},
{
id: 'qty',
title: 'Qty (1-20)',
kind: 'number',
width: 110,
editable: true,
validation: { min: 1, max: 20 },
},
{ id: 'start', title: 'Start', kind: 'number', width: 80, editable: true },
{ id: 'end', title: 'End', kind: 'number', width: 80, editable: true },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
const report = (api: GridApi<Row>, changedOnly: boolean) => {
const errors = api.validateRows({
changedOnly,
// A row-level check that column rules cannot express is added with check.
check: (row) =>
row.end < row.start
? {
rowKey: row.id,
columnId: 'end',
message: 'End must be after the start.',
}
: null,
});
if (errors.length === 0) return say('No problems found.');
say(`${errors.length} problem(s)`);
for (const e of errors) say(`#${e.rowKey} ${e.columnId}: ${e.message}`);
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log('Press "Validate all". Focus and scroll stay where they are.');
}
export const actions = {
all: (api: GridApi<Row>) => report(api, false),
changed: (api: GridApi<Row>) => report(api, true),
// If you try to change a code to the same value as another row, the unique rule blocks it with a reason.
editCode: (api: GridApi<Row>) => api.startEdit(2, 'code'),
};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.all(api)"
>Validate all</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.changed(api)"
>Validate changed rows</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.editCode(api)"
>Edit code</button>
</div>
<mx-grid style="height: 270px" [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;
code: string;
qty: number;
start: number;
end: number;
}
// Row 4 has the same code as row 1, row 5 has a qty out of range, and row 3 ends before it starts.
const rows: Row[] = [
{ id: 1, code: 'A-100', qty: 5, start: 9, end: 12 },
{ id: 2, code: 'A-200', qty: 8, start: 10, end: 14 },
{ id: 3, code: 'A-300', qty: 3, start: 15, end: 11 },
{ id: 4, code: 'A-100', qty: 7, start: 9, end: 13 },
{ id: 5, code: 'A-500', qty: 99, start: 8, end: 10 },
];
// unique keeps a value from repeating in other rows of the same column (the cell being edited is excluded from the comparison).
// It also blocks on edit, paste and save, but validateRows can check the current data all at once at any time.
export const options: GridOptions<Row> = {
columns: [
{
id: 'code',
title: 'Code',
width: 110,
editable: true,
validation: { required: true, unique: true },
},
{
id: 'qty',
title: 'Qty (1-20)',
kind: 'number',
width: 110,
editable: true,
validation: { min: 1, max: 20 },
},
{ id: 'start', title: 'Start', kind: 'number', width: 80, editable: true },
{ id: 'end', title: 'End', kind: 'number', width: 80, editable: true },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
const report = (api: GridApi<Row>, changedOnly: boolean) => {
const errors = api.validateRows({
changedOnly,
// A row-level check that column rules cannot express is added with check.
check: (row) =>
row.end < row.start
? {
rowKey: row.id,
columnId: 'end',
message: 'End must be after the start.',
}
: null,
});
if (errors.length === 0) return say('No problems found.');
say(`${errors.length} problem(s)`);
for (const e of errors) say(`#${e.rowKey} ${e.columnId}: ${e.message}`);
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log('Press "Validate all". Focus and scroll stay where they are.');
}
export const actions = {
all: (api: GridApi<Row>) => report(api, false),
changed: (api: GridApi<Row>) => report(api, true),
// If you try to change a code to the same value as another row, the unique rule blocks it with a reason.
editCode: (api: GridApi<Row>) => api.startEdit(2, 'code'),
};