- Data Grid docs
- Rows and records
Context menu
Right-click a cell for copy, paste, filter by its value, sort, group, chart, find, pin, move and remove, and add your own commands or take the grid's away.
The context menu puts the commands for one cell where the user is looking: copy it, show only records with its value, sort by its column, pin its row, open its record. It opens with a right-click, the Menu key or Shift+F10, and your app can add its own commands to it, change the grid's, or turn it off.
Open the live example and right-click a cell
In the grid
Right-click a cell, or focus it and press the Menu key or Shift+F10. A right-click outside the selected range moves the range to that cell first, as a spreadsheet does. Escape closes the menu and focus returns to the cell.
| Item | Shows | Does |
|---|---|---|
| Copy (Copy cells for a range) | always | Copies the cell or range as tab-separated text, as Ctrl+C |
| Copy with headings | always | The same, with the column headings as the first line |
| Paste | in an editable grid | Pastes the clipboard from this cell, as Ctrl+V |
| Fill down | in an editable grid on local records | Copies the range's first record into the records below it, as Ctrl+D; needs a range of two rows or more |
| Show only "value", Hide "value" | always | Adds a filter that keeps only, or hides, the records with this cell's value (Show only blanks, Hide blanks for an empty cell) |
| Sort ascending, Sort descending | always | Sorts by the cell's column; the current direction is ticked |
| Group by | when the records aren't a tree | Adds the cell's column as a group level |
| Chart the selected cells… | for a range of more than one cell | Opens the range chart |
| Find… | on local records | Opens Find with the cell's text |
| Pin row to top, Pin row to bottom, or Unpin row | when the grid can pin rows | Keeps the record above or below the scrolling ones, or lets it go |
| Move up, Move down | with rowDrag: true, no sort, groups or tree, for a record that isn't pinned | Moves the record one place, as Alt+Up and Alt+Down do |
| Expand details (Collapse details) | with a detailRenderer | Opens or closes the record's detail panel |
| Record details | always | Opens the record dialog |
| Remove record (Remove records for a range) | in an editable grid on local records | Removes the record, or the records of the range |
| Export… | when the grid can export | Opens the export dialog |
Separators split the menu into copy and paste, filter and sort, view, and record commands.
From code
| Option or method | Does |
|---|---|
contextMenu: false | Turns the menu off: right-click shows the browser's own menu |
contextMenu: (items, context) => items | Gets the grid's items and the place the menu opened, and returns the items to show |
openContextMenu(id, field) | Opens a cell's menu under the cell; resolves false when the menu is off or empty |
copyRange({ headers: true }) | Copies the range with the headings first, as Copy with headings, and resolves with the text |
context is { id, field, value, text, range }: the record's ID, the cell's field, its value and display text, and the selected range (TableRange, or null). Each item is { id, label, action, icon?, disabled?, checked?, shortcut? }, or { separator: true }:
idnames the grid's own items:copy,copyWithHeaders,paste,fillDown,filterByValue,excludeValue,sortAscending,sortDescending,groupBy,chart,find,pinRowTop,pinRowBottom,unpinRow,moveUp,moveDown,expand,details,removeRecordandexport. Your items can leave it out.actionruns when the item is chosen. An error it throws, or a promise it rejects with, shows in the grid's status line and fireserror.iconnames one of the grid's icons, such ascopy,paste,filter,chart,search,pin,details,exportorremove; an unknown name leaves the space empty.disabledgreys the item out,checkedticks it, andshortcutis the text shown at the right, such as'Ctrl+E'. The shortcut is a label only: the grid doesn't bind the key.
The function gets copies of the grid's items, so it can filter, reorder or change them. Returning something other than a list keeps the grid's items; a function that throws does too, and the error goes to the console. Separators at the ends and next to each other are dropped, and an empty list opens no menu.
JavaScript
import { mountTable } from '@8sheets/data-grid';
const grid = await mountTable(document.querySelector('#orders'), {
title: 'Orders',
data: orders,
contextMenu: (items, context) => [
// Your command first, for the record under the pointer.
{ label: 'Open order ' + context.id, icon: 'details', action: () => router.go(`/orders/${context.id}`) },
{ separator: true },
// The grid's own items, without export and grouping.
...items.filter((item) => item.id !== 'export' && item.id !== 'groupBy'),
],
});
// Open the menu of the focused cell from your own button.
menuButton.onclick = () => {
const cell = grid.getFocusedCell();
if (cell) grid.openContextMenu(cell.id, cell.field);
};React
import { useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
import { DataGrid } from '@8sheets/data-grid-react';
export function Orders({ orders }) {
const navigate = useNavigate();
const options = useMemo(() => ({
title: 'Orders',
data: orders,
contextMenu: (items, context) => [
{ label: 'Open order ' + context.id, icon: 'details', action: () => navigate(`/orders/${context.id}`) },
{ separator: true },
...items.filter((item) => item.id !== 'export'),
],
}), [orders, navigate]);
return <div style={{ height: 480 }}><DataGrid options={options} /></div>;
}Vue
<script setup>
import { computed } from 'vue';
import { useRouter } from 'vue-router';
import { DataGrid } from '@8sheets/data-grid-vue';
const props = defineProps({ orders: Array });
const router = useRouter();
const options = computed(() => ({
title: 'Orders',
data: props.orders,
contextMenu: (items, context) => [
{ label: 'Open order ' + context.id, icon: 'details', action: () => router.push(`/orders/${context.id}`) },
{ separator: true },
...items.filter((item) => item.id !== 'export'),
],
}));
</script>
<template>
<div style="height: 480px"><DataGrid :options="options" /></div>
</template>Angular
import { Component, Input, inject } from '@angular/core';
import { Router } from '@angular/router';
import { DataGridComponent, DataGridOptions } from '@8sheets/data-grid-angular';
@Component({
selector: 'app-orders',
imports: [DataGridComponent],
template: `<eight-data-grid [options]="options" style="height: 480px" />`,
})
export class Orders {
private router = inject(Router);
options: DataGridOptions = {};
@Input() set orders(data: Record<string, unknown>[]) {
this.options = {
title: 'Orders',
data,
contextMenu: (items, context) => [
{ label: 'Open order ' + context.id, icon: 'details', action: () => this.router.navigate(['/orders', context.id]) },
{ separator: true },
...items.filter((item) => item.id !== 'export'),
],
};
}
}contextMenu is live: setOptions({ contextMenu: false }) turns the menu off without a new mount.
Limits
- The menu opens on record cells. Headings keep their own column menu.
- The grid's items act on one cell, its range or its record.
- Copy, Copy with headings and Paste need the browser's clipboard permission.
- Your items'
shortcutis a label; bind the keys yourself if you want them to work.