Skip to content

Grid Columns

Grid columns in Serenity are defined declaratively in a columns class — a plain class whose properties describe the columns of a grid. The class is turned into a columns script (a dynamic script) that the client grid loads to build its columns.

The Columns Class

A columns class is a plain class with public properties. Each property becomes a column:

[ColumnsScript]
public class OrderColumns
{
    [Width(150), EditLink]
    public string? CustomerID { get; set; }

    [Width(200)]
    public string? ShipCity { get; set; }

    [Width(120), AlignRight]
    public decimal? Freight { get; set; }
}

The [ColumnsScript] attribute marks the class so that a columns script is generated and registered. See Script Generation for the attribute itself.

[ColumnsScript] in Detail

Auto Key Determination

When you use [ColumnsScript] without a key, the script key is the full name of the type:

[ColumnsScript]
public class OrderColumns { }
// key = "MyProject.Northwind.OrderColumns"

You can also specify an explicit key:

[ColumnsScript("Northwind.Order")]
public class OrderColumns { }

Other options of the attribute:

  • Permission — the permission required to load the columns script. When it is not set, the PropertyItemsScripts settings decide (public by default, or the ReadPermission of the row when PublicByDefault is false), see Dynamic Scripts.
  • Revision — a number that is sent to the client with the columns. When the current revision is positive and differs from the one saved with the user's personal grid settings, a restore resets the saved column order, visibility, widths and pinning to their initial values while keeping the saved sorting and filters. 0 disables the check, see Persisting Settings.

The generated dynamic script is registered under the key Columns.<key> (the ColumnsScript dynamic script type prefixes the name with Columns.). So for the example above the script name is Columns.Northwind.Order.

What It Generates

A columns script is a dynamic script (specifically a PropertyItemsScript) that produces a PropertyItemsData object — a JSON structure containing the PropertyItem[] array for the columns, plus any AdditionalItems. The PropertyItem objects are built by the IPropertyItemProvider from the attributes on the columns class (and its BasedOnRow row, if any).

The script is served through the dynamic script middleware, and can be retrieved as JSON (via ~/DynamicData) or as a script (via ~/DynJS.axd).

Accessing Columns from the Client

On the client side you load the columns data with the async helpers:

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

const data = await getColumnsDataAsync("Northwind.Order");
// data.items is the PropertyItem[] for the columns

The older synchronous getColumns (and getColumnsData) methods are deprecated in favor of the async versions, which don't block the UI thread. Grids load their columns automatically through getColumnsKey() / getPropertyItemsData(), so you normally don't call these directly.

[BasedOnRow]

A columns class often maps to a row. The BasedOnRowAttribute tells Serenity that the columns class is based on a row type, so each column property inherits the attributes from the matching row field (display name, formatter, filtering, etc.):

[ColumnsScript("Northwind.Order")]
[BasedOnRow(typeof(OrderRow))]
public class OrderColumns
{
    [Width(150)]
    public string? CustomerID { get; set; }
    // ...
}
  • Properties whose names match row fields inherit the row field's attributes (e.g. [DisplayText], [FormatterType], [Filtering]).
  • CheckNames = true validates that every property matches a row field (add [SkipNameCheck] to properties that shouldn't be checked).
  • When BasedOnRow is set, the provider also generates AdditionalItems for any FilteringIdField referenced by the columns but not explicitly declared.

Column Attributes

These attributes apply to columns-class properties and set the corresponding PropertyItem properties that the client grid reads.

Layout & Width

Attribute PropertyItem property Description
WidthAttribute width Column width in pixels
FixedWidthAttribute width (fixed) Fixed width that can't be resized
AlignCenterAttribute / AlignRight alignment Text alignment in the column

Behavior

Attribute PropertyItem property Description
SortableAttribute sortable Whether the column can be sorted
EditLinkAttribute editLink Renders the value as a link that opens the edit dialog. Its Quiet option (editLinkQuiet) makes the link look like plain text until the row is hovered or the link has the focus, and overrides the grid's editLinks option for that column
RowSelectionColumnAttribute rowSelection Turns the property into the check box column of GridRowSelectionMixin; the property must be named RowSelection, and the grid creates the mixin itself
RowActionsColumnAttribute rowActions Turns the property into the row actions column of StartSharp's RowActionsMixin (the property must be named RowActions); the client turns it into the __actions__ utility column, which the column picker cannot hide and the exports leave out
RecordViewTypeAttribute recordViews Adds a client side view of the record of the property, e.g. a card that opens when its value is hovered. Placement (Trigger or Card), IdField, Default, Below, HideSwitch and SwitchPlacement control where and how it is shown
ShowSelectionAttribute showSelection Shows a selection checkbox in the column
PinToStartAttribute / PinToEnd pinnedPosition Pins the column to the start/end of the grid
FocusableAttribute / TabbableAttribute tabIndex Keyboard focus/tab behavior

Formatting

Attribute PropertyItem property Description
FormatterTypeAttribute formatterType The formatter used to render the cell (e.g. Date, Number, Enum)
FormatterOptionAttribute formatterParams Options passed to the formatter
DisplayFormatAttribute format Display format for date/number formatters
SummaryTypeAttribute summaryType Aggregation shown in the grid footer (e.g. Sum, Avg); its members now have localized texts, and AggregateType (server-side aggregates) uses the same numbers

Filtering

Attribute PropertyItem property Description
FilteringTypeAttribute filteringType The filtering type for the column's quick filter
FilteringOptionAttribute filteringParams Options for the filtering type
QuickFilterAttribute quickFilter Shows a quick filter for the column. Pinned keeps it visible when the filter bar collapses behind its Filters button, and Range = false makes the quick filter of a date column edit a single date instead of a range
NotFilterableAttribute filtering Disables filtering for the column

Record Views

A record view is a client side component that shows something about the record of a property, e.g. a card with the details of the customer of an order, when its value is hovered, focused or long pressed (RecordViewPlacement.Trigger). A view that is about the records of the whole grid, e.g. a card view the grid shows instead of its table, goes on the columns class itself (RecordViewPlacement.Card).

The view classes generate their own attributes with their options, e.g. [CustomerCardView(Below = "md", Default = true)], so a property may have more than one view. The id field of the view is the one the join of the property tells, as it is for an edit link, unless the attribute gives it. The views travel with the property items (PropertyItem.RecordViews) and with the columns scripts (PropertyItemsData.RecordViews, also in the compact scripts), and a grid shows the switches that choose between them unless HideSwitch is set.

The PropertyItem type is shared between columns and forms — see the PropertyItem API reference for the full list of properties.

See Also