훅으로 편집과 삭제 막기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.
코드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="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>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));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]),
};<!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="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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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를 돌려주면 그 일은 일어나지 않아요.
// 결제 끝난 행은 편집기가 열리지 않고 삭제도 막혀요.
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;
},
},
};
function setup(api, write) {
log = write;
const off = api.on('editStarted', (e) => write(`editStarted: ${e.rowKey}/${e.columnId}`));
return () => {
off();
log = () => undefined;
};
}
const actions = {
editPaid: (api) => api.startEdit(2, 'title'),
editOpen: (api) => api.startEdit(1, 'title'),
removePaid: (api) => api.removeRows([2]),
removeOpen: (api) => api.removeRows([3]),
};
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('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));
</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.editPaid(api)}
>결제된 행 편집</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.editOpen(api)}
>열린 행 편집</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.removePaid(api)}
>결제된 행 삭제</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.removeOpen(api)}
>열린 행 삭제</button>
</div>
<Grid style={{ height: 230 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
title: string;
status: 'open' | 'paid';
amount: number;
}
const rows: Row[] = [
{ 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: (message: LogText) => void = () => undefined;
const isPaid = (key: unknown): boolean => rows.find((r) => r.id === key)?.status === 'paid';
// hooks는 일이 일어나기 전에 불려요. false를 돌려주면 그 일은 일어나지 않아요.
// 결제 끝난 행은 편집기가 열리지 않고 삭제도 막혀요.
export const options: GridOptions<Row> = {
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: GridApi<Row>, write: (message: LogText) => void) {
log = write;
const off = api.on('editStarted', (e) => write(`editStarted: ${e.rowKey}/${e.columnId}`));
return () => {
off();
log = () => undefined;
};
}
export const actions = {
editPaid: (api: GridApi<Row>) => api.startEdit(2, 'title'),
editOpen: (api: GridApi<Row>) => api.startEdit(1, 'title'),
removePaid: (api: GridApi<Row>) => api.removeRows([2]),
removeOpen: (api: GridApi<Row>) => api.removeRows([3]),
};버튼을 누르면 이 예제의 코드가 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.editPaid(api)"
>결제된 행 편집</button>
<button
type="button"
:disabled="!api"
@click="api && actions.editOpen(api)"
>열린 행 편집</button>
<button
type="button"
:disabled="!api"
@click="api && actions.removePaid(api)"
>결제된 행 삭제</button>
<button
type="button"
:disabled="!api"
@click="api && actions.removeOpen(api)"
>열린 행 삭제</button>
</div>
<Grid style="height: 230px" :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;
title: string;
status: 'open' | 'paid';
amount: number;
}
const rows: Row[] = [
{ 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: (message: LogText) => void = () => undefined;
const isPaid = (key: unknown): boolean => rows.find((r) => r.id === key)?.status === 'paid';
// hooks는 일이 일어나기 전에 불려요. false를 돌려주면 그 일은 일어나지 않아요.
// 결제 끝난 행은 편집기가 열리지 않고 삭제도 막혀요.
export const options: GridOptions<Row> = {
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: GridApi<Row>, write: (message: LogText) => void) {
log = write;
const off = api.on('editStarted', (e) => write(`editStarted: ${e.rowKey}/${e.columnId}`));
return () => {
off();
log = () => undefined;
};
}
export const actions = {
editPaid: (api: GridApi<Row>) => api.startEdit(2, 'title'),
editOpen: (api: GridApi<Row>) => api.startEdit(1, 'title'),
removePaid: (api: GridApi<Row>) => api.removeRows([2]),
removeOpen: (api: GridApi<Row>) => api.removeRows([3]),
};버튼을 누르면 이 예제의 코드가 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.editPaid(api)"
>결제된 행 편집</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.editOpen(api)"
>열린 행 편집</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.removePaid(api)"
>결제된 행 삭제</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.removeOpen(api)"
>열린 행 삭제</button>
</div>
<mx-grid style="height: 230px" [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;
title: string;
status: 'open' | 'paid';
amount: number;
}
const rows: Row[] = [
{ 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: (message: LogText) => void = () => undefined;
const isPaid = (key: unknown): boolean => rows.find((r) => r.id === key)?.status === 'paid';
// hooks는 일이 일어나기 전에 불려요. false를 돌려주면 그 일은 일어나지 않아요.
// 결제 끝난 행은 편집기가 열리지 않고 삭제도 막혀요.
export const options: GridOptions<Row> = {
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: GridApi<Row>, write: (message: LogText) => void) {
log = write;
const off = api.on('editStarted', (e) => write(`editStarted: ${e.rowKey}/${e.columnId}`));
return () => {
off();
log = () => undefined;
};
}
export const actions = {
editPaid: (api: GridApi<Row>) => api.startEdit(2, 'title'),
editOpen: (api: GridApi<Row>) => api.startEdit(1, 'title'),
removePaid: (api: GridApi<Row>) => api.removeRows([2]),
removeOpen: (api: GridApi<Row>) => api.removeRows([3]),
};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="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>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));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]),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Block edits and removals with hooks</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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.
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;
},
},
};
function setup(api, write) {
log = write;
const off = api.on('editStarted', (e) => write(`editStarted: ${e.rowKey}/${e.columnId}`));
return () => {
off();
log = () => undefined;
};
}
const actions = {
editPaid: (api) => api.startEdit(2, 'title'),
editOpen: (api) => api.startEdit(1, 'title'),
removePaid: (api) => api.removeRows([2]),
removeOpen: (api) => api.removeRows([3]),
};
options.ariaLabel = "Block edits and removals with hooks";
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('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));
</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.editPaid(api)}
>Edit paid row</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.editOpen(api)}
>Edit open row</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.removePaid(api)}
>Remove paid row</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.removeOpen(api)}
>Remove open row</button>
</div>
<Grid style={{ height: 230 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
title: string;
status: 'open' | 'paid';
amount: number;
}
const rows: Row[] = [
{ 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: (message: LogText) => void = () => undefined;
const isPaid = (key: unknown): boolean => 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: GridOptions<Row> = {
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: GridApi<Row>, write: (message: LogText) => void) {
log = write;
const off = api.on('editStarted', (e) => write(`editStarted: ${e.rowKey}/${e.columnId}`));
return () => {
off();
log = () => undefined;
};
}
export const actions = {
editPaid: (api: GridApi<Row>) => api.startEdit(2, 'title'),
editOpen: (api: GridApi<Row>) => api.startEdit(1, 'title'),
removePaid: (api: GridApi<Row>) => api.removeRows([2]),
removeOpen: (api: GridApi<Row>) => api.removeRows([3]),
};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.editPaid(api)"
>Edit paid row</button>
<button
type="button"
:disabled="!api"
@click="api && actions.editOpen(api)"
>Edit open row</button>
<button
type="button"
:disabled="!api"
@click="api && actions.removePaid(api)"
>Remove paid row</button>
<button
type="button"
:disabled="!api"
@click="api && actions.removeOpen(api)"
>Remove open row</button>
</div>
<Grid style="height: 230px" :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;
title: string;
status: 'open' | 'paid';
amount: number;
}
const rows: Row[] = [
{ 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: (message: LogText) => void = () => undefined;
const isPaid = (key: unknown): boolean => 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: GridOptions<Row> = {
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: GridApi<Row>, write: (message: LogText) => void) {
log = write;
const off = api.on('editStarted', (e) => write(`editStarted: ${e.rowKey}/${e.columnId}`));
return () => {
off();
log = () => undefined;
};
}
export const actions = {
editPaid: (api: GridApi<Row>) => api.startEdit(2, 'title'),
editOpen: (api: GridApi<Row>) => api.startEdit(1, 'title'),
removePaid: (api: GridApi<Row>) => api.removeRows([2]),
removeOpen: (api: GridApi<Row>) => api.removeRows([3]),
};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.editPaid(api)"
>Edit paid row</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.editOpen(api)"
>Edit open row</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.removePaid(api)"
>Remove paid row</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.removeOpen(api)"
>Remove open row</button>
</div>
<mx-grid style="height: 230px" [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;
title: string;
status: 'open' | 'paid';
amount: number;
}
const rows: Row[] = [
{ 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: (message: LogText) => void = () => undefined;
const isPaid = (key: unknown): boolean => 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: GridOptions<Row> = {
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: GridApi<Row>, write: (message: LogText) => void) {
log = write;
const off = api.on('editStarted', (e) => write(`editStarted: ${e.rowKey}/${e.columnId}`));
return () => {
off();
log = () => undefined;
};
}
export const actions = {
editPaid: (api: GridApi<Row>) => api.startEdit(2, 'title'),
editOpen: (api: GridApi<Row>) => api.startEdit(1, 'title'),
removePaid: (api: GridApi<Row>) => api.removeRows([2]),
removeOpen: (api: GridApi<Row>) => api.removeRows([3]),
};