Skip to content

Column options ​

A column is a plain ColumnDef object. The column.* factories build one with sensible typed defaults; whatever you pass in the options object is spread over those defaults, so any field below can be overridden (column.money("amount", { align: "start" })).

Cells are text by default; HTML is an explicit opt-in.

js
import { Datagrid, column, selection, actions } from "@akinozgen17/datagrids";
import { agents, statusLabels, statusTones } from "./data.js";

const rows = agents(40).map((a) => ({
  ...a,
  hours: (a.id % 9) * 1.25,
  note: a.status === "done" ? "<strong>Closed</strong>" : "<em>In progress</em>",
}));
const out = document.querySelector("#ref-columns-out");

new Datagrid("#ref-columns", {
  label: "Column factories",
  columns: [
    column.select(),
    column.index(),
    column.link("ref", { title: "Ref", href: "#agent-{id}" }),
    column.text("name", { title: "Name" }),
    column.html("note", { title: "Note" }),
    column.number("hours", { title: "Hours", decimals: 1, locale: "en-US" }),
    column.money("amount", { title: "Amount", currency: "USD", locale: "en-US" }),
    column.date("date", { title: "Date", time: true }),
    column.badge("status", { title: "Status", labels: statusLabels, tones: statusTones }),
    column.actions(),
  ],
  data: rows,
  pageSize: 5,
  size: "sm",
  use: [
    selection({ bulk: false }),
    actions({ items: [{ id: "view", label: "View", icon: "eye", handler: (row) => (out.textContent = `View ${row.name}`) }] }),
  ],
});
html
<table id="ref-columns"></table>
<div class="demo-output" id="ref-columns-out">Click "View" in a row.</div>

ColumnDef fields ​

FieldTypeDefaultDescription
keystringrequiredColumn identity: used by sort, filters, setColumnVisible, state and export. Unique per grid.
datastringkeyDotted path to the value in the row ("unit.name"). Sent as columns[i][data] on the SSP wire.
namestring""Server-side column name, sent as columns[i][name] (SSP wire only).
titlestringkeyHeader text; also used in the column menu, export headers and announcements.
typeColumnTypeset by the factorytext, number, money, date, badge, link, html, boolean, index, actions, select. Drives export typing and the operators of column filters.
sortablebooleantrue (false for special columns)Header becomes a sort button with a permanent sort icon.
sortKeystring—Client mode: sort by this row path instead of the cell value. Server protocols sort by the column itself.
searchablebooleantrue (false for special columns)Included in the toolbar search.
visiblebooleantrueInitial visibility (also the "reset" target of the column menu).
hideablebooleantrue (false for special columns)Offered in the column menu; setColumnVisible(key, false) is refused otherwise.
exportablebooleantrue (false for special columns)Included in Excel, CSV and print exports.
align"start" | "end" | "center"start (end for number and money)Written as data-align on the <th> and <td>.
classNamestring—Class for the column's <th> and <td> cells.
defaultContentstring—Text for an empty value. Without it an empty cell shows a muted dash (—).
canbooleantruePermission flag: false removes the column everywhere — render, column menu, export, state and the request.
lazybooleanfalseServer-computed on demand: with the lazyColumns module the column is requested only while it is visible.
htmlboolean—true writes the value (or a string returned by render) as HTML.
textboolean—true forces text even when the source defaults to HTML.
sticky"start" | "end"—Pins the column to an edge while the table scrolls horizontally.
render(value, row, ctx) => Node | string | number | null—Cell content. A Node is appended as is; a string or number is written as text unless the column has html: true; null, undefined or "" falls back to defaultContent. ctx is { row, key, index, position, td, locale, text }: key is the column key, index the position in the view, position = page offset + index, locale the grid's locale. value already arrives as text; ctx.text(row, path) gives any other field as text too (on an HTML-default source such as yajra() the row's strings are server-escaped and may hold markup; the row itself is never changed).
value(row) => unknown—Computed value; replaces the data lookup for render, search, client sort and client filters.
compare(a, b) => numbercollation of localeClient sort comparator for two values.
searchText(value, row) => stringString(value)Text the client search matches against.
exportValue(row) => unknown—Value written to exports instead of the cell value.
exportType"string" | "number" | "money" | "date" | "boolean"from type, else inferredCell type in Excel; numbers and dates stay real numbers and dates.
exportFormatstringby typeExcel number format, e.g. "#,##0.00", "dd.mm.yyyy".
labelsRecord<string, string>—Value → label map. Used by badge columns, column filters and export.
col"select" | "actions" | "rownum" | "go" | "handle"—Marks a special column filled by the core or a module. Set by column.select(), column.actions(), column.index(), reorderColumn() and the rowClick option; don't set it yourself.

Special columns (with col) are not sortable, searchable, hideable or exportable unless you say otherwise, keep their position when columns are reordered, and are left out of exports.

Render mode ​

Every cell is either written with textContent (text) or innerHTML (HTML). The rules, in order:

  1. render returns a Node → appended as is (all typed factories do this).
  2. html: true (or column.html()) → HTML, for the raw value and for strings returned by render.
  3. text: true (or column.text()) → text.
  4. A render function that returns a string → text.
  5. Otherwise the data source decides:
SourceUntyped cells
data: [...] / arraySource, fetchAlltext
serverSide with ssp(), dgp(), laravelPaginator()text
serverSide with yajra()HTML — yajra escapes non-raw columns on the server, so text would show &amp;
DOM source (existing <tbody>)HTML — the original rows are adopted, cell markup survives
customSourceits cells setting, default text

On a yajra endpoint leave escaped string columns untyped. Use column.text() only for a column the endpoint sends unescaped (listed in rawColumns) that must still be shown as plain text.

Formatting locale ​

number, money and date format with the grid's locale unless you pass locale on the column; money uses TRY unless you pass currency.

js
column.money("amount", { title: "Amount", currency: "USD", locale: "en-US" });
column.date("date", { title: "Date", locale: "en-US" });

column.text(key, options?) ​

Plain text, always written with textContent.

SetsValue
type"text"
texttrue

column.html(key, options?) ​

Raw HTML from the row value: the explicit opt-in. Only use it for markup your server produced and escaped.

SetsValue
type"html"
htmltrue
searchTextthe value with tags stripped
exportValuethe value with tags stripped

column.number(key, options?) ​

Extra optionTypeDefaultDescription
decimalsnumber—Fixed fraction digits. Without it: 0 to 2 digits as needed.
localestringgrid localeIntl.NumberFormat locale.
source"number" | "tr""number"How strings are read. number: machine numbers as a database sends them ("0.125", "1.500" = 1.5); other strings are read leniently. tr: formatted text ("1.234,5", "1.500" = 1500).
SetsValue
type / align"number" / "end"
comparenumeric; empty values first
exportType / exportFormat"number" / "#,##0" ("#,##0.00" with decimals: 2)
exportValuethe parsed number
render<span class="dg-num">; negative values get data-sign="neg"

column.money(key, options?) ​

Extra optionTypeDefaultDescription
currencystring (ISO 4217)"TRY"Symbol shown after the amount: ₺, $, €, otherwise the code itself.
decimalsnumber2Fraction digits (display and export format).
localestringgrid localeIntl.NumberFormat locale.
source"number" | "tr" | "html""number"How the value is read. number: machine numbers ("1234.50", "1.500" = 1.5), other strings leniently (1.234,50, 1,234.50). tr: formatted text ("1.500" = 1500). html strips tags first (server-rendered amounts).
SetsValue
type / align"money" / "end"
comparenumeric on the parsed value
exportType / exportFormat"money" / #,##0.00 "₺" (symbol of currency)
exportValuethe parsed number
searchTextthe amount with a decimal comma (1234,50)
render<span class="dg-money"> + <span class="dg-money-cur">; negative values get data-sign="neg"

column.date(key, options?) ​

Extra optionTypeDefaultDescription
source"iso" | "local" | "dmy""iso"Input format. iso: anything new Date() parses (offset-aware ISO 8601). local: YYYY-MM-DD[ HH:MM[:SS]] in the browser's time zone. dmy: DD.MM.YYYY[ HH:MM[:SS]] (., / or -). Date objects are used as they are.
timebooleanfalseAdds the time (HH:MM) in a second span.
timeZonestring (IANA)browser zoneZone used for display, e.g. "Europe/Istanbul".
localestringgrid localeIntl.DateTimeFormat locale (day, month, year as 2-digit/2-digit/numeric).
SetsValue
type"date"
compareby time; unparseable values first
exportType / exportFormat"date" / "dd.mm.yyyy" ("dd.mm.yyyy hh:mm" with time)
exportValuea Date
searchTextthe formatted date
render<span class="dg-date"> (+ <span class="dg-date-time">)

column.badge(key, options?) ​

Extra optionTypeDefaultDescription
labelsRecord<string, string>{}Value → label. Unlisted values show as they are.
tonesRecord<string, Tone>{}Value → chip tone: info, ok, warn, bad, accent; neutral, muted or no tone draw a neutral chip.
SetsValue
type"badge"
labelsthe map (column filters offer it as choices)
compareby label
searchText / exportValuethe label
render<span class="dg-chip" data-tone="…">
Extra optionTypeDefaultDescription
hrefstring | (row) => stringrequiredURL template with {path} placeholders ("/agents/{id}", "/units/{unit.id}/staff"); each value is URL-encoded. A function's result is used as is.
targetstring—Link target; "_blank" adds rel="noopener".

The URL must start with http:, https:, mailto:, tel:, /, . or #. Anything else (javascript:, data:, a bare agents/1) renders the text without a link.

SetsValue
type"link"
render<a class="dg-link"> with the cell value as text

column.phone(key, options?) ​

Extra optionTypeDefaultDescription
countrystring"90"Country calling code added when the number lacks it (a leading 0 is dropped).
whatsappbooleanfalseAdds a second "WA" link to https://wa.me/<digits>.

Renders the value as a tel: link (type: "text").

column.boolean(key, options?) ​

Yes / no with a permanent icon and text (colour is never the only signal).

Extra optionTypeDefaultDescription
labels{ true?: string, false?: string }Evet / Hayır or Yes / NoTexts; the default follows the page language (<html lang>).
showFalsebooleantruefalse leaves false cells empty.

Values are read as booleans: 1 / 0, true / false, "on" / "off", "evet" / "hayır", "yes" / "no"; anything else is empty.

SetsValue
type / align"boolean" / "center"
comparefalse before true; empty values first
searchText / exportValuethe label
render<span class="dg-bool" data-value="true|false"> with an icon and .dg-bool-text

column.parseDate(), column.parseBool() ​

The readers behind column.date() and column.boolean(), exported for your own render, value or compare functions:

js
column.parseDate("05.01.2026 14:30", "dmy"); // Date (local time) or null
column.parseBool("evet");                     // true, false or null

column.index(options?) ​

Row number that continues across pages (page offset + position).

SetsValue
key / col"__index" / "rownum"
title"#"
sortable / searchablefalse

column.actions(options?) ​

Placeholder for the actions module, which fills it with the row menu.

SetsValue
key / col"__actions" / "actions"
align / sticky"end" / "end"
sortable, searchable, exportable, hideablefalse

column.select(options?) ​

Placeholder for the selection module, which fills it with checkboxes.

SetsValue
key / col"__select" / "select"
title""
sortable, searchable, exportable, hideablefalse

Header attributes (DOM source) ​

When a server-rendered table is enhanced without columns, these attributes on the <thead> cells define the columns. See Data sources.

AttributeEffect
data-dg-keyColumn key (default: a slug of the header text).
data-dg-typeColumn type; number and money also right-align and sort numerically. Cells keep their server markup.
data-dg-alignstart, end or center.
data-dg-sortable="false"Not sortable.
data-dg-searchable="false"Not searchable.
data-dg-visible="false"Hidden initially.
data-dg-hideable="false"Not offered in the column menu.
data-dg-export="false"Left out of exports.

A data-order attribute on a <td> is used as that cell's value for sorting and search instead of its text.

Gotchas ​

  • A string from render shows tags literally: strings are text unless the column has html: true. Return a Node, or opt in with html: true.
  • "1.250" became 1.25 (or 1250): with the default source: "number" a machine-style string is read as a JS number ("1.250" = 1.25, as a DECIMAL column sends it). For formatted Turkish text ("1.250" meaning one thousand two hundred fifty) pass source: "tr". Send numbers as JSON numbers when you can.
  • A date-only ISO string shows the previous day in time zones west of UTC: new Date("2026-01-05") is UTC midnight. Use source: "local" for YYYY-MM-DD values.
  • The money symbol follows the amount in every locale (1,234.50 $).
  • column.index() is never exported; add a regular column if the export needs a number.

Released under the MIT License.