# Working with Columns

> Define columns to configure your Infinite Table Angular DataGrid - fixed and flexible columns, resize, column groups and more

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

Columns are a central feature in `InfiniteTable`.

You define columns as a an object, with keys being column ids while values are the column definitions.

You then use them in the `columns` prop in your `InfiniteTable` component.

The [`columns`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns) prop is typed either as

- `Record<string, InfiniteTableColumn<DATA_TYPE>>`
- or `InfiniteTablePropColumns<DATA_TYPE>`, which is an alias for the type above

In `InfiniteTable`, columns are identified by their key in the [`columns`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns) object. **We'll refer to this as the column id**.
The column ids are used in many places - like defining the [column order](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columnOrder), column pinning, column visibility, etc.

```ts
export type Employee = {
  id: number;
  companyName: string;
  firstName: string;
  lastName: string;
  country: string;
  city: string;
  department: string;
  team: string;
  salary: number;
};

// InfiniteTableColumn is a generic type, you have to bind it to a specific data-type
import type { InfiniteTableColumn } from '@infinite-table/infinite-angular';

// we're binding it here to the `Employee` type
// which means the `column.field` has to be `keyof Employee`
const columns: Record<string, InfiniteTableColumn<Employee>> = {
  firstName: {
    field: 'firstName',
    header: 'First Name',
  },
  country: {
    field: 'country',
  },
  city: {
    field: 'city',
  },
  salary: {
    field: 'salary',
    type: 'number',
  },
};
// <infinite-table [columns]="columns" />
```

It's very important to remember you should not pass a different reference of an input on each change detection. `<infinite-table />` is optimized to only re-render when its inputs change - so if you give it new objects every time, you will get a performance penalty.

Define the columns as a field of your component (or a module-level `const`) - or as a `signal`, when they change - and bind them with `[columns]="columns"`. Don't bind a getter or a method call that creates a new object every time Angular checks the template.

**Example: Basic Column Configuration**

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

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

type Employee = {
  id: number;
  companyName: string;
  companySize: string;
  firstName: string;
  lastName: string;
  country: string;
  countryCode: string;
  city: string;
  streetName: string;
  streetNo: string;
  department: string;
  team: string;
  salary: number;
  age: number;
  email: string;
};

const columns: Record<string, InfiniteTableColumn<Employee>> = {
  firstName: {
    field: 'firstName',
    header: 'First Name',
  },
  country: {
    field: 'country',
    header: 'Country',
    columnGroup: 'location',
  },
  city: {
    field: 'city',
    header: 'City',
    columnGroup: 'address',
  },
  salary: {
    field: 'salary',
    type: 'number',
    header: 'Salary',
  },
  department: {
    field: 'department',
    header: 'Department',
  },
  team: {
    field: 'team',
    header: 'Team',
  },
  company: { field: 'companyName', header: 'Company' },

  companySize: {
    field: 'companySize',
    header: 'Company Size',
  },
};

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

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

Find out how to render custom content inside columns or even take full control of column cells and header.

## Column Types

Column types allow you to customize column behavior and appearance for multiple columns at once. Most of the properties available for columns are also available for column types - for a full list, see [columnTypes](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columnTypes) reference.

There are two special [column types](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.type) for now, but more are coming soon:

- `default` - all columns have this type, if not otherwise specified. The type does not contain any configuration, but allows you to define it and apply common configuration to all columns.
- `number` - if specified on a column (in combination with local uncontrolled sorting), the column will be sorted numerically.

Find out how to use column types to customize the appearance and behaviour of your columns.

## Column Order

The implicit column order is the order in which columns have been defined in the [`columns`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns) object. You can however control that explicitly by using the `columnOrder: string[]` prop.

```ts
@Component({
  selector: 'column-order-snippet',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source primaryKey="id" [data]="data">
      <infinite-table [columns]="columns" [(columnOrder)]="columnOrder" />
    </infinite-data-source>
  `,
})
export class ColumnOrderSnippet {
  protected readonly data = data;
  protected readonly columns = columns;

  // a model: the grid writes the new order back when the user reorders the columns
  protected readonly columnOrder = signal(['firstName', 'id', 'curency']);
}
```

The [`[(columnOrder)]`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columnOrder) prop is an array of strings, representing the column ids. A column id is the key of the column in the [`columns`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns) object.

The [`[(columnOrder)]`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columnOrder) array can contain identifiers that are not yet defined in the [`columns`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns) Map, or can contain duplicate ids. This is a feature, not a bug. We want to allow you to use the [`[(columnOrder)]`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columnOrder) in a flexible way so it can define the order of current and future columns.

[`[(columnOrder)]`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columnOrder) is a controlled prop. For the uncontrolled version, see [`defaultColumnOrder`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#defaultColumnOrder)

[`[(columnOrder)]`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columnOrder) is a model: bind it two-way to a signal, so the grid writes the new order back when the user reorders the columns. To react to the change, use the [`(columnOrderChange)`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#onColumnOrderChange) output.

**Example: Column Order demo, with firstName col displayed twice**

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

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

type Employee = {
  id: number;
  companyName: string;
  companySize: string;
  firstName: string;
  lastName: string;
  country: string;
  countryCode: string;
  city: string;
  streetName: string;
  streetNo: string;
  department: string;
  team: string;
  salary: number;
  age: number;
  email: string;
};

const columns: InfiniteTablePropColumns<Employee> = {
  firstName: {
    field: 'firstName',
    header: 'First Name',
  },
  country: {
    field: 'country',
    header: 'Country',
    columnGroup: 'location',
  },

  city: {
    field: 'city',
    header: 'City',
    columnGroup: 'address',
  },
  salary: {
    field: 'salary',
    type: 'number',
    header: 'Salary',
  },
  department: {
    field: 'department',
    header: 'Department',
  },
  team: {
    field: 'team',
    header: 'Team',
  },
  company: { field: 'companyName', header: 'Company' },

  companySize: {
    field: 'companySize',
    header: 'Company Size',
  },
};

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

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <div style="color: var(--infinite-cell-color)">
      <p>
        Current column order:
        <code>
          <pre>{{ columnOrder().join(', ') }}.</pre>
        </code>
      </p>
      <p>Drag column headers to reorder.</p>
    </div>

    <infinite-data-source [data]="dataSource" primaryKey="id">
      <infinite-table
        debugId="columnOrder-example"
        [columns]="columns"
        [(columnOrder)]="columnOrder"
        [columnDefaultWidth]="200"
      />
    </infinite-data-source>
  `,
})
export default class App {
  protected readonly dataSource = dataSource;
  protected readonly columns = columns;

  
  protected readonly columnOrder = signal<string[]>([
    'firstName',
    'country',
    'team',
    'company',
    'department',
    'companySize',
  ]);
}
```

By keeping the column order simple, namely an array of strings, ordering becomes much easier.

The alternative would be to make `columns` an array, which most DataGrids do - and whenever they are reordered, a new `columns` array would be needed.
