#useRowSelection
useRowSelection() is a composable that gives you a ref of the selected rows
of the current view. The ref gets a new value when the selection changes. Call
it in <script setup> (or in setup()) 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 useCellSelection.
#Return value
The composable returns a read-only shallow ref (Readonly<ShallowRef<T>>). 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: useRowSelection<Developer>().
<script setup lang="ts">
import { useRowSelection } from '@adaptabletools/datagrid-vue';
import type { Developer } from '../../getting-started/basic.data';
const selectedRows = useRowSelection<Developer>();
</script>
<template>
<div v-if="!selectedRows?.length">No rows selected</div>
<ul v-else>
<li v-for="rowInfo in selectedRows" :key="rowInfo.id">{{ rowInfo.data?.firstName }}</li>
</ul>
</template><Adaptable.Provider v-bind="props">
<SelectedRowsList />
<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 composable
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.