Selection

User Interaction

The TreeList widget supports single and multiple row selection. Use the selection | mode option to change the current mode.

JavaScript
$(function() {
    $("#treeListContainer").dxTreeList({
        selection: {
            mode: "single" // or "multiple" | "none"
        }
    });
});

In the single mode, only one row can be selected at a time. In the multiple mode, rows are supplied with check boxes for multiple selection. A check box in the header of the first column allows a user to select all rows at once.

DevExtreme HTML5 JavaScript jQuery Angular Knockout Widget TreeList Sorting

You can disable the latter capability by setting the selection | allowSelectAll option to false.

JavaScript
$(function() {
    $("#treeListContainer").dxTreeList({
        selection: {
            mode: "multiple",
            allowSelectAll: false
        }
    });
});

Note that selection in any mode is non-recursive, which means that when a user selects a row, its nested rows remain unselected.

Single Selection Demo Multiple Selection Demo

API

Initial and Runtime Selection

Use the selectedRowKeys option to select rows initially. Note that to access a row by its key, you should specify the key option in the Store that underlies the dataSource.

JavaScript
$(function() {
    $("#treeListContainer").dxTreeList({
        // ...
        dataSource: {
            store: {
                // ...
                key: "id"
            }
        },
        selectedRowKeys: [1, 5, 18]
    });
});

You can select rows at runtime as well with the selectRows(keys, preserve) method. Note that the preserve argument, which tells the widget whether to keep or clear the previous selection, is false by default. Before selecting a row, you can call the isRowSelected(key) method to check if this row is already selected. If you need to select all rows at once, call the selectAll() method.

JavaScript
var treeList = $("#treeListContainer").dxTreeList("instance");
if (!treeList.isRowSelected(5)) {
    treeList.selectRows([5], true);
}
JavaScript
treeList.selectRows([5], true);

To get the keys or data of the selected rows, call the getSelectedRowKeys() or getSelectedRowsData() method, respectively.

JavaScript
var treeList = $("#treeListContainer").dxTreeList("instance");
var selectedKeys = treeList.getSelectedRowKeys();
var selectedData = treeList.getSelectedRowsData();

Clear Selection Settings

Call the deselectRows(keys) method to clear the selection of specific rows.

JavaScript
$("#treeListContainer").dxTreeList("deselectRows", [1, 4, 10]);

The deselectAll() method clears selection of all visible rows, and can be used when you apply a filter and want to keep the selection of invisible rows that do not meet the filtering conditions. To clear selection of all rows regardless of their visibility, call the clearSelection() method.

JavaScript
var treeList = $("#treeListContainer").dxTreeList("instance");
treeList.deselectAll();
treeList.clearSelection();
See Also

Events

The TreeList widget raises the selectionChanged event when a row is selected or when the selection is cancelled. If the function that handles this event is going to remain unchanged, assign it to the onSelectionChanged option when you configure the widget.

JavaScript
$(function() {
    $("#treeListContainer").dxTreeList({
        onSelectionChanged: function(e) {
            var currentSelectedRowKeys = e.currentSelectedRowKeys;
            var currentDeselectedRowKeys = e.currentDeselectedRowKeys;
            var allSelectedRowKeys = e.selectedRowKeys;
            var allSelectedRowsData = e.selectedRowsData;
            // Handler of the "selectionChanged" event
        }
    });
});

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.

JavaScript
var selectionChangedEventHandler1 = function(e) {
    // First handler of the "selectionChanged" event
};

var selectionChangedEventHandler2 = function(e) {
    // Second handler of the "selectionChanged" event
};

$("#treeListContainer").dxTreeList("instance")
    .on("selectionChanged", selectionChangedEventHandler1)
    .on("selectionChanged", selectionChangedEventHandler2);
See Also