Vue 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.

App.vue
  • <template>
  • <!-- Expand specific rows -->
  • <DxTreeList ...
  • :expanded-row-keys="[1, 5, 18]"
  • />
  •  
  • <!-- Expand all rows -->
  • <DxTreeList ...
  • :auto-expand-all="true"
  • />
  • </template>
  •  
  • <script setup lang="ts">
  • import { DxTreeList } from 'devextreme-vue/tree-list';
  •  
  • </script>

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:

App.vue
  • <template>
  • <DxTreeList ...
  • ref="treeListRef"
  • />
  • </template>
  •  
  • <script setup lang="ts">
  • import { ref } from 'vue';
  • import { DxTreeList } from 'devextreme-vue/tree-list';
  •  
  • const treeListRef = ref(null);
  •  
  • function toggleRow(key) {
  • if (treeList.value.instance.isRowExpanded(key)) {
  • treeList.value.instance.collapseRow(key);
  • } else {
  • treeList.value.instance.expandRow(key);
  • }
  • }
  • </script>
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.

App.vue
  • <template>
  • <DxTreeList ...
  • @row-expanding="onRowExpanding"
  • @row-expanded="onRowExpanded"
  • @row-collapsing="onRowCollapsing"
  • @row-collapsed="onRowCollapsed">
  • </DxTreeList>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.fluent.blue.light.css';
  •  
  • import DxTreeList from 'devextreme-vue/tree-list';
  •  
  • export default {
  • components: {
  • DxTreeList
  • },
  • // ...
  • methods: {
  • onRowExpanding (e) {
  • // Handler of the "rowExpanding" event
  • },
  • onRowExpanded (e) {
  • // Handler of the "rowExpanded" event
  • },
  • onRowCollapsing (e) {
  • // Handler of the "rowCollapsing" event
  • },
  • onRowCollapsed (e) {
  • // Handler of the "rowCollapsed" event
  • }
  • }
  • }
  • </script>
See Also