Adaptable

#injectCellSelection

injectCellSelection() (the Angular counterpart of the useCellSelection hook) gives you a signal of the selected cells of the current view — their row ids, column ids, values and data types. The signal gets a new value when the selection changes. Use it to build summaries, charts or actions over the user's selection. Call it in an injection context (a field initializer or the constructor) of a component rendered inside <adaptable-provider>.

Note

Cell selection needs the view's selection.mode to be 'multi-cell' (the default) — see Multi-cell selection. For selected rows, use injectRowSelection.

#Return value

The inject function returns a read-only Signal. Its value is null when no cell is selected. Otherwise it is an object with:

  • columnIds — the ids of every column that has at least one selected cell. The selection does not have to be contiguous.

  • positions — one array per row that has a selection. Each inner array lines up with columnIds. A selected cell is an object with:

    • rowId — the primary key of the row.
    • value — the cell value.
    • columnId — the id of the column.
    • columnDataType — the data type of the column ('text', 'number', 'date', …).

    A cell in columnIds that is not selected in that row is an empty string.

The value of injectCellSelection()
{
  columnIds: ['firstName', 'salary', 'country'],
  positions: [
    [
      { rowId: 1, value: 'Emma', columnId: 'firstName', columnDataType: 'text' },
      { rowId: 1, value: 89000, columnId: 'salary', columnDataType: 'number' },
      '', // country is not selected in this row
    ],
    [
      { rowId: 3, value: 'Ines', columnId: 'firstName', columnDataType: 'text' },
      '', // salary is not selected in this row
      { rowId: 3, value: 'Japan', columnId: 'country', columnDataType: 'text' },
    ],
  ],
}

#Usage

Call cellSelection() to read the value, in the template and in the class.

Using injectCellSelection
import { Component, computed } from '@angular/core';
import {
  AdaptableProvider,
  AdaptableUI,
  injectCellSelection,
} from '@adaptabletools/datagrid-angular';

@Component({
  selector: 'selected-cells-table',
  template: `
    @if (cellSelection(); as cellSelection) {
      <table>
        <thead>
          <tr>
            @for (columnId of cellSelection.columnIds; track columnId) {
              <th>{{ columnId }}</th>
            }
          </tr>
        </thead>
        <tbody>
          @for (row of rows(); track $index) {
            <tr>
              @for (value of row; track $index) {
                <td>{{ value }}</td>
              }
            </tr>
          }
        </tbody>
      </table>
    } @else {
      <div>No cell selection</div>
    }
  `,
})
class SelectedCellsTable {
  protected readonly cellSelection = injectCellSelection();

  // one entry per selected row, with the values in columnIds order
  protected readonly rows = computed(() =>
    (this.cellSelection()?.positions ?? []).map((row) =>
      row.map((cell) => (typeof cell === 'object' ? cell.value : null)),
    ),
  );
}

@Component({
  selector: 'app-root',
  imports: [AdaptableProvider, AdaptableUI, SelectedCellsTable],
  template: `
    <adaptable-provider adaptableId="app" primaryKey="id" [data]="data" [defaultState]="defaultState">
      <selected-cells-table />
      <adaptable-ui />
    </adaptable-provider>
  `,
})
export class App {
  protected readonly data = data;
  protected readonly defaultState = defaultState;
}
Note

The raw selection (row id and column id pairs) is also part of the view state, under selection.value — see injectCurrentViewState. This inject function resolves it to values for you.

#
Summarizing the selected cells

The panel above the grid reads injectCellSelection() and shows how many cells are selected, in which columns, and the sum of the numeric ones. The view starts with a few cells selected.

Click and shift-click (or cmd/ctrl-click) cells to change the selection.

open
loading demo…