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

원할 때 한꺼번에 검증하기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.

활용Intermediate
직접 써 보기Live preview원할 때 한꺼번에 검증하기Validate whenever you want

코드Code

탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.

이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

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

index.html
<div id="app">
  <div class="toolbar">
    <button type="button" id="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>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('all').addEventListener('click', () => actions.all(api));
document.getElementById('changed').addEventListener('click', () => actions.changed(api));
document.getElementById('editCode').addEventListener('click', () => actions.editCode(api));
example.js
// 예제 틀이 알려 주는 페이지 언어(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'),
};

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

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<div id="app">
  <div class="toolbar">
    <button type="button" id="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>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('all').addEventListener('click', () => actions.all(api));
document.getElementById('changed').addEventListener('click', () => actions.changed(api));
document.getElementById('editCode').addEventListener('click', () => actions.editCode(api));
example.js
// 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'),
};