It is not only a fully functional graph visualization library, but also an explorer of data relationships.
!!!###!!!title=context menu plugin——VisActor/VTable tutorial documents!!!###!!!
ContextMenu
Introduction
ContextMenu provides rich right-click menu features for VTable tables, supporting customization of right-click menus in different areas of the table (header, body, row series number, column series number, corner). It can effectively improve the interactive experience and operation efficiency of the table. Through this plugin, users can quickly perform common operations such as copying, pasting, inserting and deleting rows and columns, and freezing rows and columns.
Features
Support customization of right-click menus in different areas of the table (header, body, row series number, column series number, corner)
Built-in common menu operations (copy, paste, insert and delete rows and columns, freeze rows and columns, etc.)
Support custom menu items and separators
Support menu item grouping and submenus
Support custom menu item click callbacks
Support listening to menu item click events with menu item and cell position details
Support menu item icons and shortcut hints
Support numeric input in menu items (e.g., insert multiple rows/columns)
Basic Usage
Here is the basic usage method of the ContextMenu plugin:
import * as VTable from '@visactor/vtable';
import { ContextMenuPlugin } from '@visactor/vtable-plugins';
// 创建表格实例
const tableInstance = new VTable.ListTable({
container: document.getElementById('container'),
columns: [
{ field: 'name', title: '姓名' },
{ field: 'age', title: '年龄' },
{ field: 'gender', title: '性别' }
],
records: [
{ name: '张三', age: 20, gender: '男' },
{ name: '李四', age: 25, gender: '男' },
{ name: '王五', age: 22, gender: '女' },
]
});
// Register and enable the right-click menu plugin
tableInstance.registerPlugin(
new ContextMenuPlugin() // Use default menu items
);
Customize Menu Items
The default menu items are defined in @visactor/vtable-plugins, you can refer to ContextMenuPlugin for details.
The ContextMenu plugin allows you to customize right-click menu items in different areas:
The ContextMenu plugin supports dynamically adjusting menu items before they are displayed, which is very useful in some scenarios:
import * as VTable from '@visactor/vtable';
import { ContextMenuPlugin } from '@visactor/vtable-plugins';
// Create table instance (code omitted)...
// Initialize the right-click menu plugin
const contextMenuPlugin = new ContextMenuPlugin({
// Use default menu items
// Dynamically adjust menu items before they are displayed
beforeShowAdjustMenuItems: (menuItems, table, col, row) => {
// Dynamically adjust menu items based on cell position, content, etc.
if (table.isHeader(col, row)) {
// Additional processing in the header area
return [
...menuItems,
'---',
{ text: '自定义表头操作', menuKey: 'custom_header_action' }
];
}
// Get cell data
const cellValue = table.getCellValue(col, row);
// Dynamically add menu items based on cell values
if (typeof cellValue === 'number' && cellValue > 100) {
return [
...menuItems,
'---',
{ text: '高亮大数值', menuKey: 'highlight_large_value' }
];
}
return menuItems;
}
});
Listen to Menu Item Click Events
If you need to observe user actions after a right-click menu item is clicked, listen to the context_menu_click event fired by ContextMenuPlugin. This event is only fired after the right-click menu plugin is installed and enabled, and it is fired after the corresponding menu item handling logic is called. Note that when menuClickCallback is configured as a function, the plugin runs that callback and skips the built-in menu handling. Built-in operations such as freeze, insert, and delete are only executed when menuClickCallback is not configured, or when it is configured as an object and the current menuKey does not match a custom callback.
import * as VTable from '@visactor/vtable';
import { ContextMenuPlugin } from '@visactor/vtable-plugins';
const contextMenuPlugin = new ContextMenuPlugin();
const tableInstance = new VTable.ListTable({
container: document.getElementById('container'),
columns,
records,
plugins: [contextMenuPlugin]
});
tableInstance.on(VTable.TABLE_EVENT_TYPE.CONTEXT_MENU_CLICK, args => {
const { col, row, contextMenu } = args;
console.log('Context menu click position:', col, row);
console.log('Menu item:', contextMenu.menuKey, contextMenu.menuText);
if (contextMenu.menuKey === 'freeze_to_this_row_and_column') {
console.log('Current frozen counts:', tableInstance.frozenRowCount, tableInstance.frozenColCount);
}
});
Event argument description:
Parameter
Type
Description
col
number
Column index of the menu trigger position; -1 when there is no corresponding cell
row
number
Row index of the menu trigger position; -1 when there is no corresponding cell
contextMenu.menuKey
string
Unique key of the clicked menu item
contextMenu.menuText
string
Display text of the clicked menu item
contextMenu.rowIndex
number | undefined
Row index of the menu trigger position; may be undefined when no row context exists
contextMenu.colIndex
number | undefined
Column index of the menu trigger position; may be undefined when no column context exists
contextMenu.cellValue
any | undefined
Cell value at the menu trigger position; undefined when there is no corresponding cell
contextMenu.inputValue
number | string | undefined
Input value of an input menu item; only exists when an input menu item is submitted
Complete Example
Here is a complete example showing how to create a table with right-click menu functionality: