Row Context Menu
This example demonstrates how to create a custom row context menu with interactive actions. The menu appears when right-clicking on a row header and provides menu items with actions.
Key features:
- Custom row context menu implementation
- Actions with visual indicators
- Styled menu buttons with hover effects
import { defineCustomElements } from '@revolist/revogrid/loader';defineCustomElements();
import { currentTheme, useRandomData } from '../composables/useRandomData';import { rowHeaders, ContextMenuPlugin, RowHeaderPlugin, ColumnStretchPlugin } from '@revolist/revogrid-pro';import type { ContextMenuConfig } from '@/release/plugins/context-menu/context-menu.types';const { createRandomData } = useRandomData();const { isDark } = currentTheme();
const grid = document.querySelector('revo-grid');if (grid) { grid.source = createRandomData(100); // Define columns grid.columns = [ { name: '🆔 ID', prop: 'id', }, { name: '🍎 Fruit', prop: 'name', size: 180, }, { name: '💰 Price', prop: 'price', }, ]; // Define plugin grid.plugins = [RowHeaderPlugin, ContextMenuPlugin, ColumnStretchPlugin]; grid.rowHeaders = rowHeaders({ showHeaderFocusBtn: true });
const rowContextMenu: ContextMenuConfig = { items: [ { name: 'Add row above', action: (_, cell) => { if (!cell) { return; } grid.source.splice(cell.y, 0, { id: 0, name: 'New row', price: 0, }); grid.source = [...grid.source]; }, }, { name: 'Add row below', action: (_, cell) => { if (!cell) { return; } grid.source.splice(cell.y + 1, 0, { id: 0, name: 'New row', price: 0, }); grid.source = [...grid.source]; }, }, { name: (focused, range) => { if (!focused) { return ''; } if (!range) { range = { x: 0, y: focused.y, x1: 0, y1: focused.y, }; } const rows = range.y1 - range.y + 1; if (!range || rows < 2) { return 'Delete row'; } return `Delete ${rows} rows`; }, action: (_, focused, range) => { if (!focused) { return; }
if (!range) { range = { x: 0, y: focused.y, x1: 0, y1: focused.y, }; }
const rows = range.y1 - range.y + 1; grid.source.splice(range.y, rows); grid.source = [...grid.source]; }, } ] }; grid.additionalData = { // Define context menu rowContextMenu, stretch: 'all' };
// Set theme grid.theme = isDark() ? 'darkCompact' : 'compact';}