JavaScript/jQuery TreeList - Expand and Collapse Rows

TreeList allows you to configure expanded rows on initialization, and expand/collapse rows programmatically.

API

All TreeList rows are initially collapsed. If you enable autoExpandAll, all rows are initially expanded. Specify the expandedRowKeys array to expand specific rows. To expand a nested row, include all parent row keys (in addition to the nested row key) in expandedRowKeys.

index.js
  • // Expand specific rows
  • $("#tree-list").dxTreeList({
  • expandedRowKeys: [1, 5, 18],
  • });
  •  
  • // Expand all rows
  • $("#tree-list").dxTreeList({
  • autoExpandAll: true
  • });

To expand/collapse rows at runtime, call expandRow(key)/collapseRow(key) or modify the expandedRowKeys array. To collapse all rows, set expandedRowKeys to an empty array ([]).

To toggle a row between expanded and collapsed states, you can implement the isRowExpanded(key) method as follows:

index.js
  • const treeList = $("#tree-list").dxTreeList("instance");
  •  
  • function toggleRow(key) {
  • 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 properties when you configure the UI component 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