> For the complete documentation index, see [llms.txt](https://docs.avonnicomponents.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.avonnicomponents.com/lwc-components/data-driven-components/tree-grid.md).

# Tree Grid

## Overview

Browse related records as expandable rows with several columns. Use a nested `query` for Salesforce relationships or `items` with `_children` for a fixed hierarchy.

<figure><img src="https://2403786217-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPjQbBtFNxVSSOtnsjuwx%2Fuploads%2Fjqmp0yVuFaJgnp9Dc4sg%2Flwc-dd-tree-grid-accounts-opportunities.jpg?alt=media" alt="Tree Grid showing Accounts with expanded Opportunity rows and multiple data columns"><figcaption><p>Account rows expand to show related Opportunities and their stage, close date and amount.</p></figcaption></figure>

### Choose a source

{% columns %}
{% column %}
{% hint style="info" %}

#### Salesforce hierarchy

Show Accounts with their Opportunities in nested rows.

[See the example](#example-1-query-mode)
{% endhint %}
{% endcolumn %}

{% column %}
{% hint style="success" %}

#### Fixed rows

Pass a small hierarchy through `items`.

[See the example](#example-2-static-mode)
{% endhint %}
{% endcolumn %}
{% endcolumns %}

{% hint style="warning" %}
Confirm your installed LWC package includes `avonni-dd-tree-grid` before using these examples.
{% endhint %}

## Use Case Examples

### Example 1: Query mode

**Scenario:** Display Accounts with their Opportunities nested beneath each account, showing the industry of each account and the amount of each opportunity.

{% tabs %}
{% tab title="accountOpportunities.html" %}

```html
<!-- accountOpportunities.html -->
<template>
    <avonni-dd-tree-grid
        query={gridQuery}
        columns={columns}
        show-item-count
        show-total-count
        onselectrows={handleSelectRows}
        ontoggle={handleToggle}
    ></avonni-dd-tree-grid>
</template>
```

{% endtab %}

{% tab title="accountOpportunities.js" %}

```js
// accountOpportunities.js
import { LightningElement } from 'lwc';

export default class AccountOpportunities extends LightningElement {
    gridQuery = {
        objectApiName: 'Account',
        orderBy: 'Name ASC NULLS LAST',
        limit: 50,
        children: [
            {
                objectApiName: 'Opportunity',
                relationshipField: 'AccountId',
                orderBy: 'Name ASC'
            }
        ]
    };
    columns = [
        { objectApiName: 'Account', fieldName: 'Name', type: 'text' },
        { objectApiName: 'Account', fieldName: 'Industry', type: 'text' },
        { objectApiName: 'Opportunity', fieldName: 'Amount', type: 'currency' }
    ];

    handleSelectRows(event) {
        const { selectedRowsKeyValue } = event.detail;
    }

    handleToggle(event) {
        const { name, expanded } = event.detail;
    }
}
```

{% endtab %}
{% endtabs %}

**Result:** A table of Accounts, each showing its Industry, with the number of related Opportunities after the name, and the total number of items in the header. Accounts start collapsed; expanding one loads its Opportunities, which show their Amount and leave the Industry cell empty. Each checkbox click fires `selectrows` with the Ids of the selected rows, and each expand or collapse fires `toggle`.

### Example 2: Static mode

**Scenario:** Display a small hierarchy held in memory, with every row expanded.

{% tabs %}
{% tab title="companyGrid.html" %}

```html
<!-- companyGrid.html -->
<template>
    <avonni-dd-tree-grid
        columns={columns}
        items={items}
        expand-all-items
        hide-checkbox-column
    ></avonni-dd-tree-grid>
</template>
```

{% endtab %}

{% tab title="companyGrid.js" %}

```js
// companyGrid.js
import { LightningElement } from 'lwc';

export default class CompanyGrid extends LightningElement {
    columns = [
        { fieldName: 'Name', label: 'Name', type: 'text' },
        { fieldName: 'Industry', label: 'Industry', type: 'text' }
    ];
    items = [
        {
            Id: '1',
            Name: 'Acme Corporation',
            Industry: 'Technology',
            _children: [
                {
                    Id: '1-1',
                    Name: 'Acme Engineering',
                    Industry: 'Technology'
                }
            ]
        },
        {
            Id: '2',
            Name: 'Global Media',
            Industry: 'Media'
        }
    ];
}
```

{% endtab %}
{% endtabs %}

\*\*Result:\*\* Two top-level rows, the first expanded to show its nested row, with no selection checkboxes.

***

## Specifications

### Attributes

| Name                     | Description                                                                                                                                                                                                                                                                                                                                                             | Type                      | Default       | Required |
| ------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- | ------------- | -------- |
| `column-widths-mode`     | Specifies how column widths are calculated. Valid values are `fixed` and `auto`. Set to `fixed` for columns with equal widths. Set to `auto` for column widths that are based on the width of the column content and the table width.                                                                                                                                   | String                    | `"fixed"`     |          |
| `columns`                | Array of column objects that define the table columns. The first column holds the expand/collapse toggle of the rows. In query mode, each column displays a field of one object: `objectApiName` is the object, and `fieldName` the field API name. Rows of the other objects leave the cell empty. In static mode, `fieldName` is the key of the row value to display. | DdTreeGridColumn\[]       | Not specified |          |
| `expand-all-items`       | If present, all the rows are expanded.                                                                                                                                                                                                                                                                                                                                  | Boolean                   | `false`       |          |
| `hide-checkbox-column`   | If present, the checkbox column for row selection is hidden.                                                                                                                                                                                                                                                                                                            | Boolean                   | `false`       |          |
| `hide-table-header`      | If present, the table header is hidden.                                                                                                                                                                                                                                                                                                                                 | Boolean                   | `false`       |          |
| `items`                  | Array of static rows displayed in the tree grid. When this property is set, the tree grid ignores the `query` property and displays the rows directly. Each row is an object whose keys match the column `fieldName` values, and whose `_children` key holds the nested rows.                                                                                           | object\[]                 | Not specified |          |
| `key-field`              | Name of the row field used as the unique row key. In query mode, the rows are keyed by their record `Id`.                                                                                                                                                                                                                                                               | String                    | `"Id"`        |          |
| `max-column-width`       | The maximum width for all columns, in pixels.                                                                                                                                                                                                                                                                                                                           | Number                    | `1000`        |          |
| `min-column-width`       | The minimum width for all columns, in pixels.                                                                                                                                                                                                                                                                                                                           | Number                    | `50`          |          |
| `query`                  | Object describing the queries that should be executed to fetch the records on each nested level of the component.                                                                                                                                                                                                                                                       | DdNestedQueryElementQuery | Not specified |          |
| `refresh-emp`            | Object describing a platform event that should be subscribed to in order to refresh the component when an event is published.                                                                                                                                                                                                                                           | DdElementRefreshEmp       | Not specified |          |
| `resize-column-disabled` | If present, column resizing is disabled.                                                                                                                                                                                                                                                                                                                                | Boolean                   | `false`       |          |
| `row-number-offset`      | Determines where to start counting the row number.                                                                                                                                                                                                                                                                                                                      | Number                    | `0`           |          |
| `show-item-count`        | If present, the number of children of a row is displayed next to its label, at every level.                                                                                                                                                                                                                                                                             | Boolean                   | `false`       |          |
| `show-row-number-column` | If present, the row number column is shown in the first column.                                                                                                                                                                                                                                                                                                         | Boolean                   | `false`       |          |
| `show-total-count`       | If present, the total number of items is displayed in the header.                                                                                                                                                                                                                                                                                                       | Boolean                   | `false`       |          |

### Column Definition

Each entry of `columns` describes one column. The first column always holds the expand/collapse toggle and the row label, whatever its `type`.

| Key               | Type    | Description                                                                                                                                                                                                                                                                                   |
| ----------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `fieldName`       | String  | Required. In query mode, the API name of the field displayed. In static mode, the key of the row value displayed.                                                                                                                                                                             |
| `objectApiName`   | String  | Required in query mode. API name of the object the field belongs to. The cells of the rows of other objects are left empty.                                                                                                                                                                   |
| `label`           | String  | Custom column header label. In query mode, it is only used when `customLabel` is `true` or `columnType` is `custom`; otherwise the field label is displayed.                                                                                                                                  |
| `customLabel`     | Boolean | In query mode, if true, `label` is displayed in the header instead of the field label. Defaults to `false`.                                                                                                                                                                                   |
| `columnType`      | String  | `objectField` (default) or `custom`. A custom column is not bound to an object field and is typically used for an action column.                                                                                                                                                              |
| `customFieldName` | String  | Name of the column when `columnType` is `custom`.                                                                                                                                                                                                                                             |
| `type`            | String  | Data type of the cells: `text` (default), `action`, `avatar`, `badge`, `boolean`, `button`, `button-icon`, `currency`, `date`, `date-local`, `email`, `location`, `number`, `percent`, `phone`, `progress-bar`, `progress-circle`, `progress-ring`, `qrcode`, `rating`, `rich-text` or `url`. |
| `typeAttributes`  | Object  | Type-specific options, in the same format as the [Data Table](https://docs.avonnicomponents.com/lwc-components/data-driven-components/datatable.md) column definition.                                                                                                                        |
| `cellAttributes`  | Object  | Styling and layout options of the cells. In query mode, the icon options are ignored on the first column.                                                                                                                                                                                     |
| `actions`         | Array   | Dropdown menu of actions added to the column header. Selecting one fires the `columnheaderactionclick` event.                                                                                                                                                                                 |
| `iconName`        | String  | Lightning Design System name of the column icon, for example `standard:account`.                                                                                                                                                                                                              |
| `initialWidth`    | Number  | Width of the column, in pixels, when it is initialized.                                                                                                                                                                                                                                       |
| `sortable`        | Boolean | If true, the column is sortable. Defaults to `false`.                                                                                                                                                                                                                                         |
| `hidden`          | Boolean | In query mode, if true, the column is hidden. Defaults to `false`.                                                                                                                                                                                                                            |
| `wrapText`        | Boolean | If true, the text of the column is wrapped. Defaults to `false`.                                                                                                                                                                                                                              |

### Methods

| Name      | Description                                                   | Argument Name | Argument Type | Argument Description |
| --------- | ------------------------------------------------------------- | ------------- | ------------- | -------------------- |
| `refresh` | Refresh the query and the records displayed in the component. |               |               |                      |

### Custom Events

#### `columnheaderactionclick`

Event fired when a column header action is clicked.

The `columnheaderactionclick` event returns the following parameters.

| Parameter          | Type   | Description                                                                                                             |
| ------------------ | ------ | ----------------------------------------------------------------------------------------------------------------------- |
| `name`             | string | Name of the action clicked.                                                                                             |
| `columnDefinition` | object | Column definition. In query mode, its `fieldName` is prefixed with the object API name, for example `Account.Industry`. |

The event properties are as follows.

| Property   | Value | Description                                                                                               |
| ---------- | ----- | --------------------------------------------------------------------------------------------------------- |
| bubbles    | false | This event does not bubble.                                                                               |
| cancelable | false | This event has no default behavior that can be canceled. You can't call `preventDefault()` on this event. |
| composed   | false | This event does not propagate outside of the component in which it was dispatched.                        |

#### `rowaction`

Event fired when a row action is clicked.

The `rowaction` event returns the following parameters.

| Parameter | Type   | Description                                                                                                        |
| --------- | ------ | ------------------------------------------------------------------------------------------------------------------ |
| `name`    | string | Name of the action clicked.                                                                                        |
| `item`    | object | Row the action belongs to, with its formatted column values.                                                       |
| `record`  | object | Record corresponding to the row the action belongs to. In static mode, no record is associated and this is `null`. |

The event properties are as follows.

| Property   | Value | Description                                                                                               |
| ---------- | ----- | --------------------------------------------------------------------------------------------------------- |
| bubbles    | false | This event does not bubble.                                                                               |
| cancelable | false | This event has no default behavior that can be canceled. You can't call `preventDefault()` on this event. |
| composed   | false | This event does not propagate outside of the component in which it was dispatched.                        |

#### `selectrows`

Event fired when rows are selected or unselected.

The `selectrows` event returns the following parameters.

| Parameter              | Type      | Description                                  |
| ---------------------- | --------- | -------------------------------------------- |
| `selectedRowsKeyValue` | string\[] | Array of the selected rows key field values. |

The event properties are as follows.

| Property   | Value | Description                                                                                               |
| ---------- | ----- | --------------------------------------------------------------------------------------------------------- |
| bubbles    | false | This event does not bubble.                                                                               |
| cancelable | false | This event has no default behavior that can be canceled. You can't call `preventDefault()` on this event. |
| composed   | false | This event does not propagate outside of the component in which it was dispatched.                        |

#### `toggle`

Event fired when a row is expanded or collapsed.

The `toggle` event returns the following parameters.

| Parameter  | Type    | Description                                              |
| ---------- | ------- | -------------------------------------------------------- |
| `name`     | string  | Key field value of the toggled row.                      |
| `expanded` | boolean | True if the row was expanded, false if it was collapsed. |

The event properties are as follows.

| Property   | Value | Description                                                                                               |
| ---------- | ----- | --------------------------------------------------------------------------------------------------------- |
| bubbles    | false | This event does not bubble.                                                                               |
| cancelable | false | This event has no default behavior that can be canceled. You can't call `preventDefault()` on this event. |
| composed   | false | This event does not propagate outside of the component in which it was dispatched.                        |

## Key Considerations

* **Query mode or static items:** In query mode you must supply a `query` and `columns` to render anything. Alternatively, pass a static `items` array; when `items` is set, `query` is ignored. The `mapping` property inherited from the other nested-query components is unused here (`ddTreeGrid.js` marks it `@ignore`): the columns decide what each row displays.
* **Columns belong to an object:** In query mode each column reads one field of one object. A row of another object leaves that cell empty, so list one column per object and field you want to show.
* **The first column is the tree column:** It always shows the row label next to the expand/collapse toggle, whatever its `type`. When it is the `Name` field (or the name field of the object), every row is labelled with the name of its own record, at any level (`treeUtils.js:242-251`, `bcTreeGrid.js:858-867`).
* **`relationshipField` is required for children:** Each child query must name the lookup field that ties it to the parent level, or that level won't load.
* **Mind the limits:** `limit` applies to the root query only; each child level loads in pages of 100 records per parent (`DEFAULT_NB_ITEMS_PER_PAGE`, `componentsUtils.js:84`), with a **Load More** row at the end of the level. `filter` (a SOQL WHERE expression) applies to the root query only; `ddNestedQueryElement.js` does not pass the filter of a child level (`_computeChildQuery`).
* **Rows load on expand:** Rows start collapsed and each expand runs a query. With `expand-all-items`, every level is loaded and expanded.
* **Counts:** `show-item-count` shows the number of children after each row label, at every level. `show-total-count` shows the total number of items in the header; in query mode it counts the records of every level of the query, not only the rows loaded so far, and it is partial, with a console warning, when the query row limit is reached (`bcNestedQueryElement.js:345-349`).
* **Row keys:** In query mode the rows are keyed by their record `Id`, and `selectrows` reports those Ids. In static mode, `key-field` names the row value that identifies a row, and every row needs a unique one.
* **Best Practice:** Set `orderBy` on each query level, and a sensible `limit` on the root query, to keep large hierarchies readable.

***

## Troubleshooting Common Issues

* **Children don't appear:** Verify each child query has the correct `relationshipField` (the lookup on the child object pointing to the parent) and that the running user can access those records.
* **A cell is empty on some rows:** The column's `objectApiName` is a different object from the row's. Add a column for that object and field.
* **The label column shows an unexpected value:** The first column drives the row label. Make it the object's name field, or accept that it displays the field you chose.
* **Nothing renders with `items`:** Each row must hold its nested rows under `_children`, and each `fieldName` must match a key of the row.
* **If issues persist:** Contact our support team at <support@avonni.app> for assistance.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.avonnicomponents.com/lwc-components/data-driven-components/tree-grid.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
