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

우클릭 이벤트 받고 가로채기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.

활용Intermediate
직접 써 보기Live preview우클릭 이벤트 받고 가로채기Receive and intercept right clicks

코드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="none">맞는 행 없음</button>
    <button type="button" id="all">전체 보기</button>
  </div>
  <div id="grid" style="height: 300px"></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('none').addEventListener('click', () => actions.none(api));
document.getElementById('all').addEventListener('click', () => actions.all(api));
example.js
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),
};

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="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>
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('none').addEventListener('click', () => actions.none(api));
document.getElementById('all').addEventListener('click', () => actions.all(api));
example.js
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),
};