DevExtreme Vue - Expand and Collapse Nodes

Initially

If a node is supposed to be expanded initially, set its expanded field to true. If another field specifies whether a node is expanded or collapsed, assign its name to the expandedExpr option as shown in the following code.

App.vue
  • <template>
  • <DxTreeView
  • key-expr="key"
  • display-expr="name"
  • expanded-expr="isExpanded"
  • :items="hierarchicalData"
  • />
  • </template>
  • <script>
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { DxTreeView } from 'devextreme-vue/tree-view';
  •  
  • const hierarchicalData = [{
  • key: '1',
  • name: 'Fruits',
  • isExpanded: true,
  • items: [
  • { key: '1_1', name: 'Apples' },
  • { key: '1_2', name: 'Oranges' }
  • ]
  • }, {
  • key: '2',
  • name: 'Vegetables',
  • isExpanded: true,
  • items: [
  • { key: '2_1', name: 'Cucumbers' },
  • { key: '2_2', name: 'Tomatoes' }
  • ]
  • }];
  •  
  • export default {
  • components: {
  • DxTreeView
  • },
  • data() {
  • return {
  • hierarchicalData
  • };
  • },
  • };
  • </script>

Using the API

The TreeView provides the following API to expand and collapse nodes:

  • All nodes
    You can use the expandAll() and collapseAll() methods to expand and collapse nodes at once. Note that the expandAll() method expands only the loaded nodes if data is loaded on demand.

    App.vue
    • <template>
    • <DxTreeView
    • :ref="treeViewRef"
    • :items="data"
    • />
    • </template>
    • <script>
    • import 'devextreme/dist/css/dx.common.css';
    • import 'devextreme/dist/css/dx.light.css';
    •  
    • import { DxTreeView } from 'devextreme-vue/tree-view';
    •  
    • const treeViewRef = 'treeView';
    • const data = [ ... ];
    •  
    • export default {
    • components: {
    • DxTreeView
    • },
    • data() {
    • return {
    • data,
    • treeViewRef
    • };
    • },
    • computed: {
    • treeView: function() {
    • return this.$refs[treeViewRef].instance;
    • }
    • },
    • methods: {
    • expandAllNodes() {
    • this.treeView.expandAll();
    • },
    • collapseAllNodes() {
    • this.treeView.collapseAll();
    • }
    • }
    • };
    • </script>
  • Individual nodes
    Call the expandItem(key) or collapseItem(key) method and pass a node key as an argument:

    App.vue
    • <template>
    • <DxTreeView
    • :ref="treeViewRef"
    • :items="data"
    • />
    • </template>
    • <script>
    • import 'devextreme/dist/css/dx.common.css';
    • import 'devextreme/dist/css/dx.light.css';
    •  
    • import { DxTreeView } from 'devextreme-vue/tree-view';
    •  
    • const treeViewRef = 'treeView';
    • const data = [ ... ];
    •  
    • export default {
    • components: {
    • DxTreeView
    • },
    • data() {
    • return {
    • data,
    • treeViewRef
    • };
    • },
    • computed: {
    • treeView: function() {
    • return this.$refs[treeViewRef].instance;
    • }
    • },
    • methods: {
    • expandNode(key) {
    • this.treeView.expandItem(key);
    • },
    • collapseNode(key) {
    • this.treeView.collapseItem(key);
    • }
    • }
    • };
    • </script>

Events

To execute certain commands when a node is expanded or collapsed, handle the itemExpanded or itemCollapsed event. Assign event handling functions to the onItemExpanded or onItemCollapsed option when you configure the widget if these functions are going to remain unchanged.

App.vue
  • <template>
  • <DxTreeView
  • :data-source="data"
  • @item-expanded="onItemExpanded"
  • @item-collapsed="onItemCollapsed"
  • />
  • </template>
  • <script>
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { DxTreeView } from 'devextreme-vue/tree-view';
  •  
  • const data = [ ... ];
  •  
  • export default {
  • components: {
  • DxTreeView
  • },
  • data() {
  • return {
  • data
  • };
  • },
  • methods: {
  • onItemExpanded(e) {
  • // Handler of the 'itemExpanded' event
  • }
  • onItemCollapsed(e) {
  • // Handler of the 'itemCollapsed' event
  • }
  • }
  • };
  • </script>

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. This approach is more typical of jQuery.

JavaScript
  • var itemCollapsedEventHandler1 = function (e) {
  • // First handler of the "itemCollapsed" event
  • };
  •  
  • var itemCollapsedEventHandler2 = function (e) {
  • // Second handler of the "itemCollapsed" event
  • };
  •  
  • $("#treeViewContainer").dxTreeView("instance")
  • .on("itemCollapsed", itemCollapsedEventHandler1)
  • .on("itemCollapsed", itemCollapsedEventHandler2);
See Also