#useCellSelection
useCellSelection() is a composable that gives you a ref of the selected cells
of the current view — their row ids, column ids, values and data types. The ref
gets a new value when the selection changes. Use it to build summaries, charts
or actions over the user's selection. Call it in <script setup> (or in
setup()) of a component rendered inside <Adaptable.Provider>.
Cell selection needs the view's selection.mode to be 'multi-cell' (the
default) — see Multi-cell selection. For
selected rows, use useRowSelection.
#Return value
The composable returns a read-only shallow ref (Readonly<ShallowRef<T>>). 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 withcolumnIds. 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
columnIdsthat is not selected in that row is an empty string.
{
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
Read cellSelection.value in the script; the template unwraps the ref.
<script setup lang="ts">
import { computed } from 'vue';
import { useCellSelection } from '@adaptabletools/datagrid-vue';
const cellSelection = useCellSelection();
// one entry per selected row, with the values in columnIds order
const rows = computed(() =>
(cellSelection.value?.positions ?? []).map((row) =>
row.map((cell) => (typeof cell === 'object' ? cell.value : null)),
),
);
</script>
<template>
<div v-if="!cellSelection">No cell selection</div>
<table v-else>
<thead>
<tr>
<th v-for="columnId in cellSelection.columnIds" :key="columnId">{{ columnId }}</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, rowIndex) in rows" :key="rowIndex">
<td v-for="(value, index) in row" :key="index">{{ value }}</td>
</tr>
</tbody>
</table>
</template><Adaptable.Provider v-bind="props">
<SelectedCellsTable />
<Adaptable.UI />
</Adaptable.Provider>The raw selection (row id and column id pairs) is also part of the view state,
under selection.value — see
useCurrentViewState. This composable
resolves it to values for you.
The panel above the grid reads useCellSelection() 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.