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.
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
pluginsproperty, from JavaScript only. - A recommended order: one structural plugin first (
treePluginorgroupsPlugin, never both), theneditPlugin,cellComponentsPlugin, andformatsPlugin, then the plugins that only add controls (filterPlugin,paginationPlugin,exportPlugin,detailPlugin). - Changing
pluginsrebuilds the table, so set it once, early.
data-grid-cell-componentsIn-row Lit components (per-column render + actionButton/checkmark helpers)data-grid-columnsColumn resize, reorder, pin, hide, and a column chooserdata-grid-detailMaster-detail (expandable row detail / subgrid)data-grid-dragRow drag source plugin (native HTML5 drag, drags the selection)data-grid-editControlled inline cell editingdata-grid-exportCSV exportdata-grid-filterQuick + per-column filteringdata-grid-formatsValue-formatting plugin (Intl number/currency/percent/date via @dojo-ng/i18n)data-grid-groupsRow grouping + aggregatesdata-grid-paginationPagination (reuses dj-pagination)data-grid-rowstateRow/cell state styling plugin (conditional row parts + cell emphasis)data-grid-selectCheckbox selection column plugin (select-all, range selection)data-grid-treeTree (hierarchical) rows
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 emitsdj-activatewith{ row, index }, whererowis 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-selectis installed (without it, Shift-click only moves the active row). "double"uses the browser's owndblclick, 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-changefires 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,
startandendare -1 andcountis the real count. - The event fires after rendering and only when
(start, end, count)changes, so settingdatain 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--activeandrow--selected, alongsiderow, 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-changefires when the user moves the active row with the keyboard or a click, with detail{ index, row }, whererowis the original row data. SettingactiveIndexfrom code does not fire it, the same way setting a native input'svaluefires nochange.
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-sortisascendingordescendingon the sorted column and absent elsewhere. A column withsortable: falsegets 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
columnStateis the user's column layout:{ sizing, order, pinning, visibility }.sizingholds pixel widths by column id,orderevery column id in display order,pinningthe ids pinned to the inlinestartandend, andvisibilityfalsefor 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-changefires with the whole state when the user changes the layout through a plugin, so storing it is one listener: storee.detail, setcolumnStateon 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.pinnedsets the starting value. minWidthandmaxWidthon a column limit its width, andminWidthis 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
columnsPluginfrom@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 agetRowId, 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 ordergrid.sortingnames, and answer withgrid.setRows(start, rows, epoch), passing back the request'sepoch. reasonis"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.setRowswith an older epoch keeps nothing and returnsfalse, 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
cacheSizerows (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, andaria-rowindexcount from the top of the full list, loaded or not. End goes torowCount - 1. When the active row is not loaded yet,dj-active-changefires withrow: undefined, then again with the row once it arrives.dj-activatedoes nothing on a placeholder. - Sorting is the host's job. A header click updates
sorting, emitsdj-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)orremoveRows(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()andscrollToIndex()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 onallSelected, androwSelectionthen holds the rows the user deselected.clearSelection()turns it off. dj-selection-changecarries{ rows, ids, all, except, count }.rowsis only the loaded selected rows, so act onids(orallandexcept) andcount.grid.selectionreturns the same withoutrows. 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 sameselection),rowStatePlugin,cellComponentsPlugin,formatsPlugin, andcolumnsPlugin. - Not supported, because they need every row on the client:
treePlugin,groupsPlugin,filterPlugin,paginationPlugin,editPlugin,detailPlugin, andexportPlugin. 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
rowHeightraises the limit.
Properties
| Property | Attribute | Type | Default |
|---|---|---|---|
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
| Event | Description |
|---|---|
dj-active-change | detail { index, row } |
dj-selection-change | detail { rows, ids, all, except, count } |
dj-rows-request | detail { start, end, epoch, reason },
remote mode only |
dj-column-state-change | detail: the columnState |
dj-sort | |
dj-range-change | detail { 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-activate | detail { 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
| Property | Default | Description |
|---|---|---|
--dj-data-grid-placeholder | None | The bar drawn in each cell of a row that is not loaded yet. |
Methods
| Method | Description |
|---|---|
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): boolean | Remote 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(): VisibleRange | The 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>;