Skip to content

Selection and Row Operations

Serenity grids support row selection through SleekGrid selection models and through corelib selection mixins that add checkbox/radio columns. This page covers both, plus common row operations.

SleekGrid Selection Models

SleekGrid uses a SelectionModel (a GridPlugin) to manage selection. The built-in RowSelectionModel treats selection as whole rows.

RowSelectionModel

RowSelectionModel provides:

  • Row selection by click — a click selects the row that is clicked, even in a grid without cell navigation, where there is no active cell to follow.
  • Active-cell-driven selection — moving the active cell selects the new row (selectActiveRow, default true).
  • Ctrl/Meta/Shift-click — toggles individual rows, and shift extends the selection.
  • Shift+Up/Down — extends the selection range from the last row that was selected, as a grid without cell navigation has no active cell to take that place. Its ranges start out empty, so getSelectedRows() works before anything is selected.
import { RowSelectionModel } from "@serenity-is/sleekgrid";

grid.setSelectionModel(new RowSelectionModel());

Grid Selection Methods

The grid exposes selection methods:

Method Purpose
setSelectedRows(rows) Selects the given row indices.
getSelectedRows() Returns the selected row indices.
setSelectionModel(model) Sets the selection model.
getSelectionModel() Returns the current selection model.
onSelectedRowsChanged Event fired when the selection changes.

Corelib Selection Mixins

For grids that need a checkbox or radio column, corelib provides two mixins.

GridRowSelectionMixin (multi-select)

GridRowSelectionMixin adds multi (checkbox) row selection with a select-all header checkbox:

import { GridRowSelectionMixin } from "@serenity-is/corelib";

declare private rowSelection: GridRowSelectionMixin;

protected override onViewSubmit() {
    // ...
}

protected override afterInit() {
    super.afterInit();

    this.rowSelection = new GridRowSelectionMixin(this);
    this.rowSelection.setSelectedKeys(this.getView().getItems().map(x => x.ID.toString()));
}

Create the select column with GridRowSelectionMixin.createSelectColumn:

protected override createColumns() {
    const columns = super.createColumns();
    columns.splice(0, 0, GridRowSelectionMixin.createSelectColumn(() => this.rowSelection));
    return columns;
}

Key methods:

Method Purpose
getSelectedKeys() The keys of the selected items.
getSelectedAsInt32() / getSelectedAsInt64() Selected keys as numbers.
setSelectedKeys(keys) Replaces the selection with the given keys.
selectKeys(keys) Adds keys to the selection.
getSelectionState() The keys, their count, how many are on the shown page (onPage) and how many rows of the page can be checked (selectableOnPage), so a bar can tell the selection of the page from the rest.
onSelectionChanged A PubSub<GridRowSelectionState> notified after every change, e.g. to enable the buttons of a bar that acts on the checked rows. A mixin that replaces another one for the same grid keeps its subscribers.
clear() Clears the selection (and repaints the rows).
resetCheckedAndRefresh() Clears the selection and reloads the grid.
updateSelectAll() Updates the state of the select-all header checkbox.
createSelectColumn(getMixin?, columnOptions?) Creates the checkbox select column. When the getter is omitted, the mixin of the grid that is being rendered is found with tryGetInstance.
GridRowSelectionMixin.tryGetInstance(grid) Finds the mixin of a grid (or of its SleekGrid) through a WeakMap, so a widget can act on the checked rows without holding a reference to the grid.
GridRowSelectionMixin.selectColumnId / isSelectColumn(column) The id of the column and a check that tells it apart from the others (by its formatter, so a columns class can declare it), e.g. for the column picker or an export.
destroy() Detaches the mixin from the grid.

The mixin also adds a select-all checkbox to the checkbox of each group row (selectGroups, on by default), which is checked, unchecked or partial as the rows of the group are, and is about the rows of the group that the view has — so a group whose rows are paged or loaded from the server as they are needed still gets one, for the rows that are there. Space on the checkbox column checks the active row or group like a click does, and with row navigation (enableCellNavigation: "row") Space or X checks it from any column.

A grid that selects its rows is usually given a bar below the grid that tells how many are checked and offers the actions that can be taken on them; that bar subscribes to onSelectionChanged.

Declaring the Column in a Columns Class

Instead of creating the mixin and the column in code, a columns class can declare the column with the RowSelectionColumn attribute on a property named RowSelection. The client turns it into the check box column, creates the mixin itself, and the page neither adds the column nor creates the mixin:

[ColumnsScript]
public class OrderColumns
{
    [RowSelectionColumn]
    public string? RowSelection { get; set; }
    // ...
}

The column keeps the __select__ id (with the [×] name in the column picker unless the property has a title of its own), so the grids that create it in code, and their saved column settings, are not affected.

GridRadioSelectionMixin (single-select)

GridRadioSelectionMixin adds single (radio) row selection:

import { GridRadioSelectionMixin } from "@serenity-is/corelib";

declare private radioSelection: GridRadioSelectionMixin;

protected override afterInit() {
    super.afterInit();
    this.radioSelection = new GridRadioSelectionMixin(this);
}

It provides clear(), resetCheckedAndRefresh(), the static formatRadioItem and tryGetInstance, and destroy(), which removes its click handler.

Breaking change: the column of the radio buttons has the __radio__ id instead of the __select__ id it shared with the check box column, and its header is (o) instead of [×], so the two can be told apart, e.g. in the column picker or an export. A saved column setting of the old id, and code that finds the column by it, needs the new one.

Both mixins accept a selectable option to control which items can be selected:

new GridRowSelectionMixin(this, {
    selectable: item => item.IsActive
});

Row Cursor

A grid whose rows open something, e.g. a dialog, can move a row cursor with the keyboard instead of a cell cursor: enableCellNavigation: "row". The row of the cursor is filled like a selection, without the ring of a cell cursor, and Up, Down, Page Up, Page Down, Home and End move it between the rows, while Left and Right scroll the columns and Tab leaves the grid. A click on a row puts the cursor there, and Down or Up starts it at the first or last row. The grid container gets the slick-row-navigation class.

In such a grid, EntityGrid opens the dialog of the row on Enter, like a click on its edit link, unless the grid is edited in place (which leaves Enter to its cell editors), and GridRowSelectionMixin checks the row on Space or X.

protected override getSlickOptions() {
    const opt = super.getSlickOptions();
    opt.enableCellNavigation = "row";
    return opt;
}

A grid row is no longer tinted just because one of its cells is the active one, so the cell that has the cursor is what is marked. A grid that wants the row of the cursor highlighted, e.g. one that is edited a row at a time, sets the highlightActiveRow option.

Row Operations

Common row operations on a grid:

// Get the item at a row index
const item = this.itemAt(row);

// Get the id of the item at a row index
const id = this.itemId(row);

// Get the row index for an item id
const row = this.rowById(id);

// Get the current items
const items = this.view.getItems();

// Reload from the server
this.view.populate();

// Refresh the grid
this.refresh();

See Also