Skip to content

Creating and Configuring Grids

This page shows how to create grids in a Serene/StartSharp application and how to configure them. There are two kinds of grids:

  • EntityGrid — the common case, bound to a row/entity with CRUD dialogs, routing, and service integration.
  • DataGrid — the base grid, used directly for grids that don't follow the standard entity CRUD pattern.

Creating an EntityGrid

A typical generated grid extends EntityGrid and overrides a few methods to wire up its columns, dialog, row, and service:

import { EntityGrid } from "@serenity-is/corelib";
import { RoleColumns, RoleRow, RoleService } from "../../ServerTypes/Administration";
import { RoleDialog } from "./RoleDialog";

export class RoleGrid extends EntityGrid<RoleRow> {
    protected override getColumnsKey() { return RoleColumns.columnsKey; }
    protected override getDialogType() { return RoleDialog; }
    protected override getRowDefinition() { return RoleRow; }
    protected override getService() { return RoleService.baseUrl; }
}

The key methods:

Method Purpose
getColumnsKey() The columns script key (e.g. RoleColumns.columnsKey).
getDialogType() The dialog class opened for add/edit.
getRowDefinition() The row class (provides id property, local text prefix, etc.).
getService() The service base URL (e.g. RoleService.baseUrl).
getIdProperty() The id field name (defaults to the row's id property).
getIsActiveProperty() The is-active field name (for soft-delete grids).
getLocalTextPrefix() The local text prefix for the grid.

Registering the Grid

Grids are registered with registerClass so they can be looked up by key:

export class RoleGrid extends EntityGrid<RoleRow> {
    static override[Symbol.typeInfo] = this.registerClass(nsAdministration);
    // ...
}

See Type Registration for details.

Creating a Standalone DataGrid

When a grid doesn't follow the standard entity CRUD pattern, extend DataGrid directly. The PermissionCheckEditor in the template is a good example — it's a tree grid of permission keys that loads data from a remote data script rather than a list service:

export class PermissionCheckEditor<P extends PermissionCheckEditorOptions = PermissionCheckEditorOptions>
    extends DataGrid<PermissionCheckItem, P> {

    static override[Symbol.typeInfo] = this.registerEditor(nsAdministration, [IGetEditValue, ISetEditValue]);

    protected override getIdProperty() { return "Key"; }

    protected override getGridCanLoad() {
        return false; // don't load from a list service
    }

    protected override getButtons(): ToolButton[] {
        return [];
    }
}

Because it doesn't load from a service, it overrides getGridCanLoad() to return false and populates the view manually with this.view.setItems(...).

Configuring Columns

Columns come from the columns script (a PropertyItem[]), converted to SleekGrid columns by createColumns(). You can customize columns by overriding createColumns():

protected override createColumns() {
    const columns = new UserColumns(super.createColumns());

    columns.Email && (columns.Email.format = ctx => ctx.value ?
        <a href={`mailto:${ctx.value}`}>{ctx.value}</a> : "");

    return columns.valueOf();
}

This example wraps a column of a user grid in a custom formatter using JSX.

Configuring the Toolbar

Override getButtons() to add toolbar buttons:

protected override getButtons(): ToolButton[] {
    return [
        {
            title: "My Action",
            cssClass: "my-action-button",
            onClick: () => { /* ... */ }
        }
    ];
}

Configuring Quick Filters

Quick filters are derived from columns marked with quickFilter: true in the columns script. Override getQuickFilters() to customize them, or createQuickFilters() to build them manually.

Configuring Sorting and Paging

  • getDefaultSortBy() — the default sort order (e.g. ["Name"] or ["Name desc"]).
  • usePager() — return true to show a pager (EntityGrid does this by default).
  • getViewOptions() — configures the RemoteView (id field, sort, rows per page). rowsPerPage is the page size, or the number of rows a request asks for at once for a view that loads its rows as they are scrolled, so it is not reduced on a narrow screen for such a view.
  • On a screen narrower than 768 pixels, the page of a responsive-height grid shrinks to 20 rows, as the grid is as high as its rows there and the page scrolls. It is done in DataGrid.createView, so it applies only to the default RemoteView and not to a view of your own.

Configuring Grid Options

Override getSlickOptions() to customize the underlying SleekGrid options:

protected override getSlickOptions() {
    const opt = super.getSlickOptions();
    opt.rowHeight = 40;
    opt.forceFitColumns = true;
    return opt;
}

Some options are set globally (in a script init file) or in getSlickOptions():

  • autoHeight in getSlickOptions() — makes a grid as tall as its rows instead of scrolling them. A grid with the responsive-height class also gets it on a narrow screen; the layout only turns that off again, so an autoHeight the grid asks for in its options is kept.
  • useNarrowAutoHeight() — override it to decide which grids are as high as their rows on a narrow screen, instead of the responsive-height class (the default). A view that can't be auto-heighted, e.g. one that keeps a row for each row of the server and loads the rows in the viewport, says so with supportsAutoHeight = false, as all its rows would be in view and loaded. Such a grid gets the s-narrow-fixed-height class instead, which gives it a height of its own (--s-narrow-grid-offset less than the screen), so it scrolls.
  • DataGrid.defaultOptions.editLinks — "all" (the default) makes each column with an [EditLink] a link to its record, while "primary" makes only the first visible one a link and shows the others as quiet links, which look like the text of their cell until the row is hovered or the link has the focus. A column's EditLink attribute can override it with Quiet, and getPrimaryEditLinkColumn(columns) picks the primary one in code.
  • enableCellNavigation: "row" in getSlickOptions() — turns on the row cursor, for a list whose rows open a dialog, see Selection and Row Operations.
  • DataGrid.defaultOptions.indentDataRows / displayTotalsRow — indent the data rows of a grouped grid under their group rows, and choose whether the group totals are on a row of their own, on the group row, or not shown, see DataGrid Architecture.
DataGrid.defaultOptions.editLinks = "primary";

Refreshing the Grid

  • this.view.populate() — reloads data from the server.
  • this.refresh() — refreshes the grid.
  • this.view.setItems(items, true) — replaces the items locally (used by standalone grids).

See Also