# Column Rendering

> Customize column rendering for Infinite Table DataGrid to match your app and use custom components. Column styling and formatting, conditional rendering...

Canonical page: https://infinite-table.com/angular/docs/learn/columns/column-rendering

Columns render the [field](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.field) value of the data they are bound to. This is the default behavior, which can be customized in a number of ways that we're exploring below.

If you want to explicitly use the TypeScript type definition for columns, import the `InfiniteTableColumn` type

```
import { InfiniteTableColumn } from '@infinite-table/infinite-react'
```

Note that it's a generic type, so when you use it, you have to bind it to your `DATA_TYPE` (the type of your data object).

When using custom rendering or custom components for columns, make sure your rendering logic doesn't have local/transient state (a signal of a custom component that copies the cell's value, for example) - what a cell renders should come from the cell information, or from state that lives outside the cell.

This is important because `InfiniteTable` uses virtualization heavily, in both _column cells and column headers_, so **custom components can and will be destroyed and created again multiple times**, during the virtualization process (triggered by user scrolling, sorting, filtering and a few other interactions).

## Change the value using `valueGetter`

The simplest way to change what's being rendered in a column is to use the `valueGetter` prop and return a new value for the column.

```tsx
const nameColumn: InfiniteTableColumn<Employee> = {
  header: 'Employee Name',
  valueGetter: ({ data }) => `${data.firstName} ${data.lastName}`,
};
```

The [`columns.valueGetter`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.valueGetter) prop is a function that takes a single argument - an object with `data` and `field` properties.

Note that the [`columns.valueGetter`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.valueGetter) is only called for non-group rows, so the `data` property is of type `DATA_TYPE`.

**Example: Column with custom valueGetter**

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

import { DataSource, InfiniteTable } from '@infinite-table/infinite-angular';
import type { InfiniteTablePropColumns } from '@infinite-table/infinite-angular';

type Developer = {
  id: number;
  firstName: string;
  lastName: string;
  country: string;
  city: string;
  currency: string;
  preferredLanguage: string;
  stack: string;
  canDesign: 'yes' | 'no';
  hobby: string;
  salary: number;
  age: number;
};

const dataSource = () => {
  return fetch(process.env.NEXT_PUBLIC_BASE_URL + '/developers1k')
    .then((r) => r.json())
    .then((data: Developer[]) => data);
};

const columns: InfiniteTablePropColumns<Developer> = {
  id: { field: 'id', defaultWidth: 80 },
  name: {
    header: 'Full Name',
    valueGetter: ({ data }) => `${data.firstName} ${data.lastName}`,
  },

  preferredLanguage: { field: 'preferredLanguage' },
  stack: { field: 'stack' },
};

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source primaryKey="id" [data]="dataSource">
      <infinite-table
        debugId="column-valueGetter-example"
        [columns]="columns"
        [columnDefaultWidth]="200"
      />
    </infinite-data-source>
  `,
})
export default class App {
  protected readonly dataSource = dataSource;
  protected readonly columns = columns;
}
```

The column value getter should not return a template, a component or other markup, because the value returned by [`columns.valueGetter`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.valueGetter) will be used when the column is sorted (when sorting is done client-side and not remotely). For more in-depth information on sorting see [the column sorting page](./column-sorting).

## Use [renderValue](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderValue) and [render](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.render) to display custom content

The next step in customizing the rendering for a column is to use the [`columns.renderValue`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderValue) or the [`columns.render`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.render) props. In those functions, you have access to more information than in the [`columns.valueGetter`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.valueGetter) function. For example, you have access to the current value of `groupBy` and `pivotBy` props.

[renderValue](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderValue) and [render](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.render) can return a string, a number, a template (a `TemplateRef`), a component (`renderComponent(MyCell)`), or an array of those.

A template gets the object the function is called with as its context - `let-data="data"`, `let-rowInfo="rowInfo"` - and the value as `let-value`. The columns are a plain object, so a template of your component goes in through the function: `renderValue: () => this.myTemplate()`, with `myTemplate = viewChild.required<TemplateRef<unknown>>('myTemplate')`.

The [renderValue](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderValue) and [render](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.render) functions are called with an object that has the following properties:

- `data` - the data object (of type `DATA_TYPE | Partial<DATA_TYPE> | null`) for the row.
- `rowInfo` - very useful information about the current row:
  - `rowInfo.collapsed` - if the row is collased or not.
  - `rowInfo.groupBy` - the current group by for the row
  - `rowInfo.indexInAll` - the index of the row in the whole data set
  - `rowInfo.indexInGroup` - the index of the row in the current group
  - `rowInfo.value` - the value (only for group rows) that will be rendered by default in group column cells.
  - ... there are other useful properties that we'll document in the near future
- `column` - the current column being rendered
- `columnsMap` - the `Map` of columns available to the table. Note these might not be all visible. The keys in this map will be column ids.
- `fieldsToColumn` a `Map` that links `DataSource` fields to columns. Columns bound to fields (so with [`columns.field`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.field) specified) will be included in this `Map`.
- `api` - A reference to the [Infinite Table API](https://infinite-table.com/docs/reference/api/index.md) object.

[`columns.render`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.render) is the last function called in the rendering pipeline for a column cell, while [`columns.renderValue`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderValue) is called before render, towards the beginning of the [rendering pipeline (read more about this below)](#rendering-pipeline).

Avoid over-writing [`columns.render`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.render) for special columns (like group columns) unless you know what you're doing. Special columns use the `render` function to render additional content inside the column (eg: collapse/expand tool for group rows). The [`columns.render`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.render) function allows you to override this additional content. So if you specify this function, it's up to you to render whatever content, including the collapse/expand tool.

However, there are easier ways to override the collapse/expand group icon, like using [`columns.renderGroupIcon`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderGroupIcon).

A component you render from the [`columns.renderValue`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderValue) and [`columns.render`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.render) functions (and from the other rendering functions) can use [`injectColumnCell`](https://infinite-table.com/docs/reference/hooks/index.md#useInfiniteColumnCell) to retrieve the same params that are passed to the render functions.

It returns a signal: read it in the template of your component (or in a `computed`), not once in the constructor.

```ts
type Developer = { country: string; name: string; id: string };

@Component({
  selector: 'country-info',
  template: `<div>Country: {{ cell().value }}</div>`,
})
class CountryInfo {
  protected readonly cell = injectColumnCell<Developer>();
}

const columns = {
  country: {
    field: 'country',
    renderValue: () => renderComponent(CountryInfo),
  },
};
```

**Example: Column with custom renderValue**

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

import { DataSource, InfiniteTable } from '@infinite-table/infinite-angular';
import type {
  DataSourceGroupBy,
  InfiniteTableColumnRenderValueParam,
  InfiniteTablePropColumns,
  InfiniteTablePropGroupColumn,
} from '@infinite-table/infinite-angular';

type Developer = {
  id: number;
  firstName: string;
  lastName: string;
  country: string;
  city: string;
  currency: string;
  preferredLanguage: string;
  stack: string;
  canDesign: 'yes' | 'no';
  hobby: string;
  salary: number;
  age: number;
};

const dataSource = () => {
  return fetch(process.env.NEXT_PUBLIC_BASE_URL + '/developers1k')
    .then((r) => r.json())
    .then((data: Developer[]) => data);
};

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source
      primaryKey="id"
      [data]="dataSource"
      [defaultGroupBy]="defaultGroupBy"
    >
      <infinite-table
        debugId="column-renderValue-example"
        [groupColumn]="groupColumn"
        [columns]="columns"
        [columnDefaultWidth]="200"
      />
    </infinite-data-source>

    
    <ng-template #stack let-data="data">
      <b>🎇 {{ data?.stack }}</b>
    </ng-template>

    <ng-template #groupedBy let-rowInfo="rowInfo">
      Grouped by <b>{{ rowInfo.value }}</b>
    </ng-template>
  `,
})
export default class App {
  protected readonly dataSource = dataSource;

  private readonly stackTemplate =
    viewChild.required<TemplateRef<unknown>>('stack');
  private readonly groupedByTemplate =
    viewChild.required<TemplateRef<unknown>>('groupedBy');

  // the columns are a plain field: a template goes in through a function
  protected readonly columns: InfiniteTablePropColumns<Developer> = {
    id: { field: 'id', defaultWidth: 80 },
    stack: {
      field: 'stack',
      renderValue: ({ rowInfo }) => {
        if (rowInfo.isGroupRow) {
          return `${rowInfo.value} stuff`;
        }

        return this.stackTemplate();
      },
    },
    firstName: {
      field: 'firstName',
    },

    preferredLanguage: { field: 'preferredLanguage' },
  };

  protected readonly defaultGroupBy: DataSourceGroupBy<Developer>[] = [
    { field: 'stack' },
  ];

  protected readonly groupColumn: InfiniteTablePropGroupColumn<Developer> = {
    defaultWidth: 250,

    renderValue: ({
      rowInfo,
    }: InfiniteTableColumnRenderValueParam<Developer>) => {
      if (rowInfo.isGroupRow) {
        return this.groupedByTemplate();
      }
      return null;
    },
  };
}
```

Changing the group icon using `render`. The icon can also be changed using [`columns.renderGroupIcon`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderGroupIcon).

This snippet shows overriding the group collapse/expand tool via the [`columns.render`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.render) function.

**Example: Column with render - custom expand/collapse icon**

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

import { DataSource, InfiniteTable } from '@infinite-table/infinite-angular';
import type {
  DataSourceGroupBy,
  InfiniteTablePropColumns,
  InfiniteTablePropGroupColumn,
} from '@infinite-table/infinite-angular';

type Developer = {
  id: number;
  firstName: string;
  lastName: string;
  country: string;
  city: string;
  currency: string;
  preferredLanguage: string;
  stack: string;
  canDesign: 'yes' | 'no';
  hobby: string;
  salary: number;
  age: number;
};

const dataSource = () => {
  return fetch(process.env.NEXT_PUBLIC_BASE_URL + '/developers1k')
    .then((r) => r.json())
    .then((data: Developer[]) => data);
};

const columns: InfiniteTablePropColumns<Developer> = {
  id: { field: 'id', defaultWidth: 80 },
  stack: {
    field: 'stack',
  },
  firstName: {
    field: 'firstName',
  },
  preferredLanguage: { field: 'preferredLanguage' },
};

const defaultGroupBy: DataSourceGroupBy<Developer>[] = [{ field: 'stack' }];

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source
      primaryKey="id"
      [data]="dataSource"
      [defaultGroupBy]="defaultGroupBy"
    >
      <infinite-table
        debugId="column-render-example"
        [groupColumn]="groupColumn"
        [columns]="columns"
        [columnDefaultWidth]="200"
      />
    </infinite-data-source>

    
    <ng-template
      #groupCell
      let-rowInfo="rowInfo"
      let-toggleCurrentGroupRow="toggleCurrentGroupRow"
    >
      <div
        style="cursor: pointer; display: flex; align-items: center"
        [style.color]="rowInfo.collapsed ? '#b00000' : 'blue'"
        (click)="toggleCurrentGroupRow()"
      >
        <!-- &ngsp; is a space Angular keeps between two elements -->
        <i style="margin-right: 5px">Grouped by</i>&ngsp;<b>{{ rowInfo.value }}</b>
        <svg
          style="display: inline-block"
          [style.fill]="rowInfo.collapsed ? '#b00000' : 'blue'"
          width="20px"
          height="20px"
          viewBox="0 0 24 24"
          fill="#000000"
        >
          @if (rowInfo.collapsed) {
            <path d="M0 0h24v24H0V0z" fill="none" />
            <path
              d="M12 5.83L15.17 9l1.41-1.41L12 3 7.41 7.59 8.83 9 12 5.83zm0 12.34L8.83 15l-1.41 1.41L12 21l4.59-4.59L15.17 15 12 18.17z"
            />
          } @else {
            <path
              d="M7.41 18.59L8.83 20 12 16.83 15.17 20l1.41-1.41L12 14l-4.59 4.59zm9.18-13.18L15.17 4 12 7.17 8.83 4 7.41 5.41 12 10l4.59-4.59z"
            />
          }
        </svg>
      </div>
    </ng-template>
  `,
})
export default class App {
  protected readonly dataSource = dataSource;
  protected readonly columns = columns;
  protected readonly defaultGroupBy = defaultGroupBy;

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

  protected readonly groupColumn: InfiniteTablePropGroupColumn<Developer> = {
    defaultWidth: 250,
    render: ({ rowInfo }) =>
      rowInfo.isGroupRow ? this.groupCellTemplate() : null,
  };
}
```

This snippet shows how you can override the group collapse/expand tool via the [`columns.renderGroupIcon`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderGroupIcon) function.

**Example: Column with custom expand/collapse tool via renderGroupIcon**

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

import { DataSource, InfiniteTable } from '@infinite-table/infinite-angular';
import type {
  DataSourcePropGroupBy,
  InfiniteTableColumn,
  InfiniteTablePropColumns,
} from '@infinite-table/infinite-angular';

type Developer = {
  id: number;
  firstName: string;
  lastName: string;
  country: string;
  city: string;
  currency: string;
  preferredLanguage: string;
  stack: string;
  canDesign: 'yes' | 'no';
  hobby: string;
  salary: number;
  age: number;
};

const dataSource = () => {
  return fetch(process.env.NEXT_PUBLIC_BASE_URL + '/developers1k')
    .then((r) => r.json())
    .then((data: Developer[]) => data);
};

const columns: InfiniteTablePropColumns<Developer> = {
  id: { field: 'id', defaultWidth: 80 },
  stack: {
    field: 'stack',
  },
  firstName: {
    field: 'firstName',
  },

  preferredLanguage: { field: 'preferredLanguage' },
};
const groupBy: DataSourcePropGroupBy<Developer> = [
  {
    field: 'country',
    column: {
      header: 'Country group',
      renderGroupValue: ({ value }) => `Country: ${value}`,
    },
  },
  { field: 'preferredLanguage' },
];

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source [data]="dataSource" primaryKey="id" [groupBy]="groupBy">
      <infinite-table
        debugId="column-renderGroupIcon-example"
        [columns]="columns"
        [groupColumn]="groupColumn"
      />
    </infinite-data-source>

    
    <ng-template
      #groupIcon
      let-rowInfo="rowInfo"
      let-toggleCurrentGroupRow="toggleCurrentGroupRow"
    >
      <div
        (click)="toggleCurrentGroupRow()"
        style="cursor: pointer; margin-right: 10px"
      >
        {{ rowInfo.isGroupRow ? (rowInfo.collapsed ? '👇' : '👉') : '' }}
      </div>
    </ng-template>
  `,
})
export default class App {
  protected readonly dataSource = dataSource;
  protected readonly columns = columns;
  protected readonly groupBy = groupBy;

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

  protected readonly groupColumn: InfiniteTableColumn<Developer> = {
    renderGroupIcon: () => this.groupIconTemplate(),
  };
}
```

## Using inject functions for custom rendering

The components you render from the [`columns.render`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.render) and [`columns.renderValue`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderValue) functions (with `renderComponent`) are created with the cell's injector, so they can use the inject functions `InfiniteTable` provides. Call them in a field initializer or in the constructor of the component.

### Inject function: [`injectColumnCell`](https://infinite-table.com/docs/reference/hooks/index.md#useInfiniteColumnCell)

In a component rendered inside a column cell, you can use [`injectColumnCell`](https://infinite-table.com/docs/reference/hooks/index.md#useInfiniteColumnCell) to get access to the current cell's rendering information - the argument passed to the [render](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.render) or [renderValue](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderValue) functions - as a signal.

```ts
import { Component } from '@angular/core';
import {
  injectColumnCell,
  renderComponent,
} from '@infinite-table/infinite-angular';
import type { InfiniteTableColumn } from '@infinite-table/infinite-angular';

@Component({
  selector: 'custom-name',
  // the content of the cell, without a wrapper element
  host: { style: 'display: contents' },
  template: `<b>{{ cell().data?.firstName }}</b>, {{ cell().data?.lastName }}`,
})
class CustomName {
  protected readonly cell = injectColumnCell<Employee>();
}

const nameColumn: InfiniteTableColumn<Employee> = {
  header: 'Employee Name',
  renderValue: () => renderComponent(CustomName),
};
```

**Example: Column with render & useInfiniteColumnCell**

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

import {
  DataSource,
  InfiniteTable,
  injectColumnCell,
  renderComponent,
} from '@infinite-table/infinite-angular';
import type { InfiniteTablePropColumns } from '@infinite-table/infinite-angular';

type Developer = {
  id: number;
  firstName: string;
  lastName: string;
  country: string;
  city: string;
  currency: string;
  preferredLanguage: string;
  stack: string;
  canDesign: 'yes' | 'no';
  hobby: string;
  salary: number;
  age: number;
};

const dataSource = () => {
  return fetch(process.env.NEXT_PUBLIC_BASE_URL + '/developers1k')
    .then((r) => r.json())
    .then((data: Developer[]) => data);
};

const EMOJIS: Record<string, string> = {
  photography: '📸',
  cooking: '👨🏻‍🍳',
  dancing: '💃',
  reading: '📚',
  sports: '⛹️',
};

@Component({
  selector: 'custom-cell',
  template: `<b>{{ emoji() }} + {{ label() }}</b>`,
})
class CustomCell {
  
  private readonly cell = injectColumnCell<Developer>();

  protected readonly emoji = computed(() => EMOJIS[this.cell().value] ?? '🤷');
  protected readonly label = computed(() =>
    this.cell().data?.stack === 'frontend' ? '⚛️' : '',
  );
}

const columns: InfiniteTablePropColumns<Developer> = {
  id: { field: 'id', maxWidth: 80 },
  firstName: { field: 'firstName' },
  hobby: {
    field: 'hobby',
    // we're not using the arg of the render function directly
    // but CustomCell uses `injectColumnCell()` to retrieve it instead
    render: () => renderComponent(CustomCell),
  },
};

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source primaryKey="id" [data]="dataSource">
      <infinite-table
        debugId="column-render-hooks-example"
        [columns]="columns"
        [columnDefaultWidth]="200"
      />
    </infinite-data-source>
  `,
})
export default class App {
  protected readonly dataSource = dataSource;
  protected readonly columns = columns;
}
```

### Inject function: [`injectHeaderCell`](https://infinite-table.com/docs/reference/hooks/index.md#useInfiniteHeaderCell)

For column headers, a component rendered inside the header (with `renderComponent`) can use [`injectHeaderCell`](https://infinite-table.com/docs/reference/hooks/index.md#useInfiniteHeaderCell) to get access to the current header's rendering information - the argument passed to the [`columns.header`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.header) function - as a signal.

```ts
import { Component } from '@angular/core';
import {
  injectHeaderCell,
  renderComponent,
} from '@infinite-table/infinite-angular';
import type { InfiniteTableColumn } from '@infinite-table/infinite-angular';

@Component({
  selector: 'custom-header',
  template: `<b>{{ headerCell().column.field }}</b>`,
})
class CustomHeader {
  protected readonly headerCell = injectHeaderCell<Employee>();
}

const nameColumn: InfiniteTableColumn<Employee> = {
  field: 'firstName',
  header: () => renderComponent(CustomHeader),
};
```

**Example: Column Header with render & useInfiniteHeaderCell**

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

import {
  DataSource,
  InfiniteTable,
  injectHeaderCell,
} from '@infinite-table/infinite-angular';
import type { InfiniteTablePropColumns } from '@infinite-table/infinite-angular';

type Developer = {
  id: number;
  firstName: string;
  lastName: string;
  country: string;
  city: string;
  currency: string;
  preferredLanguage: string;
  stack: string;
  canDesign: 'yes' | 'no';
  hobby: string;
  salary: number;
  age: number;
};

const dataSource = () => {
  return fetch(process.env.NEXT_PUBLIC_BASE_URL + '/developers1k')
    .then((r) => r.json())
    .then((data: Developer[]) => data);
};

// the host element of the component is the header cell
@Component({
  selector: 'div[hobbyHeader]',
  template: `<b style="color: #0000c2">{{ headerCell().column.field }} 🤷📸👨🏻‍🍳💃📚⛹️</b>`,
})
class HobbyHeader {
  
  protected readonly headerCell = injectHeaderCell<Developer>();
}

const columns: InfiniteTablePropColumns<Developer> = {
  id: { field: 'id', maxWidth: 80 },
  stack: {
    field: 'stack',
  },
  hobby: {
    field: 'hobby',
    components: {
      HeaderCell: HobbyHeader,
    },
  },
};

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source primaryKey="id" [data]="dataSource">
      <infinite-table
        debugId="column-header-hooks-example"
        [columns]="columns"
        [columnDefaultWidth]="200"
      />
    </infinite-data-source>
  `,
})
export default class App {
  protected readonly dataSource = dataSource;
  protected readonly columns = columns;
}
```

## Use [column.components](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.components) to customize the column

There are cases when custom rendering via the [`columns.render`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.render) and [`columns.renderValue`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderValue) props is not enough and you want to fully control the column cell and render your own custom component for that.

For such scenarios, you can specify `column.components.HeaderCell` and `column.components.ColumnCell`, which will use those components to render the DOM nodes of the column header and column cells respectively.

```ts
import type { InfiniteTableColumn } from '@infinite-table/infinite-angular';

// the host element of the component is the cell
@Component({
  selector: 'div[redColumnCell]',
  imports: [InfiniteRender],
  host: { style: 'color: red' },
  // the default content of the cell
  template: `<ng-container [infiniteRender]="children()" />`,
})
class ColumnCell {
  protected readonly children = injectColumnCellChildren();
}

// the host element of the component is the header cell
@Component({
  selector: 'div[redHeaderCell]',
  imports: [InfiniteRender],
  host: { style: 'color: red' },
  template: `<ng-container
      [infiniteRender]="headerCell().renderBag.sortIcon"
    />First name`,
})
class HeaderCell {
  protected readonly headerCell = injectHeaderCell<Developer>();
}

const nameColumn: InfiniteTableColumn<Developer> = {
  header: 'Name',
  field: 'firstName',
  components: {
    ColumnCell,
    HeaderCell,
  },
};
```

A custom component is the cell (or the header cell) itself: give it an attribute selector on a `div` (`selector: 'div[myColumnCell]'`). The grid creates the cell element with your component on it and adds the class names, styles and event listeners of the cell to that element - the `host` styles and classes of your component are merged with them.

```ts
// a component specified in column.components.ColumnCell:
// its host element is the cell
@Component({
  selector: 'div[customColumnCell]',
  imports: [InfiniteRender],
  // the default content of the cell, where React renders `children`
  template: `<ng-container [infiniteRender]="children()" />`,
})
class ColumnCell {
  protected readonly children = injectColumnCellChildren();
}
```

The default content of the cell is a renderable you get with `injectColumnCellChildren()` (`injectHeaderCellChildren()` for header cells): render it with `<ng-container [infiniteRender]="children()" />` where you want it.

There is no `domRef` to pass on - the host element of your component is the cell. The cell information is available through [`injectColumnCell`](https://infinite-table.com/docs/reference/hooks/index.md#useInfiniteColumnCell) ([`injectHeaderCell`](https://infinite-table.com/docs/reference/hooks/index.md#useInfiniteHeaderCell) for header cells).

**Example: Custom components**

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

import {
  DataSource,
  InfiniteRender,
  InfiniteTable,
  injectColumnCell,
  injectColumnCellChildren,
  injectHeaderCell,
} from '@infinite-table/infinite-angular';
import type {
  InfiniteTablePropColumns,
  InfiniteTablePropColumnTypes,
} from '@infinite-table/infinite-angular';

type Developer = {
  id: number;
  firstName: string;
  lastName: string;
  country: string;
  city: string;
  currency: string;
  preferredLanguage: string;
  stack: string;
  canDesign: 'yes' | 'no';
  hobby: string;
  salary: number;
  age: number;
};

const dataSource = () => {
  return fetch(process.env.NEXT_PUBLIC_BASE_URL + '/developers1k')
    .then((r) => r.json())
    .then((data: Developer[]) => data);
};

// the host element of the component is the header cell
@Component({
  selector: 'div[defaultHeaderComponent]',
  host: { style: 'border: 1px solid #fefefe' },
  // here you would usually render the default children
  // (`injectHeaderCellChildren()`) and the sort tool, but in this case we
  // want to override the default sort tool as well (which is part of the
  // default children)
  template: `{{ headerCell().column.field }} {{ sortTool() }}`,
})
class DefaultHeaderComponent {
  protected readonly headerCell = injectHeaderCell<Developer>();

  protected readonly sortTool = computed(() => {
    switch (this.headerCell().columnSortInfo?.dir) {
      case undefined:
        return '👉';
      case 1:
        return '👇';
      case -1:
        return '☝🏽';
    }
    return '';
  });
}

// the host element of the component is the cell
@Component({
  selector: 'div[stackComponent]',
  imports: [InfiniteRender],
  host: { '[style]': 'style()' },
  template: `<ng-container [infiniteRender]="children()" />
    <div style="flex: 1"></div>
    {{ emoji() }}`,
})
class StackComponent {
  private readonly cell = injectColumnCell<Developer>();
  // the default content of the cell
  protected readonly children = injectColumnCellChildren();

  private readonly isFrontEnd = computed(
    () => this.cell().value === 'frontend',
  );

  protected readonly emoji = computed(() => (this.isFrontEnd() ? '⚛️' : '💽'));

  protected readonly style = computed(() => ({
    padding: '5px 20px',
    border: `1px solid ${this.isFrontEnd() ? 'red' : 'green'}`,
  }));
}

const columnTypes: InfiniteTablePropColumnTypes<Developer> = {
  default: {
    // override all columns to use these components
    components: {
      HeaderCell: DefaultHeaderComponent,
    },
  },
};

const columns: InfiniteTablePropColumns<Developer> = {
  id: { field: 'id', defaultWidth: 80 },
  stack: {
    field: 'stack',
    renderValue: ({ data }) => 'Stack: ' + data?.stack,
    components: {
      HeaderCell: DefaultHeaderComponent,
      ColumnCell: StackComponent,
    },
  },
  firstName: {
    field: 'firstName',
  },
  preferredLanguage: {
    field: 'preferredLanguage',
  },
};

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source primaryKey="id" [data]="dataSource">
      <infinite-table
        debugId="column-components-example"
        [columns]="columns"
        [columnTypes]="columnTypes"
      />
    </infinite-data-source>
  `,
})
export default class App {
  protected readonly dataSource = dataSource;
  protected readonly columns = columns;
  protected readonly columnTypes = columnTypes;
}
```

If the above [`columns.components`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.components) is still not enough, read about the rendering pipeline below.

## Rendering pipeline

The rendering pipeline for columns is a series of functions defined on the column that are called while rendering.

All the functions that have the word `render` in their name will be called with an object that has a `renderBag` property, which contains values that will be rendered.

The default [`columns.render`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.render) function (the last one in the pipeline) ends up rendering a few things:

- a `value` - generally comes from the [field](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.field) the column is bound to
- a `groupIcon` - for group columns
- a `selectionCheckBox` - for columns that have [`columns.renderSelectionCheckBox`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderSelectionCheckBox) defined (combined with row selection)

When the rendering process starts for a column cell, all the above end up in the `renderBag` object.

### Rendering pipeline - `renderBag.value`

As already mentioned, the `value` defaults to the value of the column [field](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.field) for the current row.

If the column is not bound to a field, you can define a [valueGetter](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.valueGetter). The [valueGetter](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.valueGetter) only has access to `{data, field?}` in order to compute a value and return it.

After the [valueGetter](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.valueGetter) is called, the [valueFormatter](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.valueFormatter) is next in the rendering pipeline.

This is called with more details about the current cell

```tsx
const column: InfiniteTableColumn<T> = {
  // the valueGetter can be useful when rows are nested objects
  // or you want to compose multiple values from the row
  valueGetter: ({ data }) => {
    return data.person.salary * 10;
  },
  valueFormatter: ({
    value,
    isGroupRow,
    data,
    field,
    rowInfo,
    rowSelected,
    rowActive,
  }) => {
    // the value here is what the `valueFormatter` returned
    return `USD ${value}`;
  },
};
```

After [valueGetter](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.valueGetter) and [valueFormatter](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.valueFormatter) are called, the resulting value is the actual value used for the cell. This value will also be assigned to `renderBag.value`

When [renderValue](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderValue) and [render](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.render) are called by `InfiniteTable`, both `value` and `renderBag` will be available as properties to the arguments object.

```ts
@Component({
  selector: 'hello-world-column',
  imports: [InfiniteRender],
  template: `
    <ng-template #bold let-value>
      <b>{{ value }}</b>
    </ng-template>

    <ng-template #hello let-renderBag="renderBag">
      <div>Hello <ng-container [infiniteRender]="renderBag.value" />!</div>
    </ng-template>
  `,
})
class HelloWorldColumn {
  private readonly bold = viewChild.required<TemplateRef<unknown>>('bold');
  private readonly hello = viewChild.required<TemplateRef<unknown>>('hello');

  readonly column: InfiniteTableColumn<T> = {
    valueGetter: () => 'world',
    renderValue: ({ value }) => {
      // at this stage, `value` is 'world' and `renderBag.value` has the same value, 'world'
      return this.bold();
    },

    render: ({ renderBag }) => {
      // at this stage `value` is 'world'
      // but `renderBag.value` is the #bold template, as this was the value returned by `renderValue`
      return this.hello();
    },
  };
}
```

After the [renderValue](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderValue) function is called, the following are also called (if available):

- [renderGroupValue](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderGroupValue) - for group rows
- [renderLeafValue](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderLeafValue) - for leaf rows

You can think of them as an equivalent to [renderValue](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderValue), but narrowed down to group/non-group rows.

Inside those functions, the `renderBag.value` refers to the value returned by the [renderValue](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderValue) function.

### Rendering pipeline - `renderBag.groupIcon`

In a similar way to `renderBag.value`, the `renderBag.groupIcon` is also piped through to the [render](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.render) function.

```ts
const column: InfiniteTableColumn<T> = {
  renderGroupIcon: ({ renderBag }) => {
    return [' [ ', renderBag.groupIcon, ' ] '];
  },
  render: ({ renderBag }) => {
    return [
      // use the groupIcon from the renderBag
      renderBag.groupIcon,
      renderBag.value,
    ];
  },
};
```

Inside [`columns.renderGroupIcon`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderGroupIcon), you have access to `renderBag.groupIcon`, which is basically the default group icon - so you can use that if you want, and build on that.

Also inside [`columns.renderGroupIcon`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderGroupIcon), you have access to `toggleGroupRow` so you can properly hook the collapse/expand behaviour to your custom group icon.

### Rendering pipeline - `renderBag.selectionCheckBox`

Like with the previous properties of `renderBag`, you can customize the `selectionCheckBox` (used when multiple selection is configured) to be piped-through - for columns that specify [`columns.renderSelectionCheckBox`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderSelectionCheckBox).

```ts
@Component({
  selector: 'custom-selection-column',
  template: `
    <ng-template
      #selection
      let-rowSelected="rowSelected"
      let-isGroupRow="isGroupRow"
      let-toggleRow="toggleCurrentRowSelection"
      let-toggleGroupRow="toggleCurrentGroupRowSelection"
    >
      <!-- rowSelected is null for a group row with an indeterminate state -->
      <div (click)="isGroupRow ? toggleGroupRow() : toggleRow()">
        [ {{ rowSelected === null ? '-' : rowSelected ? 'x' : 'o' }} ]
      </div>
    </ng-template>
  `,
})
class CustomSelectionColumn {
  private readonly selection =
    viewChild.required<TemplateRef<unknown>>('selection');

  readonly column: InfiniteTableColumn<T> = {
    renderSelectionCheckBox: (params) => {
      if (!params.rowInfo) {
        // the function is also called for the column header
        return params.renderBag.selectionCheckBox;
      }
      // you could return renderBag.selectionCheckBox to have the default checkbox
      return this.selection();
    },
    render: ({ renderBag }) => {
      return [
        // use the selectionCheckBox from the renderBag
        renderBag.selectionCheckBox,
        renderBag.groupIcon,
        renderBag.value,
      ];
    },
  };
}
```

To recap, here is the full list of the functions in the rendering pipeline, in order of invocation:

1. [`columns.valueGetter`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.valueGetter) - doesn't have access to `renderBag`
2. [`columns.valueFormatter`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.valueFormatter) - doesn't have access to `renderBag`
3. [`columns.renderGroupIcon`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderGroupIcon) - can use all properties in `renderBag`
4. [`columns.renderSelectionCheckBox`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderSelectionCheckBox) - can use all properties in `renderBag`
5. [`columns.renderValue`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderValue) - can use all properties in `renderBag`
6. [`columns.renderGroupValue`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderGroupValue) - can use all properties in `renderBag`
7. [`columns.renderLeafValue`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderLeafValue) - can use all properties in `renderBag`
8. [`columns.render`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.render) - can use all properties in `renderBag`

Additionally, the [`columns.components.ColumnCell`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.components.ColumnCell) custom component does have access to the `renderBag` via [`injectColumnCell`](https://infinite-table.com/docs/reference/hooks/index.md#useInfiniteColumnCell)
