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

줄 단위로 고치고 한 번에 확정하기Edit a whole row and commit it at once

줄 편집 모드에서는 Tab으로 같은 줄의 칸들을 차례로 고치고, 마지막에 줄 전체를 한 번에 확정해요. Esc 한 번이면 그 줄에서 고친 것이 모두 사라져요. 칸마다 바로 확정하는 기본 방식과 바꿔 가며 비교해 볼 수 있어요.In row mode, Tab walks through the cells of a row and the whole row commits at once. One Esc discards everything edited in that row. Compare with the default mode where each cell commits on its own.

입문Beginner
직접 써 보기Live preview줄 단위로 고치고 한 번에 확정하기Edit a whole row and commit it at once

코드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">
    <select aria-label="편집 단위" id="mode">
      <option value="row" selected>줄 단위</option>
      <option value="cell">칸 단위</option>
    </select>
    <button type="button" id="editFirst">첫 줄 편집</button>
  </div>
  <div id="grid" style="height: 210px"></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('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
document.getElementById('editFirst').addEventListener('click', () => actions.editFirst(api));
example.js
const rows = [
  { id: 1, name: '김서연', team: '개발', hours: 32 },
  { id: 2, name: '이준호', team: '디자인', hours: 40 },
  { id: 3, name: '박민지', team: '기획', hours: 24 },
  { id: 4, name: '최도윤', team: '개발', hours: 36 },
];
// editMode: 'row'는 줄 전체를 한 덩어리로 고쳐요. Tab으로 같은 줄의 다음 칸으로 가고, 마지막 칸에서 Enter를 누르거나
// 다른 줄을 누르면 줄 전체가 확정돼요. Esc는 그 줄에서 고친 것을 모두 버려요.
// 기본값 'cell'은 칸마다 따로 확정해요.
export const options = {
  editMode: 'row',
  columns: [
    { id: 'name', title: '이름', width: 120, editable: true, validation: { required: true } },
    {
      id: 'team',
      title: '팀',
      kind: 'select',
      width: 110,
      editable: true,
      editorOptions: ['개발', '디자인', '기획'].map((value) => ({ value })),
    },
    {
      id: 'hours',
      title: '주간 시간',
      kind: 'number',
      width: 110,
      align: 'end',
      editable: true,
      validation: { min: 0, max: 60 },
    },
  ],
  rows,
  rowKey: 'id',
};
let rowMode = true;
export function setup(api, log) {
  log({
    ko: '이름 칸을 더블클릭하고 Tab으로 팀·시간까지 고친 뒤 Enter를 눌러 보세요.',
    en: 'Double-click a name cell, edit the team and hours with Tab, then press Enter.',
  });
  const title = (id) => api.getColumns().find((c) => c.id === id)?.title ?? id;
  const offs = [
    api.on('rowEditCommitted', (e) => log({
      ko: `✓ ${e.rowKey}번 줄 확정: ${e.cells.map((c) => title(c.columnId)).join(', ')}`,
      en: `✓ Row ${e.rowKey} confirmed: ${e.cells.map((c) => title(c.columnId)).join(', ')}`,
    })),
    api.on('rowEditCancelled', (e) => log({ ko: `↺ ${e.rowKey}번 줄 취소`, en: `↺ Row ${e.rowKey} cancelled` })),
    // 줄 단위에서는 줄이 확정되기 전에는 아무것도 적지 않아요(칸이 하나씩 확정되는 것이 아니에요).
    api.on('editCommitted', (e) => {
      if (rowMode)
        return;
      log(`· ${title(e.columnId)} = ${String(e.newValue)}`);
    }),
  ];
  return () => offs.forEach((off) => off());
}
export const actions = {
  mode: (api, value) => {
    rowMode = value !== 'cell';
    api.setOptions({ editMode: rowMode ? 'row' : 'cell' });
  },
  editFirst: (api) => api.startEdit(1, 'name'),
};

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">
    <select aria-label="Edit unit" id="mode">
      <option value="row" selected>Row</option>
      <option value="cell">Cell</option>
    </select>
    <button type="button" id="editFirst">Edit first row</button>
  </div>
  <div id="grid" style="height: 210px"></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('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
document.getElementById('editFirst').addEventListener('click', () => actions.editFirst(api));
example.js
const rows = [
  { id: 1, name: 'Liam Carter', team: 'Dev', hours: 32 },
  { id: 2, name: 'Emma Stone', team: 'Design', hours: 40 },
  { id: 3, name: 'Noah Kim', team: 'Planning', hours: 24 },
  { id: 4, name: 'Olivia Reed', team: 'Dev', hours: 36 },
];
// editMode: 'row' edits a whole row as one unit. Tab moves to the next cell in the same row, and pressing Enter in the last cell
// or clicking another row commits the whole row. Esc discards everything changed in that row.
// The default 'cell' commits each cell separately.
export const options = {
  editMode: 'row',
  columns: [
    { id: 'name', title: 'Name', width: 140, editable: true, validation: { required: true } },
    {
      id: 'team',
      title: 'Team',
      kind: 'select',
      width: 110,
      editable: true,
      editorOptions: ['Dev', 'Design', 'Planning'].map((value) => ({ value })),
    },
    {
      id: 'hours',
      title: 'Weekly hours',
      kind: 'number',
      width: 130,
      align: 'end',
      editable: true,
      validation: { min: 0, max: 60 },
    },
  ],
  rows,
  rowKey: 'id',
};
let rowMode = true;
export function setup(api, log) {
  log('Double-click a name cell, edit the team and hours with Tab, then press Enter.');
  const title = (id) => api.getColumns().find((c) => c.id === id)?.title ?? id;
  const offs = [
    api.on('rowEditCommitted', (e) => log(`✓ Row ${e.rowKey} confirmed: ${e.cells.map((c) => title(c.columnId)).join(', ')}`)),
    api.on('rowEditCancelled', (e) => log(`↺ Row ${e.rowKey} cancelled`)),
    // In row mode nothing is logged until the row is committed (cells are not committed one by one).
    api.on('editCommitted', (e) => {
      if (rowMode)
        return;
      log(`· ${title(e.columnId)} = ${String(e.newValue)}`);
    }),
  ];
  return () => offs.forEach((off) => off());
}
export const actions = {
  mode: (api, value) => {
    rowMode = value !== 'cell';
    api.setOptions({ editMode: rowMode ? 'row' : 'cell' });
  },
  editFirst: (api) => api.startEdit(1, 'name'),
};