Loading...
eventTypes)You can enable selection and rubberband with the Selection plugin, for example:
import { Graph, Selection } from '@antv/x6'const graph = new Graph({background: {color: '#F2F7FA',},})graph.use(new Selection({enabled: true,}),)
alt key. Hold down the alt key and press the left mouse button on a blank area of the canvas to drag the selection box to select nodes.| Property Name | Type | Default Value | Required | Description |
|---|---|---|---|---|
| className | string | - | Additional style name for customizing styles | |
| multiple | boolean | true | Whether to enable multi-selection; when enabled, hold down the ctrl or command key to click nodes for multi-selection | |
| multipleSelectionModifiers | ModifierKey | ['ctrl', 'meta'] | Used to set the modifier keys for multi-selection | |
| rubberband | boolean | false | Whether to enable the rubberband selection feature | |
| modifiers | ModifierKey | - | Used to set the modifier keys for rubberband selection | |
| strict | boolean | false | Whether the selection box needs to completely enclose nodes to select them | |
| movable | boolean | true | Whether the selected nodes move together when dragging the selection box | |
| content | string | - | Set additional display content | |
| filter | Filter | - | Node filter | |
| showNodeSelectionBox | boolean | false | Whether to show the selection box for nodes | |
| showEdgeSelectionBox | boolean | false | Whether to show the selection box for edges | |
| pointerEvents | 'none' | 'auto' | auto | When showNodeSelectionBox is enabled, an element overlays the node and its events may not respond; set pointerEvents: none to resolve this. Default is auto. | |
| eventTypes | SelectionEventType[] | ['leftMouseDown', 'mouseWheelDown'] | Used to set the trigger event types for rubberband selection |
The type definition for Filter is as follows:
type Filter = string[] | { id: string }[] | (this: Graph, cell: Cell) => boolean
string[]: An array of node shapes; only specified node/edge shapes can be selected.({ id: string })[]: An array of node IDs; only specified nodes/edges can be selected.(this: Graph, cell: Cell) => boolean: Only nodes/edges that return true can be selected.The type definition for ModifierKey is as follows:
type ModifierKey = string | ('alt' | 'ctrl' | 'meta' | 'shift' | 'space')[] | null
The modifier keys in X6 include alt, ctrl, meta, shift, and space. When modifiers are configured, clicking the mouse while holding a modifier triggers the corresponding behavior. If rubberband selection and canvas panning have identical triggers (same eventTypes and same modifiers), rubberband selection takes precedence and the default panning is disabled; if they differ, they work independently. When both start on leftMouseDown over blank areas, configure different modifiers for selection and panning to avoid conflicts. You can configure a single key (e.g., alt) or multiple keys (e.g., ['alt', 'ctrl']). Multiple keys in an array are treated as OR. For more flexible configurations, use the following forms:
alt indicates pressing alt.[alt, ctrl] indicates pressing alt or ctrl.alt|ctrl indicates pressing alt or ctrl.alt&ctrl indicates pressing alt and ctrl simultaneously.alt|ctrl&shift indicates pressing alt and shift simultaneously or pressing ctrl and shift simultaneously.The type definition for SelectionEventType is as follows:
type SelectionEventType = 'leftMouseDown' | 'mouseWheelDown';
Interaction methods that trigger rubberband selection. Supports two forms or combinations of them:
leftMouseDown: Pressing the left mouse button to drag.mouseWheelDown: Pressing the mouse wheel to drag.select(cells: Cell | string | (Cell | string)[]): this
Selects the specified nodes/edges. Note that this method does not unselect currently selected nodes/edges; it appends the specified nodes/edges to the selection. If you also need to unselect currently selected nodes/edges, please use the resetSelection(...) method.
unselect(cells: Cell | string | (Cell | string)[]): this
Unselects the specified nodes/edges.
isSelected(cell: Cell | string): boolean
Returns whether the specified node/edge is selected.
resetSelection(cells?: Cell | string | (Cell | string)[]): this
Clears the selection first, then selects the provided nodes/edges.
getSelectedCells(): Cell[]
Gets the selected nodes/edges.
cleanSelection(): this
Clears the selection.
isSelectionEmpty(): boolean
Returns whether the selection is empty.
isSelectionEnabled(): boolean
Returns whether selection is enabled.
enableSelection(): this
Enables selection capability.
disableSelection(): this
Disables selection capability.
toggleSelection(enabled?: boolean): this
Toggles the enabled state of selection. Parameters are as follows:
| Name | Type | Required | Default Value | Description |
|---|---|---|---|---|
| enabled | boolean | - | Whether to enable selection capability; defaults to toggling the enabled state. |
isMultipleSelection(): boolean
Returns whether multi-selection is enabled.
enableMultipleSelection(): this
Enables multi-selection.
disableMultipleSelection(): this
Disables multi-selection.
toggleMultipleSelection(multiple?: boolean): this
Toggles the enabled state of multi-selection. Parameters are as follows:
| Name | Type | Required | Default Value | Description |
|---|---|---|---|---|
| multiple | boolean | - | Whether to enable multi-selection; defaults to toggling the enabled state. |
isSelectionMovable(): boolean
Returns whether the selected nodes/edges can be moved.
enableSelectionMovable(): this
Enables moving of selected nodes/edges.
disableSelectionMovable(): this
Disables moving of selected nodes/edges.
toggleSelectionMovable(enabled?: boolean): this
Toggles whether selected nodes/edges can be moved. Parameters are as follows:
| Name | Type | Required | Default Value | Description |
|---|---|---|---|---|
| enabled | boolean | - | Whether to enable moving of selected nodes/edges; defaults to toggling the enabled state. |
isRubberbandEnabled(): boolean
Returns whether rubberband selection is enabled.
enableRubberband(): this
Enables rubberband selection.
disableRubberband(): this
Disables rubberband selection.
toggleRubberband(enabled?: boolean): this
Toggles the enabled state of rubberband selection. Parameters are as follows:
| Name | Type | Required | Default Value | Description |
|---|---|---|---|---|
| enabled | boolean | - | Whether to enable rubberband selection; defaults to toggling the enabled state. |
isStrictRubberband(): boolean
Returns whether strict rubberband selection is enabled. When strict rubberband selection is enabled, only nodes/edges that are completely enclosed by the selection box will be selected.
enableStrictRubberband(): this
Enables strict rubberband selection. When strict rubberband selection is enabled, only nodes/edges that are completely enclosed by the selection box will be selected.
disableStrictRubberband(): this
Disables strict rubberband selection. When strict rubberband selection is disabled, nodes/edges can be selected as long as the selection box intersects with their bounding box.
toggleStrictRubberband(enabled?: boolean): this
Toggles the enabled state of strict rubberband selection. Parameters are as follows:
| Name | Type | Required | Default Value | Description |
|---|---|---|---|---|
| enabled | boolean | - | Whether to enable strict rubberband selection; defaults to toggling the enabled state. |
setSelectionFilter(filter?:| null| (string | { id: string })[]| ((this: Graph, cell: Cell) => boolean)): this
Sets the filtering criteria for selection; only nodes/edges that meet the filtering criteria can be selected.
setRubberbandModifiers(modifiers?: string | ModifierKey[] | null): this
Sets the modifier keys for rubberband selection; rubberband selection can only be triggered when the modifier keys are pressed simultaneously.
setSelectionDisplayContent(content?:| null| false| string| ((this: Graph, selection: Selection, contentElement: HTMLElement) => string)): this
Sets additional display content for selected nodes/edges.
| Event Name | Parameter Type | Description |
|---|---|---|
cell:selected | { cell: Cell; options: Model.SetOptions } | Triggered when a node/edge is selected |
node:selected | { node: Node; options: Model.SetOptions } | Triggered when a node is selected |
edge:selected | { edge: Edge; options: Model.SetOptions } | Triggered when an edge is selected |
cell:unselected | { cell: Cell; options: Model.SetOptions } | Triggered when a node/edge is unselected |
node:unselected | { node: Node; options: Model.SetOptions } | Triggered when a node is unselected |
edge:unselected | { edge: Edge; options: Model.SetOptions } | Triggered when an edge is unselected |
selection:changed | {added: Cell[]; removed: Cell[]; selected: Cell[]; options: Model.SetOptions} | Triggered when the selected nodes/edges change (add/remove) |
graph.on('node:selected', ({ node }) => {console.log(node)})// We can also listen to events on the plugin instanceselection.on('node:selected', ({ node }) => {console.log(node)})