Expand and Collapse Rows

API

All rows are collapsed by default. Assign an array of keys to the expandedRowKeys option to expand specific rows initially. If a to-be-expanded row lies deep in the hierarchy, make sure to include keys of all its parent rows as well. Set the autoExpandAll option to true if all rows should be expanded initially.

JavaScript
$(function() {
    $("#treeListContainer").dxTreeList({
        expandedRowKeys: [1, 5, 18],
        // autoExpandAll: true
    });
});

Call the expandRow(key) or collapseRow(key) method to respectively expand or collapse a row at runtime. You can check a row's current state by calling the isRowExpanded(key) method.

JavaScript
function toggleRow(key) {
    var treeList = $("#treeListContainer").dxTreeList("instance");
    if (treeList.isRowExpanded(key)) {
        treeList.collapseRow(key);
    } else {
        treeList.expandRow(key);
    }
}
See Also

Events

To execute certain commands before or after a row was expanded or collapsed, handle the rowExpanding, rowExpanded, rowCollapsing or rowCollapsed event. Assign event handling functions to the corresponding onEventName options when you configure the widget if these functions are going to remain unchanged.

JavaScript
$(function() {
    $("#treeListContainer").dxTreeList({
        onRowExpanding: function (e) {
            // Handler of the "rowExpanding" event
        },
        onRowExpanded: function (e) {
            // Handler of the "rowExpanded" event
        },
        onRowCollapsing: function (e) {
            // Handler of the "rowCollapsing" event
        },
        onRowCollapsed: function (e) {
            // Handler of the "rowCollapsed" event
        }
    });
});

If you are going to change event handlers at runtime, or if you need to attach several handlers to a single event, subscribe to the events using the on(eventName, eventHandler) method.

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

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

$("#treeListContainer").dxTreeList("instance")
    .on("rowCollapsed", rowCollapsedEventHandler1)
    .on("rowCollapsed", rowCollapsedEventHandler2);
See Also