#injectRowSelection
injectRowSelection() (the Angular counterpart of the useRowSelection
hook) gives you a signal of the selected rows of the current view. The signal
gets a new value when the selection changes. Call it in an injection context
(a field initializer or the constructor) of a component rendered inside
<adaptable-provider>.
Row selection needs the view's selection.mode to be 'single-row' or
'multi-row' — see Selection. For
selected cells, use injectCellSelection.
#Return value
The inject function returns a read-only Signal. Its value is null when the
current view has no row selection mode. Otherwise it is an array — empty when
nothing is selected — with one
row info
object per selected row currently in the grid. Read the row data from
rowInfo.data and the primary key from rowInfo.id. With grouping, selected
group rows are included too (rowInfo.isGroupRow is true for them).
Pass your row type to type the data: injectRowSelection<Developer>().
import { Component } from '@angular/core';
import {
AdaptableProvider,
AdaptableUI,
injectRowSelection,
} from '@adaptabletools/datagrid-angular';
@Component({
selector: 'selected-rows-list',
template: `
@if (selectedRows()?.length) {
<ul>
@for (rowInfo of selectedRows(); track rowInfo.id) {
<li>{{ rowInfo.data?.firstName }}</li>
}
</ul>
} @else {
<div>No rows selected</div>
}
`,
})
class SelectedRowsList {
protected readonly selectedRows = injectRowSelection<Developer>();
}
@Component({
selector: 'app-root',
imports: [AdaptableProvider, AdaptableUI, SelectedRowsList],
template: `
<adaptable-provider adaptableId="app" primaryKey="id" [data]="data" [defaultState]="defaultState">
<selected-rows-list />
<adaptable-ui />
</adaptable-provider>
`,
})
export class App {
protected readonly data = data;
protected readonly defaultState = defaultState;
}Row selection is configured per view:
{
id: 'all',
label: 'All developers',
selection: {
mode: 'multi-row',
showCheckboxColumn: true,
// optional initial selection
value: { defaultSelection: false, selectedRows: [3, 4] },
},
}The raw selection (the selected primary keys) is also part of the view state,
under selection.value — see
injectCurrentViewState. This inject
function resolves it to row objects for you.
The view uses multi-row selection with a checkbox column. The panel above the
grid reads injectRowSelection() and shows the names and the total salary of
the selected developers.
Tick and untick rows to see it update.