# Cell Selection

> InfiniteTable DataGrid component supports single and multiple cell selection.

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

To use multi-cell selection, you need to configure the `<DataSource />` component with `selectionMode="multi-cell"` - see [`selectionMode`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#selectionMode) for details. For selecting rows, see the [Row Selection](https://infinite-table.com/angular/docs/learn/selection/row-selection.md) page.

```ts
<infinite-data-source primaryKey="id" [data]="data" selectionMode="multi-cell" />

<!-- can be "single-row", "multi-row", "multi-cell" or false -->
```

Click cells in the grid to add to the selection.

Use `Shift+Click` to select a range of cells and `Cmd/Ctrl+Click` to add single cells to the selection.

**Example: Multiple cell selection example**

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

import { InfiniteTable, DataSource } 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: 60 },
  firstName: { field: 'firstName' },
  preferredLanguage: { field: 'preferredLanguage' },
  stack: { field: 'stack' },
  country: { field: 'country' },
  age: { field: 'age', type: 'number' },
  salary: { field: 'salary', type: 'number' },
  currency: { field: 'currency', type: 'number' },
};

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

## Using default selection

You can specify a default value for cell selection by using the [`defaultCellSelection`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#defaultCellSelection) prop.

```ts
// <infinite-data-source
//   primaryKey="id"
//   [data]="data"
//   selectionMode="multi-cell"
//   [defaultCellSelection]="defaultCellSelection"
// />
protected readonly defaultCellSelection: DataSourcePropCellSelection_MultiCell =
  {
    defaultSelection: false,
    selectedCells: [
      [3, 'stack'], // rowId + colId
      [5, 'stack'], // rowId + colId
      [0, 'firstName'], // rowId + colId
    ],
  };
```

Cell selection uses `[rowId, colId]` cell descriptors to identify cells to be marked as selected or deselected - read more in the [Cell selection format](#cell-selection-format).

By default some cells are already selected in the grid below, by using the [`defaultCellSelection`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#defaultCellSelection) prop on the `<DataSource />` component.

**Example: Multiple cell selection with a default selection value**

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

import { InfiniteTable, DataSource } from '@infinite-table/infinite-angular';
import type {
  InfiniteTablePropColumns,
  DataSourcePropCellSelection_MultiCell,
} 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: 60 },
  firstName: { field: 'firstName' },
  preferredLanguage: { field: 'preferredLanguage' },
  stack: { field: 'stack' },
  country: { field: 'country' },
  age: { field: 'age', type: 'number' },
  salary: { field: 'salary', type: 'number' },
  currency: { field: 'currency', type: 'number' },
};

const defaultCellSelection: DataSourcePropCellSelection_MultiCell = {
  defaultSelection: false,
  selectedCells: [
    [3, 'stack'], // rowId + colId
    [4, 'stack'],
    [5, 'stack'],
    [0, 'firstName'],
  ],
};

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

Whe you're using cell selection with or without any default value (via the [`defaultCellSelection`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#defaultCellSelection)), you're using an uncontrolled prop. This means that the selection state is managed by the `<DataSource />` component and not by you. If you want to control the selection state yourself, you can use the controlled [`[(cellSelection)]`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#cellSelection) prop instead - see [Using controlled selection](#using-controlled-selection) for details.

## Cell selection format

The [`[(cellSelection)]`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#cellSelection) prop is an object with the following shape:

- `defaultSelection` - `boolean` - whether or not cells are selected by default.
- either:
  - `selectedCells`: `[rowId, colId][]` - an array of cells that should be selected (this is combined with `defaultSelection: false`)
- or
  - `deselectedCells`: `[rowId, colId][]` - an array of cells that should be deselected (this is combined with `defaultSelection: true`)

When `defaultSelection` is `true`, you will only need to specify the `deselectedCells` prop.

And when `defaultSelection` is `false`, you will only need to specify the `selectedCells` prop.

In this way, you can either specify which cells should be selected or which cells should be deselected - and have a default that matches the most common case.

The `selectedCells`/`deselectedCells` are arrays of `[rowId, colId]` tuples. The `rowId` is the `id` of the row ([the primary key](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#primaryKey)), and the `colId` is the `id` of the column (the identifier of the column in the [`columns`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns) prop).

The following scenarios are all possible:

```tsx
const defaultCellSelection = {
  defaultSelection: false,
  selectedCells: [
    ['id2', 'stack'],
    ['id2', 'stack'],
    ['id0', 'firstName'],
  ],
};
```

```tsx
const defaultCellSelection = {
  defaultSelection: true,
  deselectedCells: [
    ['row2', 'stack'],
    ['row3', 'stack'],
    ['row5', 'firstName'],
  ],
};
```

### Using wildcards for selection

It's also possible to use wildcards for selecting cells. This is useful if you want to select all cells in a column, or all cells in a row.

```tsx
const defaultCellSelection = {
  defaultSelection: false,
  selectedCells: [
    ['*', 'stack'],
    ['row2', 'firstName'],
  ],
};
```

```tsx
const defaultCellSelection = {
  defaultSelection: false,
  selectedCells: [
    ['row1', '*'],
    ['row2', 'firstName'],
  ],
};
```

```tsx
const defaultCellSelection = {
  defaultSelection: true,
  deselectedCells: [['*', 'stack']],
};
```

## Using controlled selection

The controlled [`[(cellSelection)]`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#cellSelection) is a model: bind it two-way to a signal, and the grid writes the selection the user makes back to your signal. To react to the change, use the [`(cellSelectionChange)`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#onCellSelectionChange) output.

This example binds the [`[(cellSelection)]`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#cellSelection) model of the `<DataSource />` component two-way to a signal, and shows the current value of the signal above the grid.

**Example: Using controlled cell selection**

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

import { InfiniteTable, DataSource } from '@infinite-table/infinite-angular';
import type {
  InfiniteTablePropColumns,
  DataSourcePropCellSelection_MultiCell,
} 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: 60 },
  firstName: { field: 'firstName' },
  preferredLanguage: { field: 'preferredLanguage' },
  stack: { field: 'stack' },
  country: { field: 'country' },
  age: { field: 'age', type: 'number' },
  salary: { field: 'salary', type: 'number' },
  currency: { field: 'currency', type: 'number' },
};

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <div
      style="
        display: flex;
        flex: 1;
        color: var(--infinite-cell-color);
        flex-flow: column;
        background: var(--infinite-background);
      "
    >
      <div style="max-height: 200px; overflow: auto; border: 2px solid magenta">
        Current selection:
        <pre>{{ cellSelectionJSON() }}</pre>
      </div>

      <infinite-data-source
        primaryKey="id"
        [data]="dataSource"
        [(cellSelection)]="cellSelection"
        selectionMode="multi-cell"
      >
        <infinite-table
          debugId="controlled-cell-selection-example"
          [columns]="columns"
          [columnDefaultWidth]="100"
        />
      </infinite-data-source>
    </div>
  `,
})
export default class App {
  protected readonly dataSource = dataSource;
  protected readonly columns = columns;

  
  protected readonly cellSelection =
    signal<DataSourcePropCellSelection_MultiCell>({
      defaultSelection: false,
      selectedCells: [
        [3, 'stack'],
        [0, 'firstName'],
      ],
    });

  protected readonly cellSelectionJSON = computed(() =>
    JSON.stringify(this.cellSelection(), null, 2),
  );
}
```

## Using the Cell Selection API

The `<DataSource />` component also exposes a [Cell Selection API](https://infinite-table.com/docs/reference/cell-selection-api/index.md), which you can use to select and deselect cells programmatically.

**Example: Using the CellSelectionAPI to select a column**

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

import { DataSource, InfiniteTable } from '@infinite-table/infinite-angular';
import type {
  DataSourcePropCellSelection_MultiCell,
  InfiniteTableApi,
  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: 60 },
  firstName: { field: 'firstName' },
  preferredLanguage: { field: 'preferredLanguage' },
  stack: { field: 'stack' },
  country: { field: 'country' },
  age: { field: 'age', type: 'number' },
  salary: { field: 'salary', type: 'number' },
  currency: { field: 'currency', type: 'number' },
};

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <div
      style="
        display: flex;
        flex: 1;
        color: var(--infinite-cell-color);
        flex-flow: column;
        background: var(--infinite-background);
      "
    >
      <button
        style="
          margin: 10px;
          padding: 10px;
          border-radius: 5px;
          border: 2px solid magenta;
        "
        (click)="api()?.cellSelectionApi?.selectColumn('firstName')"
      >
        Select "firstName" column
      </button>
      <div style="max-height: 200px; overflow: auto; border: 2px solid magenta">
        Current selection:
        <pre>{{ cellSelectionJSON() }}</pre>
      </div>

      <infinite-data-source
        primaryKey="id"
        [data]="dataSource"
        [(cellSelection)]="cellSelection"
        selectionMode="multi-cell"
      >
        <infinite-table
          debugId="controlled-cell-selection-with-api-example"
          [columns]="columns"
          [columnDefaultWidth]="100"
          (ready)="api.set($event.api)"
        />
      </infinite-data-source>
    </div>
  `,
})
export default class App {
  protected readonly dataSource = dataSource;
  protected readonly columns = columns;

  protected readonly cellSelection =
    signal<DataSourcePropCellSelection_MultiCell>({
      defaultSelection: false,
      selectedCells: [
        [3, 'stack'],
        [0, 'firstName'],
      ],
    });

  protected readonly cellSelectionJSON = computed(() =>
    JSON.stringify(this.cellSelection(), null, 2),
  );

  
  protected readonly api = signal<InfiniteTableApi<Developer> | null>(null);
}
```
