Skip to content

Static API & configure() ​

Everything is a named export of the package. The IIFE build exposes the same names on window.Datagrids:

js
import { Datagrid, column, configure, find, reload, upgrade } from "@akinozgen17/datagrids";
html
<script src="https://unpkg.com/@akinozgen17/datagrids/dist/datagrids.global.js"></script>
<script>
  const { Datagrid, column, configure } = Datagrids;
  Datagrids.reload("agents");
</script>

Exports ​

ExportKindSee
Datagridclass (new Datagrid(table, options), Datagrid.get())Grid options, Instance methods
columnnamespace: text, html, number, money, date, badge, link, phone, boolean, index, actions, select; helpers parseDate, parseBoolColumn options
arraySource, domSource, fetchAll, customSource, sourceModuledata source modulesData sources
serverSide, ssp, yajra, dgp, laravelPaginatorserver source and protocolsServer protocols
stateSave, colVis, colReorder, columnFilters, filterPanel, filterPresets, urlSync, softDeletes, actions, selection, excel, csv, print, footer, metaBind, responsive, lazyColumns, stickyHeader, keyboardGrid, rowReorder, multiSort, autoRefreshfeature modulesModules
reorderColumn, REORDER_KEYthe drag-handle column for rowReorder() and its key ("__reorder")rowReorder
buttonnamespace: reload, resetState, custom, groupButtons
configure, configglobal defaultsbelow
get, find, reloadgrid lookupbelow
upgrade, presetdeclarative initbelow
en, trmessage packsMessages
VERSIONstringthe package version
SelectionModelclassthe selection state model used by selection()

All TypeScript types (DatagridOptions, ColumnDef, ModuleDef, Query, Messages, …) are exported too.

get() ​

ts
Datagrid.get(target: Element | string | null): Datagrid | null
get(target: Element | string | null): Datagrid | null

Returns the grid of a table, or null. target is the table, any element inside it, or a selector. It never creates a grid, so get() is safe to call anywhere.

find() ​

ts
find(name: string): Datagrid | null

Returns the live grid created with name, or null. Names are registered at construction and removed on destroy().

reload() ​

ts
reload(name: string, options?: { keepPage?: boolean; silent?: boolean }): boolean

Reloads the grid called name (for example after a dialog in another tab saved a record). Returns false and warns DG006 when there is no such grid.

configure() ​

ts
configure(patch: GlobalConfig): GlobalConfig

Sets host-wide defaults and returns the resulting config. Keys you leave out (or pass as undefined) keep their value. A grid's own options always win over these.

KeyDefaultDescription
confirmbuilt-in <dialog>(message, options) => Promise<boolean>. Confirmation adapter for grid.confirm(), row actions and grid.action({ confirm }). options: title, tone, confirmLabel, cancelLabel, subject.
notifylive region + error banner(kind, message) => void. Toast adapter for grid.notify(), action results and export failures; kind is success, error or info.
csrffalseServer sources and fetchAll send X-CSRF-TOKEN from <meta name="csrf-token">; non-GET server requests also carry a _token parameter. grid.action() always sends the token when the meta tag exists.
laraveltrueSends X-Requested-With: XMLHttpRequest with server requests, fetchAll and grid.action(), so Laravel answers with JSON.
timeout30000Server request timeout in ms; serverSide({ timeout }) overrides it.
onSessionExpired—(error) => void. Called when a load gets 401, 419 or a redirect to a login page; the sessionexpired event fires as well.
fetchglobalThis.fetchReplaces fetch for server sources, fetchAll and grid.action() (tests, auth wrappers).
isError—(body, res) => boolean | string | null. For grid.action() (and row actions with request): a 2xx response whose parsed body still reports a failure. Truthy = failed, a string is the message. Example: (b) => b?.status === false && (b.error ?? true).
messages—Partial<Messages>: UI string overrides for every grid, merged over the language pack.
locale<html lang>, else the browser's language, else "en"Default locale of every grid.
pageSize25Default rows per page.
pageSizes[10, 25, 50, 100]Default page-size menu.
maxPageSize1000Default page size limit and server "export all" row limit.
warningstrueDevelopment messages: coded console warnings, module warnings and the server error "Details" disclosure. Turn off in production.
errorMode"inline""inline", "silent" or "throw"; see Grid options.

When a grid reads them: the host adapters (confirm, notify, csrf, laravel, timeout, onSessionExpired, fetch, isError), messages, locale, pageSize and pageSizes are read when a grid is created, so call configure() before creating grids. errorMode, maxPageSize and warnings are read when they are needed and also affect existing grids.

js
import { configure } from "@akinozgen17/datagrids";

configure({
  csrf: true,
  notify: (kind, message) => toast[kind](message),
  confirm: (message, o) => dialogs.ask({ message, title: o.title, danger: o.tone === "danger" }),
  onSessionExpired: () => location.assign("/login"),
  warnings: import.meta.env.DEV,
});

config ​

The live global config object that configure() writes to. Read it for the current defaults; don't assign to it directly (warnings are only switched through configure()).

upgrade() ​

ts
upgrade(root?: ParentNode): Datagrid[]

Creates a grid for every table[data-datagrid] under root (default document) that doesn't have one yet and returns the new grids. Calling it again (after injecting HTML) is safe: existing grids are skipped.

Table attributeBecomes
data-datagridMarker. Its value is a preset name whose modules are attached; empty for none.
data-dg-namename
data-dg-labellabel
data-dg-loadload (eager, visible, manual)
data-dg-page-sizepageSize
data-dg-sortsort, as key:dir (date:desc; any other dir is ascending)
data-dg-themetheme
data-dg-sizesize
data-dg-densitydensity

Only these attributes are read; there is no JSON configuration attribute. Columns come from the <thead> header attributes, rows from the existing <tbody>, and everything else from the preset.

html
<table data-datagrid="list" data-dg-name="agents" data-dg-label="Agents" data-dg-sort="date:desc"
       data-url="/agents/data">
  <thead><tr><th data-dg-key="name">Name</th><th data-dg-key="date" data-dg-type="date">Date</th></tr></thead>
</table>

preset() ​

ts
preset(name: string, factory: (table: HTMLTableElement) => ModuleInput[]): void

Registers modules for upgrade(). The factory runs once per table and receives it, so it can read the table's own attributes:

js
import { preset, upgrade, serverSide, yajra, stateSave, colVis, excel } from "@akinozgen17/datagrids";

preset("list", (table) => [
  serverSide({ url: table.dataset.url, protocol: yajra(), csrf: true }),
  stateSave(),
  colVis(),
  excel(),
]);
upgrade();

A preset is just an array of modules: the same array works in use: [...]. An unknown preset name attaches nothing and does not warn.

VERSION ​

VERSION: string, the library version, e.g. for bug reports.

en, tr ​

The English and Turkish message packs (Messages objects). Spread one to start a new language:

js
import { en } from "@akinozgen17/datagrids";

const de = { ...en, search: "Suchen…", searchLabel: "Suchen", retry: "Erneut versuchen" };
new Datagrid("#agents", { label: "Agenten", locale: "de", messages: de, columns });

Released under the MIT License.