Skip to main content
Version: v5.3

Multiple Selection with IafViewerDBM

Overview​

{{IafViewerDBM}} reports graphic selection changes through OnSelectedElementChangeCallback. The callback receives the currently selected element ids after a select or deselect. Whether the app keeps a single selection or a multi-selection is entirely application-owned: drive it with the selection prop and your own Ctrl / Command key handling.

This recipe follows the Confluence guide Multiple Selection In V4.5. Selection UX basics live under IafViewer UI; this page shows the integration pattern.

Sibling recipes: Model Comparison Using View3D, Federated View3D Buildings on Gis(Mapbox), UE with ArcGIS, Overview Map, and Photosphere.

Building blocks​

How to set up multiple selection​

Step 1: Pass selection from app state​

selection is always an array of element ids. Length 1 is single selection; length > 1 is multi-selection. The viewer highlights whatever you pass.

<IafViewerDBM
selection={this.state.selectedElementIds}
OnSelectedElementChangeCallback={this.handleOnSelectedElementChangeCallback}
enableOptimizedSelection={this.props.enableOptimizedSelection}
model={this.props.model}
serverUri={this.props.serverUri}
/>

Step 2: Own multi vs single in the callback​

OnSelectedElementChangeCallback only supplies the ids involved in the latest graphic change. Your handler decides whether to toggle (Ctrl / Command held) or replace the selection.

handleOnSelectedElementChangeCallback = (elementIds) => {
this.setState((prevState) => {
let newSelection;
if (prevState.isCtrlPressed) {
newSelection = [...prevState.selectedElementIds];
elementIds.forEach((id) => {
if (newSelection.includes(id)) {
newSelection = newSelection.filter((existingId) => existingId !== id);
} else {
newSelection.push(id);
}
});
} else {
newSelection = elementIds;
}

this.props.onSelect?.(newSelection.map((id) => ({ id })));
return { selectedElementIds: newSelection };
});
};

Step 3: Track Ctrl / Command when using optimized selection​

When enableOptimizedSelection is true, register window key listeners so isCtrlPressed stays accurate. Remove them on unmount.

handleKey = (event) => {
this.setState({ isCtrlPressed: event.ctrlKey || event.metaKey });
};

componentDidMount() {
if (this.props.enableOptimizedSelection) {
window.addEventListener("keydown", this.handleKey);
window.addEventListener("keyup", this.handleKey);
}
}

componentWillUnmount() {
if (this.props.enableOptimizedSelection) {
window.removeEventListener("keydown", this.handleKey);
window.removeEventListener("keyup", this.handleKey);
}
}

Note: With enableOptimizedSelection={true}, Communicator Ctrl-based multi-select inside the viewer is limited (see package note PLG-1115). The pattern above — app tracks Ctrl/Command and merges into selection — is the supported multi-select path for that mode.

Useful concepts​

PieceWho owns it
Highlight which ids are selectedApp → selection prop
Notify after graphic click / deselectViewer → OnSelectedElementChangeCallback
Single vs multi policyApp (replace vs toggle when Ctrl/Command)
Fast asset↔node mappingOptional enableOptimizedSelection

The library does not expose a separate “selection mode” enum. Multi-select is “array length + your callback logic.”

Complete example​

import React from "react";
import IafViewerDBM from "@dtplatform/iaf-viewer";

class MultiSelectViewerHost extends React.Component {
state = {
selectedElementIds: [],
isCtrlPressed: false,
};

handleKey = (event) => {
this.setState({ isCtrlPressed: event.ctrlKey || event.metaKey });
};

componentDidMount() {
if (this.props.enableOptimizedSelection) {
window.addEventListener("keydown", this.handleKey);
window.addEventListener("keyup", this.handleKey);
}
}

componentWillUnmount() {
if (this.props.enableOptimizedSelection) {
window.removeEventListener("keydown", this.handleKey);
window.removeEventListener("keyup", this.handleKey);
}
}

handleOnSelectedElementChangeCallback = (elementIds) => {
this.setState((prevState) => {
let newSelection;
if (prevState.isCtrlPressed) {
newSelection = [...prevState.selectedElementIds];
elementIds.forEach((id) => {
if (newSelection.includes(id)) {
newSelection = newSelection.filter((existingId) => existingId !== id);
} else {
newSelection.push(id);
}
});
} else {
newSelection = elementIds;
}

this.props.onSelect?.(newSelection.map((id) => ({ id })));
return { selectedElementIds: newSelection };
});
};

render() {
return (
<IafViewerDBM
model={this.props.model}
serverUri={this.props.serverUri}
selection={this.state.selectedElementIds}
enableOptimizedSelection={this.props.enableOptimizedSelection}
OnSelectedElementChangeCallback={this.handleOnSelectedElementChangeCallback}
/>
);
}
}

export default MultiSelectViewerHost;