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

훅으로 편집과 삭제 막기Block edits and removals with hooks

옵션 hooks의 beforeEdit과 beforeRemoveRows가 false를 돌려주면 편집기가 열리지 않고 행이 지워지지 않아요. 막힌 일은 이벤트도 되돌리기 기록도 남기지 않아요.When beforeEdit or beforeRemoveRows in the hooks option returns false, the editor does not open and the rows are not removed. A blocked action leaves no event and no undo entry.

활용Intermediate
직접 써 보기Live preview훅으로 편집과 삭제 막기Block edits and removals with hooks

코드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="editPaid">결제된 행 편집</button>
    <button type="button" id="editOpen">열린 행 편집</button>
    <button type="button" id="removePaid">결제된 행 삭제</button>
    <button type="button" id="removeOpen">열린 행 삭제</button>
  </div>
  <div id="grid" style="height: 230px"></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('editPaid').addEventListener('click', () => actions.editPaid(api));
document.getElementById('editOpen').addEventListener('click', () => actions.editOpen(api));
document.getElementById('removePaid').addEventListener('click', () => actions.removePaid(api));
document.getElementById('removeOpen').addEventListener('click', () => actions.removeOpen(api));
example.js
const rows = [
  { id: 1, title: '주문 A', status: 'open', amount: 12000 },
  { id: 2, title: '주문 B', status: 'paid', amount: 34000 },
  { id: 3, title: '주문 C', status: 'open', amount: 5600 },
  { id: 4, title: '주문 D', status: 'paid', amount: 78000 },
];
let log = () => undefined;
const isPaid = (key) => rows.find((r) => r.id === key)?.status === 'paid';
// hooks는 일이 일어나기 전에 불려요. false를 돌려주면 그 일은 일어나지 않아요.
// 결제 끝난 행은 편집기가 열리지 않고 삭제도 막혀요.
export const options = {
  columns: [
    { id: 'id', title: '번호', kind: 'number', width: 70 },
    { id: 'title', title: '이름', width: 140, editable: true },
    { id: 'status', title: '상태', width: 90 },
    { id: 'amount', title: '금액', kind: 'number', width: 110, editable: true },
  ],
  rows,
  rowKey: 'id',
  hooks: {
    beforeEdit: ({ rowKey }) => {
      const paid = isPaid(rowKey);
      if (paid)
        log({
          ko: `${rowKey}번은 결제가 끝나 편집할 수 없어요`,
          en: `Row ${rowKey} is paid and cannot be edited`,
        });
      return !paid;
    },
    beforeRemoveRows: ({ keys }) => {
      const blocked = keys.filter(isPaid);
      if (blocked.length > 0)
        log({
          ko: `결제 끝난 행(${blocked.join(', ')})은 지울 수 없어요`,
          en: `Paid rows (${blocked.join(', ')}) cannot be removed`,
        });
      return blocked.length === 0;
    },
  },
};
export function setup(api, write) {
  log = write;
  const off = api.on('editStarted', (e) => write(`editStarted: ${e.rowKey}/${e.columnId}`));
  return () => {
    off();
    log = () => undefined;
  };
}
export const actions = {
  editPaid: (api) => api.startEdit(2, 'title'),
  editOpen: (api) => api.startEdit(1, 'title'),
  removePaid: (api) => api.removeRows([2]),
  removeOpen: (api) => api.removeRows([3]),
};

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="editPaid">Edit paid row</button>
    <button type="button" id="editOpen">Edit open row</button>
    <button type="button" id="removePaid">Remove paid row</button>
    <button type="button" id="removeOpen">Remove open row</button>
  </div>
  <div id="grid" style="height: 230px"></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('editPaid').addEventListener('click', () => actions.editPaid(api));
document.getElementById('editOpen').addEventListener('click', () => actions.editOpen(api));
document.getElementById('removePaid').addEventListener('click', () => actions.removePaid(api));
document.getElementById('removeOpen').addEventListener('click', () => actions.removeOpen(api));
example.js
const rows = [
  { id: 1, title: 'Order A', status: 'open', amount: 12 },
  { id: 2, title: 'Order B', status: 'paid', amount: 34 },
  { id: 3, title: 'Order C', status: 'open', amount: 6 },
  { id: 4, title: 'Order D', status: 'paid', amount: 78 },
];
let log = () => undefined;
const isPaid = (key) => rows.find((r) => r.id === key)?.status === 'paid';
// hooks are called before the thing happens. If one returns false, it does not happen.
// For paid rows the editor does not open and removal is blocked too.
export const options = {
  columns: [
    { id: 'id', title: 'No.', kind: 'number', width: 70 },
    { id: 'title', title: 'Name', width: 140, editable: true },
    { id: 'status', title: 'Status', width: 90 },
    { id: 'amount', title: 'Amount', kind: 'number', width: 110, editable: true },
  ],
  rows,
  rowKey: 'id',
  hooks: {
    beforeEdit: ({ rowKey }) => {
      const paid = isPaid(rowKey);
      if (paid)
        log(`Row ${rowKey} is paid and cannot be edited`);
      return !paid;
    },
    beforeRemoveRows: ({ keys }) => {
      const blocked = keys.filter(isPaid);
      if (blocked.length > 0)
        log(`Paid rows (${blocked.join(', ')}) cannot be removed`);
      return blocked.length === 0;
    },
  },
};
export function setup(api, write) {
  log = write;
  const off = api.on('editStarted', (e) => write(`editStarted: ${e.rowKey}/${e.columnId}`));
  return () => {
    off();
    log = () => undefined;
  };
}
export const actions = {
  editPaid: (api) => api.startEdit(2, 'title'),
  editOpen: (api) => api.startEdit(1, 'title'),
  removePaid: (api) => api.removeRows([2]),
  removeOpen: (api) => api.removeRows([3]),
};