Skip to content

Grid options ​

ts
new Datagrid(table: HTMLTableElement | string, options?: DatagridOptions)

table is a <table> element or a selector that matches exactly one table (DG005 otherwise). Calling the constructor again on a table that already has a grid returns the existing grid. All options are optional; most grids need label, columns and either data or a source in use.

js
import { Datagrid, column, serverSide, yajra, stateSave } from "@akinozgen17/datagrids";

new Datagrid("#agents", {
  label: "Field agents",
  columns: [
    column.text("name", { title: "Name" }),
    column.date("date", { title: "Date" }),
  ],
  sort: [["date", "desc"]],
  pageSize: 50,
  use: [
    serverSide({ url: "/api/agents", protocol: yajra(), csrf: true }),
    stateSave(),
  ],
});

Defaults marked global come from configure() and can be changed host-wide there; the option on a grid always wins.

Structure ​

OptionTypeDefaultDescription
columnsColumnDef[]read from the tableColumn definitions in wire order. When omitted, the DOM source reads <thead> (data-dg-* header attributes); otherwise <thead> cells become plain columns. See Column options.
useModuleInput[][]Modules to attach: one data source plus any feature modules. Arrays nest, so a preset is an array.
dataRow[]—Shorthand for use: [arraySource(rows)] (client mode). Ignored when use already contains a source.
namestring—Registry name for find(name) and reload(name). Also the default storage key of stateSave, the urlSync namespace and the export file name prefix. Must be unique on the page.
labelstring—Accessible name of the grid (rendered visually hidden). Give every grid one; without any name the grid warns DG002.
labelledBystring—Id of a visible element that names the grid. Wins over label and title.

When none of label, labelledBy and title is set, a <caption> in the table names the grid.

Data & paging ​

OptionTypeDefaultDescription
pageSizenumber25 globalRows per page. A value above maxPageSize is clamped at construction too (DG013).
pageSizesnumber[][10, 25, 50, 100] globalChoices in the page-size menu. -1 is shown as "All" and is accepted only when the protocol allows it (ssp({ allowAll: true })). The current pageSize is added when it is missing.
maxPageSizenumber1000 globalUpper limit for grid.pageSize(n) (larger values are clamped with DG013) and the row limit of a server-side exportRows("all").
pagingbooleantruefalse shows every row on one page: no pager, no page-size menu (grid.pageSize() returns 0).
rowKeystring | (row) => stringrow.id ?? row.DT_RowIdRow identity: a dotted path or a function. Keys drive selection, refreshRow, removeRow, row actions and focus keeping. Missing keys fall back to the position with DG003.

Search & sort ​

OptionTypeDefaultDescription
searchboolean | { delay?, minLength?, placeholder? }trueThe toolbar search box. false removes the box (grid.search(text) still works).
search.delaynumber (ms)400 server · 200 clientTyping debounce. Enter searches at once; Escape clears.
search.minLengthnumber0Shorter input searches for "".
search.placeholderstringmessages.searchPlaceholder text.
sortArray<[key, "asc" | "desc"] | { key, dir }>noneInitial sort. Sorting is single-column in the UI; grid.sort() accepts several keys (client mode sorts by each in turn). On an SSP server source an empty sort sends the first column ascending (the classic server default), also after the sort was cleared at runtime; only an explicit sort: [] here sends no order at all.

Loading ​

OptionTypeDefaultDescription
load"eager" | "visible" | "manual""eager"eager loads right after construction. visible waits until the grid is within 200 px of the viewport (IntersectionObserver), for tabs and collapsed panels. manual shows a "Load" state until grid.load() or grid.reload() is called.
errorMode"inline" | "silent" | "throw""inline" globalinline shows the error state (or a banner when rows are already on screen) with a Retry button. silent keeps the last view and shows nothing. throw rejects the load() / reload() promise. The error event fires in every mode.
virtualboolean | numberautomatic at 300 rowsVirtual rows render only the visible window. They switch on when the rendered view has at least 300 rows (a number sets another threshold); false turns them off. true behaves like the default. The scroll area is then limited to maxHeight (default 70vh).
maxHeightstring (CSS length)—Limits the scroll area's height and makes the header row sticky (data-dg-sticky-head), e.g. "60vh".

Appearance ​

OptionTypeDefaultDescription
surface"plain" | "card""plain"card draws the grid on a raised surface with a header for title / description.
titlestring—Card heading. With surface: "card" it is also the grid's accessible name.
descriptionstring—Muted line under the card heading (card surface only).
size"sm" | "md" | "lg""md"Font and control size (13 px body at md).
density"compact" | "normal" | "comfortable""normal"Row height and cell padding, independent of size.
theme"light" | "dark" | "system"inheritedForces a color scheme. By default the grid follows the host's color-scheme.
head"caps" | "sentence""caps"Header style: small uppercase or sentence case. Caps needs <html lang> (DG011).
stripedbooleanfalseZebra rows.
wrapbooleantruefalse keeps every cell on one line (white-space: nowrap).
countbooleanfalseShows "N records" in the toolbar's end slot (in the card header with surface: "card").

The options map to root attributes (data-size, data-density, data-dg-theme, …) listed in CSS tokens.

Rows ​

OptionTypeDefaultDescription
rowTone(row) => Tone | null—Tints a row: "ok", "warn", "bad", "info" or "muted" (written as data-tone on the <tr>).
rowClass(row) => string | null—Extra classes for the <tr> (space-separated).
rowAttrs(row) => Record<string, string> | null—Extra attributes for the <tr>.
rowClick(row, event: MouseEvent) => void—Makes rows clickable: adds a trailing chevron column and data-clickable. Clicks on links, buttons, form controls, menu items and the select/actions cells are ignored. Keep a real link in the row for keyboard users.

Localization ​

OptionTypeDefaultDescription
localestring (BCP 47)configure({ locale }), else <html lang>, else the browser's language, else "en"Number formatting in the footer and pager, search folding and client sorting. Locales starting with tr get the Turkish message pack, all others English. Column formats are separate: see Column options.
messagesPartial<Messages>—Overrides individual UI strings, merged over the pack and configure({ messages }). See Messages.

Host adapters ​

OptionTypeDefaultDescription
hostHostAdaptersfrom configure()Per-grid override of the global host adapters: confirm, notify, csrf, laravel, timeout, onSessionExpired, fetch, isError. Keys are explained under configure().

Unknown options ​

Any other key is ignored and reported once with DG001. Well-known legacy option names get a hint that names the replacement:

Key (any case)Use instead
serverSideuse: [serverSide({ url, protocol: yajra() })]
ajaxuse: [serverSide({ url, … })]
domtoolbar slots start, end, filters
buttonsbutton modules: button.reload(), excel()…
languagemessages
stateSaveuse: [stateSave()]
lengthMenupageSizes
pageLengthpageSize
ordersort: [["key", "desc"]]
deferLoadingload: "manual"
columnDefsoptions on each column definition

Other near-misses get a suggestion (pagesize → "Did you mean pageSize?").

Gotchas ​

  • title without surface: "card" leaves the grid unnamed: the heading is not rendered, so the name it would provide is missing (and no DG002 warning appears). Use label unless the grid is a card.
  • data plus a source in use: the source wins and data is dropped silently.
  • A huge initial pageSize is clamped to maxPageSize like a later pageSize(n) call (DG013); raise maxPageSize for a bigger page.
  • locale falls back to the browser's language (then English) when neither the option, configure({ locale }) nor <html lang> holds a valid tag; invalid tags warn (DG015).

Released under the MIT License.