#useRowSelection
useRowSelection() gives you the selected rows of the current view and
re-renders your component when the selection changes.
Row selection needs the view's selection.mode to be 'single-row' or
'multi-row' — see Selection. For
selected cells, use useCellSelection.
#Return value
The hook returns null when the current view has no row selection mode.
Otherwise it returns 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: useRowSelection<Developer>().
import { Adaptable, useRowSelection } from '@adaptabletools/datagrid-react';
function SelectedRows() {
const selectedRows = useRowSelection<Developer>();
if (!selectedRows?.length) {
return <div>No rows selected</div>;
}
return <Chart data={selectedRows.map((rowInfo) => rowInfo.data)} />;
}
function App() {
return (
<Adaptable.Provider {...props}>
<SelectedRows />
<Adaptable.UI />
</Adaptable.Provider>
);
}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
useCurrentViewState. This hook resolves
it to row objects for you.
The view uses multi-row selection with a checkbox column. The panel above the
grid reads useRowSelection() and shows the names and the total salary of the
selected developers.
Tick and untick rows to see it update.