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

마우스 없이 입력하기: 편집 시작과 키보드 이동Entering data without the mouse

더블클릭, F2·Enter, 글자 입력, 코드의 편집 시작 호출로 편집을 시작할 수 있고, Enter는 확정, Esc는 취소, Tab은 다음에 고칠 수 있는 칸으로 가요. 읽기 전용 칸은 건너뛰어요.Start an edit by double-click, F2, Enter, typing, or startEdit from code. Enter commits, Esc cancels and Tab jumps to the next editable cell, skipping read-only ones.

입문Beginner
직접 써 보기Live preview마우스 없이 입력하기: 편집 시작과 키보드 이동Entering data without the mouse

코드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="editQty">코드로 수량 편집 시작</button>
    <button type="button" id="editNote">코드로 메모 편집 시작</button>
    <button type="button" id="cancel">편집 취소</button>
  </div>
  <div id="grid" style="height: 340px"></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('editQty').addEventListener('click', () => actions.editQty(api));
document.getElementById('editNote').addEventListener('click', () => actions.editNote(api));
document.getElementById('cancel').addEventListener('click', () => actions.cancel(api));
example.js
const items = ['화이트보드', '보드 마카', '지우개', '자석 클립', '포스트잇', '레이저 포인터'];
// "품목"은 읽기 전용이고 나머지는 고칠 수 있어요. Tab은 다음에 고칠 수 있는 칸으로 가기 때문에 품목 칸은 건너뛰어요.
const rows = items.map((item, i) => ({
  id: i + 1,
  item,
  qty: 1 + ((i * 3) % 8),
  price: 1500 + ((i * 7) % 6) * 2500,
  note: '',
}));
export const options = {
  columns: [
    { id: 'item', title: '품목', width: 130 },
    { id: 'qty', title: '수량', kind: 'number', width: 90, align: 'end', editable: true },
    {
      id: 'price',
      title: '단가',
      kind: 'number',
      width: 110,
      align: 'end',
      editable: true,
      formatValue: (v) => `${Number(v).toLocaleString('ko-KR')}원`,
    },
    { id: 'note', title: '메모', width: 180, editable: true },
  ],
  rows,
  rowKey: 'id',
};
// 편집을 시작하는 방법은 세 가지예요. editStarted 이벤트의 trigger가 어떤 방법이었는지 알려 줘요.
const how = {
  doubleClick: ['더블클릭', 'double-click'],
  click: ['한 번 클릭(목록·체크박스 칸)', 'single click (list and checkbox cells)'],
  key: ['키보드(F2·Enter·글자 입력)', 'keyboard (F2, Enter, typing)'],
  api: ['코드(startEdit)', 'code (startEdit)'],
};
export function setup(api, log) {
  const title = (id) => api.getColumns().find((c) => c.id === id)?.title ?? id;
  const offs = [
    api.on('editStarted', (e) => log({
      ko: `▶ ${title(e.columnId)} 편집 시작 — ${how[e.trigger]?.[0] ?? e.trigger}${e.startKey ? ` [${e.startKey}]` : ''}`,
      en: `▶ ${title(e.columnId)} edit started — ${how[e.trigger]?.[1] ?? e.trigger}${e.startKey ? ` [${e.startKey}]` : ''}`,
    })),
    api.on('editCommitted', (e) => log(`✓ ${title(e.columnId)} = ${String(e.newValue)}`)),
    api.on('editCancelled', (e) => log({ ko: `✗ 취소 (${e.reason})`, en: `✗ Cancelled (${e.reason})` })),
  ];
  log({
    ko: '첫 줄 "수량" 칸을 한 번 눌러 두고 키보드로 해 보세요: 방향키로 이동, F2·Enter로 편집, Tab으로 다음 칸.',
    en: 'Click the "수량" (quantity) cell of the first row once, then use the keyboard: arrows to move, F2 or Enter to edit, Tab for the next cell.',
  });
  return () => offs.forEach((off) => off());
}
export const actions = {
  editQty: (api) => api.startEdit(1, 'qty'),
  editNote: (api) => api.startEdit(1, 'note'),
  cancel: (api) => api.stopEdit({ cancel: true }),
};

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="editQty">Start qty edit from code</button>
    <button type="button" id="editNote">Start note edit from code</button>
    <button type="button" id="cancel">Cancel edit</button>
  </div>
  <div id="grid" style="height: 340px"></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('editQty').addEventListener('click', () => actions.editQty(api));
document.getElementById('editNote').addEventListener('click', () => actions.editNote(api));
document.getElementById('cancel').addEventListener('click', () => actions.cancel(api));
example.js
const items = [
  'Whiteboard',
  'Board markers',
  'Eraser',
  'Magnet clips',
  'Sticky notes',
  'Laser pointer',
];
// "Item" is read-only and the rest can be edited. Tab goes to the next editable cell, so it skips the Item cell.
const rows = items.map((item, i) => ({
  id: i + 1,
  item,
  qty: 1 + ((i * 3) % 8),
  price: 1.5 + ((i * 7) % 6) * 2.5,
  note: '',
}));
export const options = {
  columns: [
    { id: 'item', title: 'Item', width: 130 },
    { id: 'qty', title: 'Qty', kind: 'number', width: 90, align: 'end', editable: true },
    {
      id: 'price',
      title: 'Price',
      kind: 'number',
      width: 110,
      align: 'end',
      editable: true,
      formatValue: (v) => `$${Number(v).toLocaleString('en-US', { minimumFractionDigits: 2 })}`,
    },
    { id: 'note', title: 'Note', width: 180, editable: true },
  ],
  rows,
  rowKey: 'id',
};
// There are several ways to start editing. The trigger of the editStarted event tells which one was used.
const how = {
  doubleClick: 'double-click',
  click: 'single click (list and checkbox cells)',
  key: 'keyboard (F2, Enter, typing)',
  api: 'code (startEdit)',
};
export function setup(api, log) {
  const title = (id) => api.getColumns().find((c) => c.id === id)?.title ?? id;
  const offs = [
    api.on('editStarted', (e) => log(`▶ ${title(e.columnId)} edit started — ${how[e.trigger] ?? e.trigger}${e.startKey ? ` [${e.startKey}]` : ''}`)),
    api.on('editCommitted', (e) => log(`✓ ${title(e.columnId)} = ${String(e.newValue)}`)),
    api.on('editCancelled', (e) => log(`✗ Cancelled (${e.reason})`)),
  ];
  log('Click the "Qty" cell of the first row once, then use the keyboard: arrows to move, F2 or Enter to edit, Tab for the next cell.');
  return () => offs.forEach((off) => off());
}
export const actions = {
  editQty: (api) => api.startEdit(1, 'qty'),
  editNote: (api) => api.startEdit(1, 'note'),
  cancel: (api) => api.stopEdit({ cancel: true }),
};