# Tree

## Overview

The Tree component displays hierarchical data in a collapsible, expandable structure. Users can expand or collapse nodes to explore different levels without cluttering the interface.

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FpiXFGawMSM5uUrOUewJL%2FCleanShot%202025-09-10%20at%2012.59.16%402x-min.png?alt=media&#x26;token=95082b8e-c93d-43a5-b3e2-f6ea2099d595" alt=""><figcaption></figcaption></figure>

### Properties

<table><thead><tr><th width="142.109375">Name</th><th width="161.859375">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>name</code></td><td><code>string</code></td><td>Component name</td></tr><tr><td><code>selectedRow</code></td><td><code>TreeActionEvent</code></td><td>The currently selected tree row</td></tr><tr><td><code>toggledRow</code></td><td><code>TreeActionEvent</code></td><td>The last toggled (expanded or collapsed) tree row</td></tr></tbody></table>

### Methods

<table><thead><tr><th width="157.11328125">Name</th><th width="163.66015625">Parameters</th><th width="99.7421875">Returns</th><th>Description</th></tr></thead><tbody><tr><td><code>setData</code></td><td><code>data: object[]</code></td><td><code>void</code></td><td>Set component data. Data is an array of tree nodes with the following structure:<br><code>{</code><br><code>data: any;</code><br><code>children: object;</code><br><code>expanded: boolean;</code><br><code>childrenCount: number;</code><br><code>icon: string;</code><br><code>}</code></td></tr><tr><td><code>selectRow</code></td><td><code>index: number</code></td><td><code>void</code></td><td>Select tree row by index</td></tr><tr><td><code>selectRowByPath</code></td><td><code>pathOrId: string</code></td><td><code>void</code></td><td>Select tree row by path or ID</td></tr></tbody></table>

### Triggers

<table><thead><tr><th width="166.6015625">Name</th><th>Description</th><th data-hidden></th></tr></thead><tbody><tr><td><strong>On Expand Toggle</strong></td><td>Triggered when a tree node is expanded</td><td></td></tr><tr><td><strong>On Row Select</strong></td><td>Triggered when a tree row is selected</td><td></td></tr></tbody></table>

## Working with the component

### Component configuration

The Tree component has two configurable fields: *data* and *columns*.&#x20;

* **Data**

The field supports the following format:

```javascript
{
    data: object,
    children: object[],
    childrenCount: number,
    icon: string,	
}
```

* `data` - an object with raw data; should contain the fields that are listed in the Columns section together with their data
* `children` - an array of objects with the same structure
* `childrenCount` (optional) - the number of children; displayed only if the children array is empty
* `icon` - the name of the icon, if required. The icon is displayed only in the first column.

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FwusjQFoR9TeBfTvrM0ty%2FCleanShot%202025-09-10%20at%2013.26.50%402x-min.png?alt=media&#x26;token=b62c9611-e268-4b76-a92c-896310402bf3" alt=""><figcaption></figcaption></figure>

* **Columns**

You can set up columns directly from the interface or using the JS mode.&#x20;

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2F59INVn9IDybHR0xvLCMS%2FCleanShot%202025-09-10%20at%2013.37.35%402x-min.png?alt=media&#x26;token=c59bc4b9-be4e-4264-b2ac-65587b523955" alt=""><figcaption></figcaption></figure>

The code format is represented as an array of the following objects:

```javascript
{
    name: string | number,
    id: string | number,
    primaryKey: boolean,
    width: number,
}
```

* `name`  - the header of the column
* `id` - the unique column identifier; gets value from the `Data.data` field;
* `primaryKey` - uniquely defines a record in the `Data` field; necessary for the correct row selection
* `width` - width of a column in px

### Use case: Using data from an action

Let's say you have two tables - *Orders* and *Order details* - and you want to display the list of orders in the Tree component so that each order can be expanded showing its details. Here's how you can configure it:

1. Start by creating the actions to load the data you need (in our example, *loadOrders* & *loadDetails* actions).
2. Next, add a *JavaScript Code* action with the following code:

```javascript
const [orders, orderDetails] = await Promise.all([
  {{actions.loadOrders.trigger()}},
  {{actions.loadDetails.trigger()}},
]);

const treeData = orders.map(order => {
  const relatedOrderDetails = orderDetails.filter(item => item.orderNumber === order.orderNumber);
  const treeItem = {
    data: {
      ...order,
      priceEach: relatedOrderDetails.reduce((acc, cur) => {
        return acc + Number(cur.priceEach);
      }, 0).toFixed(2),
      quantityOrdered: relatedOrderDetails.reduce((acc, cur) => {
        return acc + Number(cur.quantityOrdered);
      }, 0).toFixed(2),
    },
    children: relatedOrderDetails.map(item => {
      return {
        data: item,
      }
    }),
    icon: 'car',
  };
  
  return treeItem;
});

return treeData;
```

3. Add a Tree component to the canvas and assign the action from step 2 to the component's *Data* field.
4. Finally, adjust component's Columns based on the columns' data specified in the code.

{% hint style="info" %}
Make sure to specify the necessary columns as primary keys to correctly identify rows. In our example, we set the *Order Number* and *Product Code* columns as primary keys.
{% endhint %}

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FxYvJBHyGgKpWb1V5vo0B%2FCleanShot%202025-09-10%20at%2015.14.39.png?alt=media&#x26;token=6862e316-93cc-42b8-a6de-b7e1bbc8db5d" alt=""><figcaption></figcaption></figure>
