Adaptable

#Selection

The Adaptable DataGrid supports multiple selection modes: multi cell, single and multi row or no selection at all. Each view can be configured with any of those selection modes and users can change it from the settings panel.

For using the keyboard to navigate, see Keyboard navigation.

#Selection modes

The selection configuration of a view lives under selection, and selection.mode takes one of four values:

Besides configuring the selection mode, you can also choose to optionally configure the selection.value, so you can start with an initial selection already applied. Its shape depends on the mode, more on that below. When selection.mode is omitted, it is inferred from selection.value; without a selection.value, the view uses viewDefaults.selectionMode, and finally 'multi-cell':

Defaults for all views
{
  currentViewId: 'all',
  viewDefaults: {
    selectionMode: 'single-row',
  },
  views: [
    { id: 'all', label: 'All developers' },
    // a view's own setting wins over viewDefaults
    { id: 'cells', label: 'Cells', selection: { mode: 'multi-cell' } },
  ],
};

As users select rows or cells, the grid writes the selection back into selection.value. To read it as row objects or cell values, use the useRowSelection and useCellSelection hooks.

#
The four selection modes

Each view uses a different selection mode — pick one from the view picker. The line above the grid shows the current mode and what is selected, read with useViewSelection, useRowSelection and useCellSelection.

open
loading demo…

#Multi-cell selection

'multi-cell' selects ranges of cells. Click a cell to select it, then shift-click another cell or drag across cells to select the range between them. Cmd/ctrl-click adds or removes a cell.

Cell selection
{
  id: 'cells',
  label: 'Cell range',
  selection: {
    mode: 'multi-cell',
    // optional: [primary key, column id] pairs
    value: {
      defaultSelection: false,
      selectedCells: [
        [1, 'firstName'],
        [2, 'salary'],
      ],
    },
  },
};

A selected cell is a row id and a column id.

#Cells on a flat table

The row id is the primary key, and the column id is a column of the view, as in the snippet above.

#Cells on a grouped table

The same gestures select cells of data rows and of group rows. A data row's cell uses that row's primary key. A group row's cell belongs to the group row.

#Cells on a pivot view

A pivot view has no data rows, so every cell belongs to a group row: the group columns and the aggregation columns. The gestures are the same as on a table.

#Single-row selection

'single-row' selects one data row. Clicking a row selects it and clears the previous selection. The value is the primary key of that row, or null when nothing is selected.

Single row selection
{
  id: 'single',
  label: 'Single row',
  selection: {
    mode: 'single-row',
    // optional: the primary key of the selected row, or null
    value: 3,
  },
};

The spacebar selects the active data row, and selects it again to clear it.

#A single row on a flat table

Every row is a data row, so clicking any row selects it.

#A single row on a grouped table

Only data rows can be selected. Clicking a group row, or pressing the spacebar on it, leaves the selection unchanged. Clicking the expand/collapse icon of a group row leaves it unchanged too.

#A single row on a pivot view

A pivot view shows only group rows. Single-row selection selects data rows, so clicking a row in a pivot view leaves the selection unchanged.

#Multi-row selection

'multi-row' selects several data rows. Two settings decide how:

  • multiSelectWith — whether a click selects a row, or only a checkbox does.
  • multiSelectTarget — when some rows are filtered out, whether select-all and the group checkboxes include those rows.
Multi-row selection
{
  id: 'multi',
  label: 'Multiple rows',
  selection: {
    mode: 'multi-row',
    // optional: the selected primary keys
    value: { defaultSelection: false, selectedRows: [3, 4] },
  },
};

The stored value lists primary keys. Selecting a group row selects the data rows in that group, and the value lists those primary keys. With server-side grouping the value lists group keys instead.

#How users select rows

selection.multiSelectWith is 'checkbox' by default. It applies to flat tables, grouped tables and pivot views.

When no visible column shows a selection checkbox, users select rows by clicking them:

  • clicking a row selects it and clears the rest of the selection;
  • cmd/ctrl-clicking a row adds it to the selection, or removes it if it was already selected;
  • shift-clicking a row selects the range from the last row clicked without shift.

While a visible column shows a selection checkbox — the checkbox column, or a column or group column with checkboxSelection — clicking a row only makes it the active row and leaves the selection as it is. Instead:

  • clicking a checkbox selects or deselects its row, keeping the rest of the selection;
  • shift-clicking a checkbox selects the range from the last checkbox or row used.

A hidden column doesn't count: when the checkbox column is hidden, users select rows by clicking them again.

To have clicks select rows even while a checkbox is shown, set selection.multiSelectWith to 'click'. The checkboxes then work as well:

Clicking rows selects them, with a checkbox column too
{
  id: 'click-and-checkbox',
  label: 'Click or checkbox',
  selection: {
    mode: 'multi-row',
    showCheckboxColumn: true,
    // clicking a row selects it, even with the checkbox column shown
    multiSelectWith: 'click',
  },
};

The spacebar works in both cases, on the active row. Without a checkbox (or with multiSelectWith: 'click'), it works like a click: on its own it selects the active row only, with cmd/ctrl it adds or removes the row, and with shift it selects a range. While a checkbox is shown, it works like clicking the checkbox: it selects or deselects the active row, and with shift it selects a range. Clicks, checkbox clicks and the spacebar share the same starting row for shift ranges.

#Selection checkboxes

Checkboxes are only shown with 'multi-row' selection. There are several places to put them:

  • A dedicated column — on a table view, showCheckboxColumn: true adds a narrow column with the id 'checkbox-column'. Configure and place it like any other column, with columnOverrides['checkbox-column'] and columnOrder (see The checkbox column). The checkbox in the header selects all rows. Pivot views have no dedicated checkbox column.

    A dedicated checkbox column
    {
      id: 'checkbox-column',
      label: 'Checkbox column',
      selection: { mode: 'multi-row', showCheckboxColumn: true },
      columnOverrides: {
        // the dedicated column is configured like any other column
        'checkbox-column': { pinned: true },
      },
    };
  • One of your columns — on a table view, set checkboxSelection: true on the column, in its definition or in the view's columnOverrides. The header checkbox selects all rows.

    A checkbox in the First name column
    {
      id: 'name-checkbox',
      label: 'Checkbox in First name',
      selection: { mode: 'multi-row' },
      columnOverrides: {
        firstName: { checkboxSelection: true },
      },
    };
  • A group column — set checkboxSelection: true on the group column. The checkbox on a group row selects all the data rows in the group, and the header checkbox selects all rows.

    A checkbox in a group column
    {
      id: 'by-country',
      label: 'By country',
      selection: { mode: 'multi-row' },
      groupColumns: {
        'country-group': { groupBy: 'country', checkboxSelection: true },
      },
    };
  • A pivot group column — a pivot view's checkbox goes on one of the pivotGroupColumns. The checkbox on a group row selects the data rows in that group.

    A checkbox in a pivot group column
    {
      id: 'pivot',
      label: 'Salary by language',
      selection: { mode: 'multi-row' },
      pivotColumns: [{ columnId: 'preferredLanguage' }],
      pivotAggregationColumns: [{ columnId: 'salary', aggregation: 'sum' }],
      pivotGroupColumns: [
        { id: 'country-group', groupBy: 'country', checkboxSelection: true },
      ],
    };
#
Where the checkbox goes

One view per checkbox placement: a dedicated, pinned checkbox column; a checkbox in the First name column; a checkbox in the Country group column; and a checkbox in the group column of a pivot view.

open
loading demo…

#Several rows on a flat table

Every row is a data row, so a click, a checkbox or the spacebar selects that row. There are no group rows. The header checkbox and api.viewApi.rowSelectionApi.selectAll() / deselectAll() follow multiSelectTarget.

#Several rows on a grouped table

A group row is selected as a whole: clicking it, its checkbox or pressing the spacebar on it selects all the data rows in the group, and in its nested groups. Clicking the expand/collapse icon of a group row leaves the selection unchanged.

A group row with only some of its data rows selected is painted with a lighter selection background. That includes a group whose other rows are hidden by a filter when multiSelectTarget is 'all'. With a selection checkbox, this is the same partial state the checkbox shows as indeterminate.

Shift-click selects a range across groups.

#Several rows on a pivot view

A pivot view shows only group rows. Clicking a group row selects every data row in that group and in its nested groups, and the selection stores those primary keys. Cmd/ctrl-click toggles the group. Shift-click selects a range of group rows with the same rules as a grouped table: a collapsed group selects all of its data rows, and an expanded group follows its child groups.

The checkbox for a pivot view sits on a pivot group column. While that column is visible, clicking a group row leaves the selection unchanged, unless multiSelectWith is 'click'.

A group with only some of its data rows selected is painted with the lighter selection background, and its checkbox is indeterminate.

Pivot views have no column filters and no grid filter, so multiSelectTarget 'all' and 'visible' select the same rows.

#Which rows select all covers

When some rows are filtered out, selection.multiSelectTarget sets which rows the header checkbox, the group checkboxes and api.viewApi.rowSelectionApi.selectAll() / deselectAll() act on:

  • 'all' (the default) — every row, including the filtered-out ones. On a flat table the header checkbox is checked only when every row is selected, so it shows as indeterminate when just the visible rows are selected. On a grouped table the same goes for a group checkbox and the data rows in its group.
  • 'visible' — only the rows that pass the filters. Filtered-out rows keep their selection: selecting all leaves them unselected, and deselecting all leaves them selected. The header and group checkboxes reflect the visible rows only.
Select all acts on the visible rows only
{
  id: 'visible-only',
  label: 'Select visible rows',
  selection: {
    mode: 'multi-row',
    showCheckboxColumn: true,
    // select all and the group checkboxes skip the filtered-out rows
    multiSelectTarget: 'visible',
  },
  filters: { columns: [`[country] = 'USA'`] },
};

On a flat table this is the header checkbox and selectAll() / deselectAll(). On a grouped table it is those, plus each group checkbox. On a pivot view there are no filtered-out rows, so both values select the same data rows.

Without filters, both values behave the same.

With server data (mode: 'server'), the server applies the filters, so for 'all' the checkboxes need the row counts without the filters. A data source from createTdgpDataSource asks the server for them on filtered requests while multiSelectTarget is 'all', and changing multiSelectTarget to 'all' reloads a filtered view. These counts cost the server extra queries: set unfilteredCounts: false on createTdgpDataSource to never ask for them. The checkboxes then count only the rows that pass the filters, while the selection still covers the filtered-out rows.

#Ranges in grouped views

A shift range covers the rows as they are displayed, from the starting row to the clicked row, in either direction. The same rules apply to a pivot view, where every displayed row is a group row:

  • data rows are selected, and a collapsed group row selects its whole group;
  • an expanded group row follows its children: only its rows inside the range are selected, so it shows as selected when all its rows are in the range, and as partly selected when only some are. This is also the case when you shift-click an expanded group row — its rows are below it, so going down to it selects none of them;
  • the starting row keeps what its own click selected: when it is a group row, its whole group stays selected;
  • a new shift-click from the same starting row replaces the range of the previous one; rows outside the range keep their selection.

So a wider range never selects fewer rows.

For example, with the rows grouped by language and the Java group collapsed:

TypeScript
   Ines
   Maria     <- click
   Oliver
   Daniel
Java         (collapsed: John, Camila, Lara)
Go
   Liam
   Yuki      <- shift-click
   Mei

clicking Maria and then shift-clicking Yuki selects Maria, Oliver, Daniel, all of Java, Liam and Yuki, but not Mei. TypeScript and Go show as partly selected, and Java as selected. Shift-clicking the Go group row instead ends the range just above the rows of Go, so it selects Maria, Oliver, Daniel and all of Java, while Go is not selected. Clicking the Go group row first and then shift-clicking Daniel selects Daniel, all of Java and all of Go: the starting group stays selected whole.

The same rules apply to shift-clicking a checkbox and to shift + spacebar.

#
Shift ranges with groups

The rows are grouped by language, with Java collapsed. Click Maria, then shift-click Yuki, then shift-click the Go group row — the range now ends above the rows of Go. Switch to the Select with checkboxes view to do the same with the checkboxes of the group column.

open
loading demo…

#Selecting rows with server-side grouping

When the data is loaded from the server (mode: 'server') and the view groups its rows — a grouped table view, or a pivot view with group columns — most rows are not loaded yet. So in 'multi-row' selection, selection.value lists rows and groups by their group keys instead of by primary key:

  • a group is listed by its keys, e.g. ['France'] or ['France', 'Paris'], and covers all the data rows in the group, loaded or not;
  • a row is listed by the keys of its groups followed by its primary key, e.g. ['Italy', 'Rome', 45].
A selection with server-side grouping
{
  id: 'server-grouped',
  label: 'By country and city',
  selection: {
    mode: 'multi-row',
    value: {
      defaultSelection: false,
      selectedRows: [
        // every developer in France, loaded or not
        ['France'],
        // the developer with id 45, in Rome
        ['Italy', 'Rome', 45],
      ],
      // except the developer with id 12, in Paris
      deselectedRows: [['France', 'Paris', 12]],
    },
  },
  groupColumns: {
    'location-group': {
      groupBy: ['country', 'city'],
      checkboxSelection: true,
    },
  },
};

When the grouping changes, or the view switches between client and server data, the grid converts the selection to the new form. When the grouping is removed, rows listed by their full path are kept, by primary key. Groups, and rows listed by primary key, have no equivalent in the new form, so then the selection is cleared. A selection of all rows, with no exceptions, is always kept.

#Reading the selected rows

api.viewApi.rowSelectionApi.getSelectedRowInfoArray() returns the selected rows among the loaded ones. To get every selected row, including the filtered-out ones and, with server data, the ones not loaded yet, use getSelectedPrimaryKeys(), or getSelectedRowCount() for just their number. Both return a promise:

Every selected row
// every selected row, including the filtered-out ones and the ones not loaded
const primaryKeys =
  await api.viewApi.rowSelectionApi.getSelectedPrimaryKeys();
// only how many there are, without loading their primary keys
const count = await api.viewApi.rowSelectionApi.getSelectedRowCount();

With server data, a selected group or a selection of all rows can cover rows the grid hasn't loaded, so the data function works them out: a data source from createTdgpDataSource sends the server a query for the selected rows. A custom data function does this with a resolveSelection function attached to it; without one, the promise rejects unless only individual rows are selected. With multiSelectTarget: 'visible', a selected group or a selection of all rows only covers the rows that pass the filters, while rows selected one by one stay selected.

On a pivot view these calls return the underlying data rows, the same primary keys a group-row click stores.

#No selection

selection.mode: false means rows and cells cannot be selected. Clicks still move the active cell or row when keyboard navigation is on.

No selection
{
  id: 'read-only',
  label: 'No selection',
  selection: {
    mode: false,
  },
};

#Persisting the selection

The selection is persisted with the rest of the state by default, so users find the same rows or cells selected after a reload. Set selection.persist: false to leave the whole selection object — mode, value, showCheckboxColumn, multiSelectWith and multiSelectTarget — out of the persisted state; after a reload, the view starts from the selection in your initial state:

A selection that is not persisted
{
  id: 'multi',
  label: 'Multiple rows',
  selection: {
    mode: 'multi-row',
    // keep the selection out of the persisted state
    persist: false,
  },
};

keyboardNavigation is not part of selection, so it is persisted either way.

#Changing the selection in the UI

The settings panel has a Selection & navigation page (id view-selection) for the current view, in both table and pivot views. For the selection it sets:

  • the selection mode — one of the four modes;
  • the column with selection checkbox — with 'multi-row' selection, the dedicated checkbox column or any column of a table view (group columns included), or a group column of a pivot view;
  • select all applies to — with 'multi-row' selection, All rows or Visible rows (multiSelectTarget).

The same page sets the keyboard navigation.

The page is also available on its own as Adaptable.UI.ViewSelectionEditor. To replace it everywhere, pass your own through the Provider's components prop (components.ViewSelectionEditor).

#
The selection editor next to the grid

Adaptable.UI.ViewSelectionEditor edits the current view. Switch to Multiple rows to pick the column that shows the checkbox, and switch to the pivot view to pick one of its group columns.

open
loading demo…

For a custom UI, useViewSelection() exposes the same operations:

useViewSelection
import { useViewSelection } from '@adaptabletools/datagrid-react';

function SelectionToolbar() {
  const {
    mode, // the effective selection mode of the current view
    setMode, // (mode) => void
    keyboardNavigation, // the effective keyboard navigation
    keyboardNavigationAutomatic, // true when it follows the selection mode
    setKeyboardNavigation, // ('cell' | 'row' | false | 'auto') => void
    selectionColumnOptions, // columns that can show the checkbox (multi-row only)
    selectionColumnIds, // the columns that show it now
    setSelectionColumn, // (columnId | null) => void
    multiSelectTarget, // 'all' | 'visible': the rows select all acts on
    setMultiSelectTarget, // ('all' | 'visible') => void
  } = useViewSelection();

  // ...
}