Dojo NG is being built in the open, one section at a time — follow along on Heptapod.

API REFERENCE · DATA DISPLAY

<dj-data-grid>

A virtualized, sortable, selectable data grid built on TanStack Table and TanStack Virtual.

Give it columns, data, and a height. The core covers columns, in-memory data, sorting, virtual rows, row selection, keyboard row navigation, and calculated columns (GridColumn.compute). Everything else is a plugin. The grid has ARIA role grid.

Try it in the playground

@dojo-ng/data-grid · v0.5.0

npm install @dojo-ng/data-grid

import "@dojo-ng/data-grid";

Plugins

  • Filtering, pagination, inline editing, tree rows, grouping, CSV export, and master-detail are plugins. Pass an array of plugin objects to the plugins property, from JavaScript only.
  • A recommended order: one structural plugin first (treePlugin or groupsPlugin, never both), then editPlugin, cellComponentsPlugin, and formatsPlugin, then the plugins that only add controls (filterPlugin, paginationPlugin, exportPlugin, detailPlugin).
  • Changing plugins rebuilds the table, so set it once, early.

Opening rows: activation

activation decides what a plain click or Enter means on a row.

  • "none" (the default): click, Space, and Enter all toggle selection.
  • "click" (the mail and preview-pane idiom) or "double" (the file-manager idiom): a plain click, or a double click, opens the row and emits dj-activate with { row, index }, where row is the original row data. Selection does not change.
  • With activation on, Enter opens the row and Space selects it.
  • Modifier clicks always select and never open: Ctrl or Cmd-click toggles a row, and Shift-click selects a range when the checkbox column from @dojo-ng/data-grid-select is installed (without it, Shift-click only moves the active row).
  • "double" uses the browser's own dblclick, so the two clicks inside a double click never open the row on their own.
  • Activation works with any selection-mode, including "none", so a read-only list can have clickable rows.
  • To open rows by clicking while the user also builds a set for bulk actions, combine activation="click", selection-mode="multiple", and the checkbox column from @dojo-ng/data-grid-select.

Rendered rows: dj-range-change

  • dj-range-change fires when the window of rendered rows moves, so you can load data in and out, or load more at the end of the list.
  • The detail is { start, end, count, rendered }: the first and last rendered row index (inclusive), the total number of rows, and the list of rendered indexes.
  • The range includes the 8 extra rows the grid renders beyond each edge of the viewport. It is what the grid has rendered, not what the user can see, so fetching this range never leaves a gap.
  • To load more at the end: if (e.detail.end >= e.detail.count - 1) loadMore().
  • When nothing is rendered, start and end are -1 and count is the real count.
  • The event fires after rendering and only when (start, end, count) changes, so setting data in the handler is safe.

Focus and the active row

  • focus() on the host moves focus into the grid, so a consumer can return the keyboard to it (after closing a dialog, say) without reaching into the shadow root.
  • The grid shows a focus ring when it has keyboard focus, and the active row gets a stronger marker while the grid is focused.
  • The active row (the keyboard cursor) and each selected row carry extra parts, row--active and row--selected, alongside row, so a consumer can restyle them: dj-data-grid::part(row--selected) { color: inherit }. Plugin-contributed row parts (from @dojo-ng/data-grid-rowstate) are kept alongside them. Match rows with [part~="row"], not [part="row"], since a row can carry several parts.
  • dj-active-change fires when the user moves the active row with the keyboard or a click, with detail { index, row }, where row is the original row data. Setting activeIndex from code does not fire it, the same way setting a native input's value fires no change.

Keyboard

  • The grid is one tab stop. ArrowUp and ArrowDown move the active row, PageUp and PageDown move by a screen, Home and End go to the first and last row, Space selects, and Enter activates (see activation).
  • Sorting from the keyboard: ArrowUp on the first row moves to the header row. Left and Right move between headers (mirrored in right-to-left layouts), Home and End go to the first and last header, Enter or Space sorts the column, and ArrowDown returns to the first row.
  • A sortable header holds a <button part="sort-button">, which is not a tab stop of its own. aria-sort is ascending or descending on the sorted column and absent elsewhere. A column with sortable: false gets no button, so a plugin control in its header (the select-all checkbox) is never inside one.
  • Keys typed into a control inside the grid (a filter input, an edit field) go to that control, not the grid.

Columns: columnState

  • columnState is the user's column layout: { sizing, order, pinning, visibility }. sizing holds pixel widths by column id, order every column id in display order, pinning the ids pinned to the inline start and end, and visibility false for each hidden column.
  • Set it from saved settings to restore a layout. Ids the grid no longer has are dropped, and columns added since come back at their declared place. Setting it emits nothing.
  • dj-column-state-change fires with the whole state when the user changes the layout through a plugin, so storing it is one listener: store e.detail, set columnState on load.
  • Pinned columns stay put while the body scrolls sideways (position: sticky, so right-to-left layouts mirror them). A pinned column needs a fixed width; one with a flexible track is measured once and kept at that width. GridColumn.pinned sets the starting value.
  • minWidth and maxWidth on a column limit its width, and minWidth is also the floor of a flexible track. When the columns are wider than the grid, the body scrolls sideways and the header follows; Left and Right on a body row scroll it from the keyboard.
  • The UI to resize, reorder, pin, and hide columns is the columnsPlugin from @dojo-ng/data-grid-columns. A plugin column placed before every app column (the checkbox column) stays first, and joins the pinned start group when there is one.

Printing

  • When the page is printed, every row becomes part of a real <table> with a <thead>, and browsers repeat the header on each printed page. This does not apply to rows drawn by the detail plugin (@dojo-ng/data-grid-detail).
  • Safari does not repeat the table header on each printed page. This is a WebKit limitation with no reliable CSS fix.

Rows from a server: rowCount

Set rowCount to the total number of rows on the server to turn on remote mode. data is then ignored, the scrollbar is sized from rowCount, and the grid asks for the rows it needs.

The recipe:

  • Give the grid columns, rowCount, and a getRowId, so selection survives rows being dropped from the cache.
  • Listen for dj-rows-request, detail { start, end, epoch, reason }: an inclusive, block-aligned range of absolute indices. Fetch those rows, in the order grid.sorting names, and answer with grid.setRows(start, rows, epoch), passing back the request's epoch.
  • reason is "range" for rows coming on screen and "selection" for a Shift range waiting on rows. Answer both the same way.
  • Rows not loaded yet render as placeholder rows (part row--placeholder, aria-busy="true"). Plugin cell and row hooks are not called for them.

The epoch rule:

  • The grid starts a new epoch whenever the list changes under it: insertRows, removeRows, resetRows, and a sort. setRows with an older epoch keeps nothing and returns false, and the grid asks again for any rows still needed. So a host never has to cancel requests or reconcile late answers; it only passes the epoch back.

Loading and the cache:

  • Rows load in blocks of blockSize (default 100), so a block is never fetched twice in an epoch.
  • The grid keeps at most cacheSize rows (default 2,000) and drops the blocks farthest from the screen first. Blocks on screen are never dropped.
  • After a jump of more than one screen (a scrollbar drag, End), the grid waits until the rows on screen have held still for settleMs (default 100) before asking.

Indices, keyboard, and changes:

  • Every index is absolute: activeIndex, event indices, and aria-rowindex count from the top of the full list, loaded or not. End goes to rowCount - 1. When the active row is not loaded yet, dj-active-change fires with row: undefined, then again with the row once it arrives. dj-activate does nothing on a placeholder.
  • Sorting is the host's job. A header click updates sorting, emits dj-sort, drops the cached rows, scrolls to the top (the active row goes to 0), and requests the top rows in a new epoch. The grid never reorders rows it is given.
  • When rows are added or removed on the server, call insertRows(index, rowsOrCount) or removeRows(index, count). Cached rows, activeIndex, and the Shift anchor move with their rows, and a change above the screen scrolls by the same amount so nothing on screen jumps. For anything else, resetRows(). getVisibleRange() and scrollToIndex() let a host keep the user's place across a refresh.

Selection:

  • Select all means every row on the server, loaded or not: selectAll() turns on allSelected, and rowSelection then holds the rows the user deselected. clearSelection() turns it off.
  • dj-selection-change carries { rows, ids, all, except, count }. rows is only the loaded selected rows, so act on ids (or all and except) and count. grid.selection returns the same without rows. Inserts, removals, resets, and sorts keep select all on.
  • A Shift range (@dojo-ng/data-grid-select) over rows that are not loaded asks for them and selects once they have all arrived. If the list changes first, the range is dropped.

Plugins:

  • Supported: selectColumnPlugin, rowDragPlugin (its events carry the same selection), rowStatePlugin, cellComponentsPlugin, formatsPlugin, and columnsPlugin.
  • Not supported, because they need every row on the client: treePlugin, groupsPlugin, filterPlugin, paginationPlugin, editPlugin, detailPlugin, and exportPlugin. The grid leaves them out and logs one console warning naming them. Filter, group, and thread on the server instead.

Limits:

  • Rows have a fixed height (rowHeight).
  • Printing prints the cached rows only, not the whole list.
  • Browsers stop growing an element at about 17.8 million px (Firefox; Chromium and WebKit allow about 33 million). At the default 36 px row that is about 490,000 rows; past that the scrollbar cannot reach the end. Lower rowHeight raises the limit.

Properties

PropertyAttributeTypeDefault
columns columns GridColumn[] []
data data Row[] []
selectionMode selection-mode reflected "none""single""multiple" "none"
activationWhat a plain click / Enter on a row means. Default "none" = the original toggle behavior. activation reflected "none""click""double" "none"
rowHeight row-height number 36
height height string "20rem"
pluginsPlugin set. Set in JavaScript (rich data). Declared up front because TanStack row models must exist at table creation; a post-mount change rebuilds the table. Default []. Property only DataGridPlugin[] Plugins []
rowCountTotal number of rows on the server. A number turns on remote mode, where data is ignored and rows arrive through dj-rows-request and setRows. Default undefined (rows come from data). row-count number None
blockSizeRemote mode: rows per request block. Requests always name whole blocks. Default 100. block-size number 100
settleMsRemote mode: after a jump of more than one screen, wait this many milliseconds for the rows on screen to hold still before requesting rows. 0 requests at once. Default 100. settle-ms number 100
cacheSizeRemote mode: the most rows the grid keeps loaded. Blocks farthest from the screen are dropped first; blocks on screen never are. Default 2000. cache-size number 2000

Events

EventDescription
dj-active-changedetail { index, row }
dj-selection-changedetail { rows, ids, all, except, count }
dj-rows-requestdetail { start, end, epoch, reason }, remote mode only
dj-column-state-changedetail: the columnState
dj-sort
dj-range-changedetail { start, end, count, rendered } — inclusive first and last rendered row-model indices, the total row count, and the full index list; start and end are -1 when nothing is rendered
dj-activatedetail { row, index }, where row is the original row data

CSS parts

Style these with dj-data-grid::part(name).

gridheadrowrow--activerow--selectedrow--placeholdercellchrome-topchrome-bottomsubheadsort-buttondetail

CSS custom properties

PropertyDefaultDescription
--dj-data-grid-placeholderNoneThe bar drawn in each cell of a row that is not loaded yet.

Methods

MethodDescription
focus(options: FocusOptions)Forward focus to the element that owns the keyboard (the role="grid" div). The print table also carries part="grid", so this selects by class, not by part.
blur()
setRows(start: number, rows: Row[], epoch: number): booleanRemote mode: answer a dj-rows-request. start is the absolute index of rows[0]; pass back the request's epoch. Returns false, and keeps nothing, when the epoch is stale (the list changed since the request) or the grid is not in remote mode.
selectAll()Select every row. In remote mode this is every row on the server, loaded or not (allSelected); elsewhere it selects every row in the row model. Needs selectionMode="multiple". Emits dj-selection-change.
clearSelection()Deselect every row and turn allSelected off. Emits dj-selection-change if anything was selected.
resetRows({ keepScroll = true }: { keepScroll?: boolean } = {})Remote mode: drop every cached row and ask again for the rows on screen. Call it when the list changed in a way inserts and removals cannot describe. Starts a new epoch, so answers to earlier requests are ignored. keepScroll: false also scrolls to the top.
insertRows(index: number, rowsOrCount: Row[] | number)Remote mode: rowsOrCount rows were added on the server at absolute index index. Pass the new rows to cache them, or a count to show placeholders the grid will request. Cached rows, rowCount, activeIndex, and the select plugin's Shift anchor move down with their rows; selection is by id and does not move. When the insert is above the first visible row, the view scrolls by the same amount so the rows on screen stay put. Starts a new epoch, so answers to earlier requests are ignored. Does nothing outside remote mode.
removeRows(index: number, count: number)Remote mode: rows index..index + count - 1 were removed on the server. Cached rows after them, rowCount, activeIndex, and the select plugin's Shift anchor move up. When the active row is removed, the row that slides into the first removed slot becomes active (clamped to the last row); this is silent, like any programmatic activeIndex change. Rows removed above the first visible row scroll the view by the same amount, so the rows on screen stay put. Starts a new epoch. Does nothing outside remote mode.
scrollToIndex(index: number, { align = "auto" }: { align?: "start" | "center" | "end" | "auto" } = {})Scroll so row index is in view. align is "start", "center", "end", or "auto" (the default: scroll only as far as needed). In remote mode the index is absolute. Works in both modes.
getVisibleRange(): VisibleRangeThe first and last row (inclusive) inside the viewport, without the overscan rows the grid also renders. A row partly in view counts. In remote mode the indices are absolute. Both are -1 when there are no rows. A host can read it before a refresh to keep what the user was looking at in place.
toggleAt(index: number)
activateAt(index: number)Emit dj-activate for a row-model index. Fires regardless of selectionMode (a read-only list with clickable rows is a real case) but never under activation="none".

Types

The types that the properties above use, as they are declared in the source.

GridColumn

export interface GridColumn {
  id: string;
  header?: string;
  accessorKey?: string;
  sortable?: boolean;
  width?: string;
  /** Derive the cell value from the whole row (calculated columns). Maps to a TanStack
   *  `accessorFn`. A row total is `compute: r => r.a + r.b`; no plugin needed. */
  compute?: (row: Row) => unknown;
  /** TanStack aggregation for this column when grouping (set by the groups plugin's columns()
   *  hook; copied onto the ColumnDef). Core never sets it itself. */
  aggregationFn?: AggregationFnOption<Row>;
  /** The narrowest the column may be resized, in pixels. Also the floor of a flexible track. */
  minWidth?: number;
  /** The widest the column may be resized, in pixels. */
  maxWidth?: number;
  /** Pin the column to the inline start or end at first. Only the starting value: the live
   *  value is `columnState.pinning`. */
  pinned?: "start" | "end";
  /** With `@dojo-ng/data-grid-columns`: whether the user may resize it. Default `true`. */
  resizable?: boolean;
  /** With `@dojo-ng/data-grid-columns`: whether the user may move it. Default `true`. */
  reorderable?: boolean;
  /** With `@dojo-ng/data-grid-columns`: whether the user may hide it. Default `true`. */
  hideable?: boolean;
}

Row

export type Row = Record<string, unknown>;