# Managing the tree column and expand/collapse icon

> Learn how to render the tree expand/collapse icon and manage the tree column

Canonical page: https://infinite-table.com/angular/docs/learn/tree-grid/tree-column

When rendering a tree, you have to use the `TreeGrid` component (`<infinite-tree-grid>`) instead of `InfiniteTable` (`<infinite-table>`), inside a `TreeDataSource` (`<infinite-tree-data-source>`).

The `TreeGrid` component is simply an `InfiniteTable` component with some inputs removed - those don't make sense for tree scenarios.

By default no tree column is rendered.

To specify the tree column, you have to to set the [`columns.renderTreeIcon`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderTreeIcon) prop to `true` for your column of choice.

```tsx
const columns: Record<string, InfiniteTableColumn<FileSystemNode>> = {
  name: { 
    renderTreeIcon: true,
    field: 'name',
    header: 'Name' 
  },
  type: { field: 'type', header: 'Type' },
  size: { field: 'sizeInKB', type: 'number', header: 'Size (KB)' },
};
```
This is very similar to how you specify the [selection column for multi-select configurations](https://infinite-table.com/angular/docs/learn/selection/row-selection.md#using-a-selection-checkbox).

**Example: Specifying the tree column**

```ts
import { Component, computed, signal } from '@angular/core';

import { TreeDataSource, TreeGrid } from '@infinite-table/infinite-angular';
import type { InfiniteTableColumn } from '@infinite-table/infinite-angular';

type FileSystemNode = {
  id: string;
  name: string;
  type: 'folder' | 'file';
  extension?: string;
  mimeType?: string;
  sizeInKB: number;
  children?: FileSystemNode[];
};

const allColumns: Record<string, InfiniteTableColumn<FileSystemNode>> = {
  name: { field: 'name', header: 'Name' },
  type: { field: 'type', header: 'Type' },
  extension: { field: 'extension', header: 'Extension' },
  mimeType: { field: 'mimeType', header: 'Mime Type' },
  size: { field: 'sizeInKB', type: 'number', header: 'Size (KB)' },
};

const dataSource = () => {
  const nodes: FileSystemNode[] = [
    {
      id: '1',
      name: 'Documents',
      sizeInKB: 1200,
      type: 'folder',
      children: [
        {
          id: '10',
          name: 'Private',
          sizeInKB: 100,
          type: 'folder',
          children: [
            {
              id: '100',
              name: 'Report.docx',
              sizeInKB: 210,
              type: 'file',
              extension: 'docx',
              mimeType: 'application/msword',
            },
            {
              id: '101',
              name: 'Vacation.docx',
              sizeInKB: 120,
              type: 'file',
              extension: 'docx',
              mimeType: 'application/msword',
            },
            {
              id: '102',
              name: 'CV.pdf',
              sizeInKB: 108,
              type: 'file',
              extension: 'pdf',
              mimeType: 'application/pdf',
            },
          ],
        },
      ],
    },
    {
      id: '2',
      name: 'Desktop',
      sizeInKB: 1000,
      type: 'folder',
      children: [],
    },
    {
      id: '3',
      name: 'Media',
      sizeInKB: 1000,
      type: 'folder',
      children: [
        {
          id: '30',
          name: 'Music',
          sizeInKB: 0,
          type: 'folder',
          children: [],
        },
        {
          id: '31',
          name: 'Videos',
          sizeInKB: 5400,
          type: 'folder',
          children: [
            {
              id: '310',
              name: 'Vacation.mp4',
              sizeInKB: 108,
              type: 'file',
              extension: 'mp4',
              mimeType: 'video/mp4',
            },
          ],
        },
      ],
    },
  ];
  return Promise.resolve(nodes);
};

@Component({
  selector: 'demo-root',
  imports: [TreeDataSource, TreeGrid],
  template: `
    <infinite-tree-data-source
      nodesKey="children"
      primaryKey="id"
      [data]="dataSource"
    >
      <div style="color: var(--infinite-cell-color); padding: 10px">
        <p>Select the tree column</p>
        <select
          (change)="onTreeIconChange($event)"
          title="Select column to use for the tree icon"
        >
          @for (key of columnKeys; track key) {
            <option [value]="key" [selected]="key === treeIcon()">
              {{ key }}
            </option>
          }
          <option value="none" [selected]="treeIcon() === 'none'">
            No tree column
          </option>
        </select>
      </div>

      <infinite-tree-grid [columns]="columns()" />
    </infinite-tree-data-source>
  `,
})
export default class App {
  protected readonly dataSource = dataSource;
  protected readonly columnKeys = Object.keys(allColumns);

  protected readonly treeIcon = signal<string>('name');

  protected readonly columns = computed(() => {
    const cols = { ...allColumns };
    const treeIcon = this.treeIcon();

    
    cols[treeIcon] = {
      ...cols[treeIcon],
      renderTreeIcon: true,
    };

    return cols;
  });

  protected onTreeIconChange(event: Event) {
    this.treeIcon.set((event.target as HTMLSelectElement).value);
  }
}
```

## Customizing the expand/collapse icon

Using the [column.renderTreeIcon=true](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderTreeIcon) is obviously not enough to customize the expand/collapse icon.

This prop can also be a function that returns a template (a `TemplateRef` of your component, read with `viewChild`), a component (`renderComponent(MyIcon)`) or a string. A template gets the arguments of the function as its context - `let-rowInfo="rowInfo"`, `let-toggleCurrentTreeNode="toggleCurrentTreeNode"`.

With the default value of `true` for [`columns.renderTreeIcon`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderTreeIcon), an icon will be rendered only for parent nodes.

If you want to render an icon for all nodes, specify a function (and differentiate between parent and leaf nodes), and it will be called regardless of whether the node is a parent or a leaf.

**Example: Customizing the expand/collapse icon**

```ts
import {
  Component,
  TemplateRef,
  computed,
  signal,
  viewChild,
} from '@angular/core';

import { TreeDataSource, TreeGrid } from '@infinite-table/infinite-angular';
import type { InfiniteTableColumn } from '@infinite-table/infinite-angular';

type FileSystemNode = {
  id: string;
  name: string;
  type: 'folder' | 'file';
  extension?: string;
  mimeType?: string;
  sizeInKB: number;
  children?: FileSystemNode[];
};

const allColumns: Record<string, InfiniteTableColumn<FileSystemNode>> = {
  name: { field: 'name', header: 'Name' },
  type: { field: 'type', header: 'Type' },
  extension: { field: 'extension', header: 'Extension' },
  mimeType: { field: 'mimeType', header: 'Mime Type' },
  size: { field: 'sizeInKB', type: 'number', header: 'Size (KB)' },
};

const dataSource = () => {
  const nodes: FileSystemNode[] = [
    {
      id: '1',
      name: 'Documents',
      sizeInKB: 1200,
      type: 'folder',
      children: [
        {
          id: '10',
          name: 'Private',
          sizeInKB: 100,
          type: 'folder',
          children: [
            {
              id: '100',
              name: 'Report.docx',
              sizeInKB: 210,
              type: 'file',
              extension: 'docx',
              mimeType: 'application/msword',
            },
            {
              id: '101',
              name: 'Vacation.docx',
              sizeInKB: 120,
              type: 'file',
              extension: 'docx',
              mimeType: 'application/msword',
            },
            {
              id: '102',
              name: 'CV.pdf',
              sizeInKB: 108,
              type: 'file',
              extension: 'pdf',
              mimeType: 'application/pdf',
            },
          ],
        },
      ],
    },
    {
      id: '2',
      name: 'Desktop',
      sizeInKB: 1000,
      type: 'folder',
      children: [
        {
          id: '20',
          name: 'unknown.txt',
          sizeInKB: 108,
          type: 'file',
          extension: 'txt',
          mimeType: 'text/plain',
        },
      ],
    },
    {
      id: '3',
      name: 'Media',
      sizeInKB: 1000,
      type: 'folder',
      children: [
        {
          id: '30',
          name: 'Music',
          sizeInKB: 0,
          type: 'folder',
          children: [],
        },
        {
          id: '31',
          name: 'Videos',
          sizeInKB: 5400,
          type: 'folder',
          children: [
            {
              id: '310',
              name: 'Vacation.mp4',
              sizeInKB: 108,
              type: 'file',
              extension: 'mp4',
              mimeType: 'video/mp4',
            },
          ],
        },
      ],
    },
  ];
  return Promise.resolve(nodes);
};

@Component({
  selector: 'demo-root',
  imports: [TreeDataSource, TreeGrid],
  template: `
    <infinite-tree-data-source
      nodesKey="children"
      primaryKey="id"
      [data]="dataSource"
    >
      <div style="color: var(--infinite-cell-color); padding: 10px">
        <p>Select the tree column</p>
        <select
          (change)="onTreeIconChange($event)"
          title="Select column to use for the tree icon"
        >
          @for (key of columnKeys; track key) {
            <option [value]="key" [selected]="key === treeIcon()">
              {{ key }}
            </option>
          }
          <option value="no" [selected]="treeIcon() === 'no'">
            No tree column
          </option>
        </select>
      </div>

      <infinite-tree-grid [columns]="columns()" />
    </infinite-tree-data-source>

    
    <ng-template
      #treeIconTemplate
      let-rowInfo="rowInfo"
      let-toggleCurrentTreeNode="toggleCurrentTreeNode"
    >
      <div
        style="width: 24px; font-size: 12px; display: inline-block; cursor: pointer"
        (click)="toggleCurrentTreeNode()"
      >
        {{ rowInfo.isParentNode ? (rowInfo.nodeExpanded ? '👇' : '👉') : '🔴' }}
      </div>
    </ng-template>
  `,
})
export default class App {
  protected readonly dataSource = dataSource;
  protected readonly columnKeys = Object.keys(allColumns);

  protected readonly treeIcon = signal<string>('name');

  private readonly treeIconTemplate =
    viewChild.required<TemplateRef<unknown>>('treeIconTemplate');

  protected readonly columns = computed(() => {
    const cols = { ...allColumns };
    const treeIcon = this.treeIcon();

    cols[treeIcon] = {
      ...cols[treeIcon],
      renderTreeIcon: () => this.treeIconTemplate(),
    };

    return cols;
  });

  protected onTreeIconChange(event: Event) {
    this.treeIcon.set((event.target as HTMLSelectElement).value);
  }
}
```
