Cell context menu
Right-click a cell and the actions available for that cell appear as a menu. With the keyboard, select a cell and press Shift+F10 (or the ContextMenu key). The menu is on by default.
It has cut, copy, copy with headers, paste, note actions and export. Try it in the gallery example Cell context menu.
Default items
Item (id) | What it does |
|---|---|
Cut (cut) | Copies the selected cells to the clipboard and clears them. Disabled if not editable |
Copy (copy) | Copies the selected cells as text split by tabs and line breaks |
Copy with headers (copy-headers) | Adds the column titles as the first line |
| Copy with group headers | Also adds the column group titles. Shown only when the grid has header groups |
Paste (paste) | Fills cells from the clipboard. Disabled if the cell is not editable or reading is blocked |
| Add, edit or remove note | Attaches a note to the cell (Cell notes) |
Export (export) | Downloads CSV, Excel or PDF. Shown only on a grid that has export methods attached |
How copy, cut and paste behave is covered in Copy and paste. The export item appears only on a grid where you called withExport(api) (CSV, Excel and PDF export). Without it the item is not there at all.
Turn it off
cellMenu is on by default. Pass false and right-click goes back to the browser menu, and the note shortcut (Shift+F2) turns off too. The copy and paste keys keep working regardless of this option.
createGrid(el, { columns, rows, rowKey: 'id', cellMenu: false });Add and remove items
Pass an object and use the items callback. It receives the default items and returns the list to show. It runs every time the menu opens, and its second argument says which cell the menu opened on (rowKey, columnId, row, api).
createGrid(el, {
columns,
rows,
rowKey: 'id',
cellMenu: {
items: (defaults, { row, api }) => [
...defaults.filter((item) => item.id !== 'export'), // hide export
{ id: 'sep-mine', label: '', separator: true },
{
id: 'open',
label: `Open details for ${row.name}`,
run: () => openDetail(row.id),
},
],
},
});An item has these fields.
| Field | Meaning |
|---|---|
id | The item name. Use it to pick out default items |
label | The visible text |
shortcut | Dimmed key hint on the right. It only displays text |
disabled | If true, the item cannot be chosen |
separator | If true, only a divider line is drawn there |
children | If present, a submenu opens on the right |
run | Function called when chosen. The menu closes first |
If the returned list is empty the menu does not open. The default id values are cut, copy, copy-headers, copy-group-headers, paste, note-add, note-edit, note-remove and export.
Export file name
The export items download files named like grid.csv. Change the base name with exportFilename, without an extension.
cellMenu: { exportFilename: 'employees' }, // employees.csv, employees.xlsx, employees.pdfRight-click and long-press events, and intercepting them
Before a menu opens, contextMenuRequested { zone, rowKey, columnId, x, y, event } fires once (1.3.6). Then beforeContextMenu decides whether the built-in menu opens, so a vetoed request is still visible as an event. zone is cell, header, rownum or rownum-header. When a long press opens it, event is null. It fires even with cellMenu: false, so you can attach your own menu.
hooks: {
// false: no built-in menu and the browser menu is left alone
beforeContextMenu: ({ zone, columnId }) => (zone === 'header' && columnId === 'id' ? false : undefined),
},
cellMenu: { allowWithCtrl: false }, // Ctrl+right-click shows the browser menu| Event | Value | When |
|---|---|---|
cellMenuOpened, cellMenuClosed | { rowKey, columnId } | The cell menu opens and closes |
columnMenuOpened, columnMenuClosed | { columnId } | The column menu opens and closes |
cellLongPressed | { rowKey, columnId, x, y } | A cell is held by touch for 400 ms |
cellMouseOver, cellMouseOut, cellMouseDown | { rowKey, columnId, event } | The mouse enters, leaves or presses a cell |
headerMouseOver, headerMouseOut | { columnId, event } | The mouse enters or leaves a header |
Mouse events attach their DOM listeners only while someone subscribes, so they cost nothing otherwise (onAny subscribes to everything, so it attaches them). A long press is cancelled when the finger moves more than a little (10 px) or the grid scrolls. A long press opens the cell menu at that spot.
Keyboard use
| Key | Action |
|---|---|
| Shift+F10 or ContextMenu | Opens the menu below the focused cell |
| Up, Down, Home, End | Moves between items, wrapping at the ends |
| Right | Opens a submenu. Left closes it |
| Enter or Space | Runs the highlighted item |
| Esc | Closes the open submenu, or the menu itself, and returns to the cell |
| Tab | Closes the menu and lets the browser move focus |
In text inputs and cells being edited these keys are left to the browser. The open menu closes when sorting, filtering, rows or columns change, or when the grid scrolls.
Change the text
Menu text comes from the messages option. Override cellMenuLabel, cellCut, cellCopy, cellCopyHeaders, cellCopyGroupHeaders, cellPaste, cellNoteAdd, cellNoteEdit, cellNoteRemove, cellExport, cellExportCsv, cellExportXlsx or cellExportPdf. Keys you leave out use the locale defaults.
messages: { cellCopy: 'Copy cells', cellPaste: 'Paste here' },If the browser blocks reading the clipboard, the menu's paste cannot work and a short notice appears under the grid. Its text is cellPasteBlocked. In that case select a cell and press Ctrl+V.