우클릭 이벤트 받고 가로채기Receive and intercept right clicks
contextMenuRequested가 우클릭한 자리(zone)를 메뉴가 열리기 전에 알려요. hooks.beforeContextMenu가 false면 내장 메뉴를 막고, 행 번호 칸 클릭에는 zone이 붙어요.contextMenuRequested reports the zone of a right click before the menu opens. hooks.beforeContextMenu returning false blocks the built-in menu, and clicks on row numbers carry a zone.
코드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="none">맞는 행 없음</button>
<button type="button" id="all">전체 보기</button>
</div>
<div id="grid" style="height: 300px"></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('none').addEventListener('click', () => actions.none(api));
document.getElementById('all').addEventListener('click', () => actions.all(api));const rows = Array.from({ length: 8 }, (_, i) => ({
id: i + 1,
name: `이름 ${i + 1}`,
score: (i * 9) % 40,
}));
// 우클릭은 contextMenuRequested로 먼저 알려요. beforeContextMenu가 false를 돌려주면 번호 칸의 내장 메뉴를 막아요(브라우저 메뉴가 떠요).
export const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80 },
{ id: 'name', title: '이름', width: 140 },
{ id: 'score', title: '점수', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
showRowNumbers: true,
columnMenu: true,
cellMenu: { allowWithCtrl: false },
hooks: {
beforeContextMenu: ({ zone, columnId }) => zone === 'cell' && columnId === 'id' ? false : undefined,
},
};
export function setup(api, log) {
const offs = [
api.on('contextMenuRequested', (e) => log({
ko: `우클릭: ${e.zone} 행 ${e.rowKey ?? '-'} 컬럼 ${e.columnId ?? '-'}`,
en: `Right click: ${e.zone} row ${e.rowKey ?? '-'} column ${e.columnId ?? '-'}`,
})),
api.on('cellClicked', (e) => log({ ko: `칸 클릭: ${e.zone ?? 'cell'}`, en: `Cell click: ${e.zone ?? 'cell'}` })),
api.on('noMatches', () => log({ ko: '맞는 행이 없어요', en: 'No rows match' })),
];
return () => offs.forEach((off) => off());
}
export const actions = {
none: (api) => api.setFilterModel({
match: 'all',
items: [{ columnId: 'name', operator: 'equals', value: '없는 이름' }],
}),
all: (api) => api.setFilterModel(null),
};<!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="none">맞는 행 없음</button>
<button type="button" id="all">전체 보기</button>
</div>
<div id="grid" style="height: 300px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const rows = Array.from({ length: 8 }, (_, i) => ({
id: i + 1,
name: `이름 ${i + 1}`,
score: (i * 9) % 40,
}));
// 우클릭은 contextMenuRequested로 먼저 알려요. beforeContextMenu가 false를 돌려주면 번호 칸의 내장 메뉴를 막아요(브라우저 메뉴가 떠요).
const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80 },
{ id: 'name', title: '이름', width: 140 },
{ id: 'score', title: '점수', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
showRowNumbers: true,
columnMenu: true,
cellMenu: { allowWithCtrl: false },
hooks: {
beforeContextMenu: ({ zone, columnId }) => zone === 'cell' && columnId === 'id' ? false : undefined,
},
};
function setup(api, log) {
const offs = [
api.on('contextMenuRequested', (e) => log({
ko: `우클릭: ${e.zone} 행 ${e.rowKey ?? '-'} 컬럼 ${e.columnId ?? '-'}`,
en: `Right click: ${e.zone} row ${e.rowKey ?? '-'} column ${e.columnId ?? '-'}`,
})),
api.on('cellClicked', (e) => log({ ko: `칸 클릭: ${e.zone ?? 'cell'}`, en: `Cell click: ${e.zone ?? 'cell'}` })),
api.on('noMatches', () => log({ ko: '맞는 행이 없어요', en: 'No rows match' })),
];
return () => offs.forEach((off) => off());
}
const actions = {
none: (api) => api.setFilterModel({
match: 'all',
items: [{ columnId: 'name', operator: 'equals', value: '없는 이름' }],
}),
all: (api) => api.setFilterModel(null),
};
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('none').addEventListener('click', () => actions.none(api));
document.getElementById('all').addEventListener('click', () => actions.all(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.none(api)}
>맞는 행 없음</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.all(api)}
>전체 보기</button>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
score: number;
}
const rows: Row[] = Array.from({ length: 8 }, (_, i) => ({
id: i + 1,
name: `이름 ${i + 1}`,
score: (i * 9) % 40,
}));
// 우클릭은 contextMenuRequested로 먼저 알려요. beforeContextMenu가 false를 돌려주면 번호 칸의 내장 메뉴를 막아요(브라우저 메뉴가 떠요).
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80 },
{ id: 'name', title: '이름', width: 140 },
{ id: 'score', title: '점수', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
showRowNumbers: true,
columnMenu: true,
cellMenu: { allowWithCtrl: false },
hooks: {
beforeContextMenu: ({ zone, columnId }) =>
zone === 'cell' && columnId === 'id' ? false : undefined,
},
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const offs = [
api.on('contextMenuRequested', (e) =>
log({
ko: `우클릭: ${e.zone} 행 ${e.rowKey ?? '-'} 컬럼 ${e.columnId ?? '-'}`,
en: `Right click: ${e.zone} row ${e.rowKey ?? '-'} column ${e.columnId ?? '-'}`,
}),
),
api.on('cellClicked', (e) =>
log({ ko: `칸 클릭: ${e.zone ?? 'cell'}`, en: `Cell click: ${e.zone ?? 'cell'}` }),
),
api.on('noMatches', () => log({ ko: '맞는 행이 없어요', en: 'No rows match' })),
];
return () => offs.forEach((off) => off());
}
export const actions = {
none: (api: GridApi<Row>) =>
api.setFilterModel({
match: 'all',
items: [{ columnId: 'name', operator: 'equals', value: '없는 이름' }],
}),
all: (api: GridApi<Row>) => api.setFilterModel(null),
};버튼을 누르면 이 예제의 코드가 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.none(api)">맞는 행 없음</button>
<button type="button" :disabled="!api" @click="api && actions.all(api)">전체 보기</button>
</div>
<Grid style="height: 300px" :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;
name: string;
score: number;
}
const rows: Row[] = Array.from({ length: 8 }, (_, i) => ({
id: i + 1,
name: `이름 ${i + 1}`,
score: (i * 9) % 40,
}));
// 우클릭은 contextMenuRequested로 먼저 알려요. beforeContextMenu가 false를 돌려주면 번호 칸의 내장 메뉴를 막아요(브라우저 메뉴가 떠요).
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80 },
{ id: 'name', title: '이름', width: 140 },
{ id: 'score', title: '점수', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
showRowNumbers: true,
columnMenu: true,
cellMenu: { allowWithCtrl: false },
hooks: {
beforeContextMenu: ({ zone, columnId }) =>
zone === 'cell' && columnId === 'id' ? false : undefined,
},
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const offs = [
api.on('contextMenuRequested', (e) =>
log({
ko: `우클릭: ${e.zone} 행 ${e.rowKey ?? '-'} 컬럼 ${e.columnId ?? '-'}`,
en: `Right click: ${e.zone} row ${e.rowKey ?? '-'} column ${e.columnId ?? '-'}`,
}),
),
api.on('cellClicked', (e) =>
log({ ko: `칸 클릭: ${e.zone ?? 'cell'}`, en: `Cell click: ${e.zone ?? 'cell'}` }),
),
api.on('noMatches', () => log({ ko: '맞는 행이 없어요', en: 'No rows match' })),
];
return () => offs.forEach((off) => off());
}
export const actions = {
none: (api: GridApi<Row>) =>
api.setFilterModel({
match: 'all',
items: [{ columnId: 'name', operator: 'equals', value: '없는 이름' }],
}),
all: (api: GridApi<Row>) => api.setFilterModel(null),
};버튼을 누르면 이 예제의 코드가 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.none(api)"
>맞는 행 없음</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.all(api)"
>전체 보기</button>
</div>
<mx-grid style="height: 300px" [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;
name: string;
score: number;
}
const rows: Row[] = Array.from({ length: 8 }, (_, i) => ({
id: i + 1,
name: `이름 ${i + 1}`,
score: (i * 9) % 40,
}));
// 우클릭은 contextMenuRequested로 먼저 알려요. beforeContextMenu가 false를 돌려주면 번호 칸의 내장 메뉴를 막아요(브라우저 메뉴가 떠요).
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80 },
{ id: 'name', title: '이름', width: 140 },
{ id: 'score', title: '점수', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
showRowNumbers: true,
columnMenu: true,
cellMenu: { allowWithCtrl: false },
hooks: {
beforeContextMenu: ({ zone, columnId }) =>
zone === 'cell' && columnId === 'id' ? false : undefined,
},
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
const offs = [
api.on('contextMenuRequested', (e) =>
log({
ko: `우클릭: ${e.zone} 행 ${e.rowKey ?? '-'} 컬럼 ${e.columnId ?? '-'}`,
en: `Right click: ${e.zone} row ${e.rowKey ?? '-'} column ${e.columnId ?? '-'}`,
}),
),
api.on('cellClicked', (e) =>
log({ ko: `칸 클릭: ${e.zone ?? 'cell'}`, en: `Cell click: ${e.zone ?? 'cell'}` }),
),
api.on('noMatches', () => log({ ko: '맞는 행이 없어요', en: 'No rows match' })),
];
return () => offs.forEach((off) => off());
}
export const actions = {
none: (api: GridApi<Row>) =>
api.setFilterModel({
match: 'all',
items: [{ columnId: 'name', operator: 'equals', value: '없는 이름' }],
}),
all: (api: GridApi<Row>) => api.setFilterModel(null),
};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="none">No match</button>
<button type="button" id="all">Show all</button>
</div>
<div id="grid" style="height: 300px"></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('none').addEventListener('click', () => actions.none(api));
document.getElementById('all').addEventListener('click', () => actions.all(api));const rows = Array.from({ length: 8 }, (_, i) => ({
id: i + 1,
name: `Name ${i + 1}`,
score: (i * 9) % 40,
}));
// A right click is reported first through contextMenuRequested. If beforeContextMenu returns false, the built-in menu is blocked on the ID cell (the browser menu shows instead).
export const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80 },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'score', title: 'Score', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
showRowNumbers: true,
columnMenu: true,
cellMenu: { allowWithCtrl: false },
hooks: {
beforeContextMenu: ({ zone, columnId }) => zone === 'cell' && columnId === 'id' ? false : undefined,
},
};
export function setup(api, log) {
const offs = [
api.on('contextMenuRequested', (e) => log(`Right click: ${e.zone} row ${e.rowKey ?? '-'} column ${e.columnId ?? '-'}`)),
api.on('cellClicked', (e) => log(`Cell click: ${e.zone ?? 'cell'}`)),
api.on('noMatches', () => log('No rows match')),
];
return () => offs.forEach((off) => off());
}
export const actions = {
none: (api) => api.setFilterModel({
match: 'all',
items: [{ columnId: 'name', operator: 'equals', value: 'No such name' }],
}),
all: (api) => api.setFilterModel(null),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Receive and intercept right clicks</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="none">No match</button>
<button type="button" id="all">Show all</button>
</div>
<div id="grid" style="height: 300px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const rows = Array.from({ length: 8 }, (_, i) => ({
id: i + 1,
name: `Name ${i + 1}`,
score: (i * 9) % 40,
}));
// A right click is reported first through contextMenuRequested. If beforeContextMenu returns false, the built-in menu is blocked on the ID cell (the browser menu shows instead).
const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80 },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'score', title: 'Score', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
showRowNumbers: true,
columnMenu: true,
cellMenu: { allowWithCtrl: false },
hooks: {
beforeContextMenu: ({ zone, columnId }) => zone === 'cell' && columnId === 'id' ? false : undefined,
},
};
function setup(api, log) {
const offs = [
api.on('contextMenuRequested', (e) => log(`Right click: ${e.zone} row ${e.rowKey ?? '-'} column ${e.columnId ?? '-'}`)),
api.on('cellClicked', (e) => log(`Cell click: ${e.zone ?? 'cell'}`)),
api.on('noMatches', () => log('No rows match')),
];
return () => offs.forEach((off) => off());
}
const actions = {
none: (api) => api.setFilterModel({
match: 'all',
items: [{ columnId: 'name', operator: 'equals', value: 'No such name' }],
}),
all: (api) => api.setFilterModel(null),
};
options.ariaLabel = "Receive and intercept right clicks";
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('none').addEventListener('click', () => actions.none(api));
document.getElementById('all').addEventListener('click', () => actions.all(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.none(api)}
>No match</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.all(api)}
>Show all</button>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
name: string;
score: number;
}
const rows: Row[] = Array.from({ length: 8 }, (_, i) => ({
id: i + 1,
name: `Name ${i + 1}`,
score: (i * 9) % 40,
}));
// A right click is reported first through contextMenuRequested. If beforeContextMenu returns false, the built-in menu is blocked on the ID cell (the browser menu shows instead).
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80 },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'score', title: 'Score', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
showRowNumbers: true,
columnMenu: true,
cellMenu: { allowWithCtrl: false },
hooks: {
beforeContextMenu: ({ zone, columnId }) =>
zone === 'cell' && columnId === 'id' ? false : undefined,
},
};
export function setup(api: GridApi<Row>, log: (message: string) => void) {
const offs = [
api.on('contextMenuRequested', (e) =>
log(`Right click: ${e.zone} row ${e.rowKey ?? '-'} column ${e.columnId ?? '-'}`),
),
api.on('cellClicked', (e) => log(`Cell click: ${e.zone ?? 'cell'}`)),
api.on('noMatches', () => log('No rows match')),
];
return () => offs.forEach((off) => off());
}
export const actions = {
none: (api: GridApi<Row>) =>
api.setFilterModel({
match: 'all',
items: [{ columnId: 'name', operator: 'equals', value: 'No such name' }],
}),
all: (api: GridApi<Row>) => api.setFilterModel(null),
};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.none(api)">No match</button>
<button type="button" :disabled="!api" @click="api && actions.all(api)">Show all</button>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
name: string;
score: number;
}
const rows: Row[] = Array.from({ length: 8 }, (_, i) => ({
id: i + 1,
name: `Name ${i + 1}`,
score: (i * 9) % 40,
}));
// A right click is reported first through contextMenuRequested. If beforeContextMenu returns false, the built-in menu is blocked on the ID cell (the browser menu shows instead).
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80 },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'score', title: 'Score', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
showRowNumbers: true,
columnMenu: true,
cellMenu: { allowWithCtrl: false },
hooks: {
beforeContextMenu: ({ zone, columnId }) =>
zone === 'cell' && columnId === 'id' ? false : undefined,
},
};
export function setup(api: GridApi<Row>, log: (message: string) => void) {
const offs = [
api.on('contextMenuRequested', (e) =>
log(`Right click: ${e.zone} row ${e.rowKey ?? '-'} column ${e.columnId ?? '-'}`),
),
api.on('cellClicked', (e) => log(`Cell click: ${e.zone ?? 'cell'}`)),
api.on('noMatches', () => log('No rows match')),
];
return () => offs.forEach((off) => off());
}
export const actions = {
none: (api: GridApi<Row>) =>
api.setFilterModel({
match: 'all',
items: [{ columnId: 'name', operator: 'equals', value: 'No such name' }],
}),
all: (api: GridApi<Row>) => api.setFilterModel(null),
};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.none(api)"
>No match</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.all(api)"
>Show all</button>
</div>
<mx-grid style="height: 300px" [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;
export interface Row {
id: number;
name: string;
score: number;
}
const rows: Row[] = Array.from({ length: 8 }, (_, i) => ({
id: i + 1,
name: `Name ${i + 1}`,
score: (i * 9) % 40,
}));
// A right click is reported first through contextMenuRequested. If beforeContextMenu returns false, the built-in menu is blocked on the ID cell (the browser menu shows instead).
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80 },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'score', title: 'Score', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
showRowNumbers: true,
columnMenu: true,
cellMenu: { allowWithCtrl: false },
hooks: {
beforeContextMenu: ({ zone, columnId }) =>
zone === 'cell' && columnId === 'id' ? false : undefined,
},
};
export function setup(api: GridApi<Row>, log: (message: string) => void) {
const offs = [
api.on('contextMenuRequested', (e) =>
log(`Right click: ${e.zone} row ${e.rowKey ?? '-'} column ${e.columnId ?? '-'}`),
),
api.on('cellClicked', (e) => log(`Cell click: ${e.zone ?? 'cell'}`)),
api.on('noMatches', () => log('No rows match')),
];
return () => offs.forEach((off) => off());
}
export const actions = {
none: (api: GridApi<Row>) =>
api.setFilterModel({
match: 'all',
items: [{ columnId: 'name', operator: 'equals', value: 'No such name' }],
}),
all: (api: GridApi<Row>) => api.setFilterModel(null),
};