DevExtreme React - Select Nodes

Initially

If a node is supposed to be selected initially, set its selected field to true. This is a conventional field name defined by the Default Item Template pattern. If another field specifies whether a node is selected or not, assign its name to the selectedExpr option as shown in the following code.

Using the API

To select or cancel the selection of a node programmatically, call the selectItem(itemElement) or unselectItem(itemElement) method passing the key of the node as a parameter.

To select or cancel the selection of all nodes programmatically, call the selectAll() or unselectAll() method.

NOTE
If the showCheckBoxesMode is "none", nodes selected using the API do not differ from unselected nodes in appearance.

User Interaction

If only a single node should be in the selected state at a time, set the selectByClick option to true. In this case, an end user can click a node to select it.

To select several nodes simultaneously, set the showCheckBoxesMode option to "normal". This adds a check box to each node that enables you to select these nodes. You can also set the showCheckBoxesMode option to "selectAll" to allow a user to select all nodes.

View Demo

Events

The TreeView raises the following selection-related events:

You can handle these events with functions. Assign the handling functions to the onItemSelectionChanged and onSelectAllValueChanged options when you configure the widget if they are going to remain unchanged at runtime.

If you are going to change the event handler at runtime, or if you need to attach several handlers to the event, subscribe to it using the on(eventName, eventHandler) method. This approach is more typical of jQuery.

JavaScript
  • var itemSelectionChangedHandler1 = function (e) {
  • // First handler of the "itemSelectionChanged" event
  • };
  •  
  • var itemSelectionChangedHandler2 = function (e) {
  • // Second handler of the "itemSelectionChanged" event
  • };
  •  
  • $("#treeViewContainer").dxTreeView("instance")
  • .on("itemSelectionChanged", itemSelectionChangedHandler1)
  • .on("itemSelectionChanged", itemSelectionChangedHandler2);

View Demo

See Also