Skip to content

Toolbar buttons ​

The button namespace holds small toolbar modules: reload, reset view, and factories for your own buttons and dropdowns.

js
import { Datagrid, column, serverSide, yajra, stateSave, colVis, button } from "@akinozgen17/datagrids";
import { statusLabels, statusTones } from "./data.js";

new Datagrid("#mod-buttons", {
  label: "Field agents (toolbar buttons)",
  columns: [
    { key: "ref", title: "Ref" },
    { key: "name", title: "Name" },
    { key: "unit", title: "Unit" },
    { key: "task", title: "Task", visible: false },
    column.badge("status", { title: "Status", labels: statusLabels, tones: statusTones }),
  ],
  pageSize: 10,
  use: [
    serverSide({ url: "/api/agents", protocol: yajra(), csrf: true }),
    stateSave(),
    colVis(),
    button.reload(),
    button.resetState(),
  ],
});
html
<table id="mod-buttons"></table>

button.custom() and button.group() add your own actions to the same toolbar:

js
import { Datagrid, column, button } from "@akinozgen17/datagrids";
import { agents } from "./data.js";

const out = document.querySelector("#mod-buttons-custom-out");
let next = 1001;

new Datagrid("#mod-buttons-custom", {
  label: "Field agents (custom buttons)",
  columns: [
    column.text("ref", { title: "Ref" }),
    column.text("name", { title: "Name" }),
    column.text("unit", { title: "Unit" }),
  ],
  data: agents(12),
  pageSize: 5,
  use: [
    button.custom({
      name: "addButton",
      label: "Add agent",
      variant: "primary",
      onClick: (grid) => grid.addRows([{ id: next, ref: `AG-${next++}`, name: "Rebecca Chambers", unit: "S.T.A.R.S. Bravo" }]),
    }),
    button.group({
      label: "More",
      icon: "more",
      items: [
        { id: "copy", label: "Copy names", icon: "copy", onClick: (grid) => (out.textContent = grid.viewRows().map((r) => r.name).join(", ")) },
        { id: "count", label: "Count rows", onClick: (grid) => (out.textContent = `${grid.rows().length} rows`) },
      ],
    }),
  ],
});
html
<table id="mod-buttons-custom"></table>
<div class="demo-output" id="mod-buttons-custom-out" aria-live="polite"></div>

When to use it ​

Use button.reload() on lists whose data changes while the page is open, button.resetState() next to stateSave so people can undo their customizations, and button.custom() / button.group() for page actions that belong to the list ("New agent", "Import…"). Row-level actions belong in actions, multi-row actions in the selection bulk bar.

Usage ​

button.reload ​

js
import { Datagrid, button } from "@akinozgen17/datagrids";

new Datagrid("#agents", { label: "Field agents", columns, use: [source, button.reload()] });

An icon-only button that reloads and keeps the current page; the icon spins while the grid loads. With silent: true the reload skips the busy card, for lists that refresh often:

js
button.reload({ silent: true });

button.resetState ​

js
use: [source, stateSave(), colVis(), button.resetState()];

Clears the stored state and restores the initial view — columns, sort, page size — with a single reload, without reloading the page. It requires stateSave: without it the grid throws DG007.

button.custom ​

js
button.custom({
  name: "newButton",
  label: "New agent",
  icon: "edit",
  variant: "primary",
  onClick: (grid) => openAgentForm({ onSaved: () => grid.reload({ keepPage: true }) }),
});

Enable or disable it from the last response — re-evaluated after every load:

js
button.custom({
  name: "payButton",
  label: "Pay all",
  disabled: (raw) => !raw?.summary?.payable,
  onClick: (grid) => grid.action("/agents/pay", { confirm: "Pay every open item?" }),
});

disabled is not called before the first server response — the button stays enabled until then — and never on array or DOM grids, which have no response (grid.lastResponse() is null).

onClick may return a promise; the button is aria-busy until it settles.

button.group ​

js
button.group({
  name: "moreButton",
  label: "More",
  icon: "more",
  items: [
    { id: "import", label: "Import…", onClick: (grid) => openImport(grid) },
    { id: "archive", label: "Archive old records", can: user.isAdmin, onClick: archiveOld },
  ],
});

Options ​

Reload ​

OptionTypeDefaultDescription
silentbooleanfalseReload without the busy card.
labelstring"Reload"Accessible name and tooltip (the button shows only the icon).
messages{ reload, resetState, resetStateTitle }locale (en / tr)Translations.
slot"start" | "end""start"Toolbar slot.
ordernumber20Position inside the slot.
canbooleantruePermission flag.

Reset ​

OptionTypeDefaultDescription
labelstring"Reset"Visible text and accessible name. The tooltip reads "Reset the view to its defaults".
messages{ reload, resetState, resetStateTitle }locale (en / tr)Translations.
slot"start" | "end""start"Toolbar slot.
ordernumber30Position inside the slot.
canbooleantruePermission flag.

Custom ​

OptionTypeDefaultDescription
labelstring— (required)Text and accessible name.
onClick(grid) => unknown— (required)Runs on click; a returned promise keeps the button busy.
namestringbutton1, button2, …Module name and API key (grid[name]); unique per grid.
iconicon namenoneBuilt-in icon (edit, download, send, copy, trash, …).
iconOnlybooleanfalseHide the text (it stays the accessible name).
variant"primary" | "ghost"default styleVisual weight.
disabled(raw) => booleannoneDisabled state from the last response, re-evaluated after each load; not called while there is no response (enabled until the first one; array / DOM grids).
slot / order"start" / 50Toolbar position.
canbooleantruePermission flag.

Group ​

OptionTypeDefaultDescription
labelstring— (required)Menu button text and the menu's name.
itemsArray<{ id, label, icon?, can?, onClick(grid) }>— (required)Menu items; can: false removes one.
namestringbuttonGroup1, …Module name and API key.
iconicon namenoneIcon before the label.
slot / order"start" / 60Toolbar position.
canbooleantruePermission flag.

API added to the grid ​

MemberReturnsDescription
grid.reloadButton.elHTMLButtonElementThe reload button.
grid.resetStateButton.elHTMLButtonElementThe reset button.
grid[name].elHTMLButtonElementA custom button or a group's menu button (give it a name to know the key).

Events ​

None of their own. Reload and reset trigger the grid's beforeLoad / load; the reload icon follows loadingchange.

Pipelines it plugs into ​

  • Toolbar: start slot by default — reload 20, reset 30, custom 50, group 60 (after colVis 10 and before / around the Export menu 40). See toolbar order.
  • Response (custom): disabled(raw) runs after every load.

Works with ​

  • button.resetState() requires stateSave and also resets what other modules saved (for example the column order from colReorder).
  • button.reload() pairs with the error state: the "Retry" button in the table does the same.

Accessibility ​

  • All of them are native <button>s. The icon-only reload button has an accessible name and a tooltip ("Reload"); the reset button keeps its name when a narrow grid hides button text.
  • While a reload runs, the reload button carries data-loading and its icon spins (a slow pulse instead with prefers-reduced-motion); the table itself is aria-busy.
  • button.group() follows the APG menu-button pattern: ↓ / ↑ open on the first / last item, arrows move, Esc closes, Tab leaves the menu.

Replaces in DataTables ​

DataTables (Buttons, 1.10 / 2.x / 3.x)datagrids
{ text: "Reload", action: (e, dt) => dt.ajax.reload(null, false) }button.reload()
dt.state.clear(); location.reload() in a buttonbutton.resetState()
buttons: [{ text, action }]button.custom({ label, onClick })
dt.button(i).enable() / .disable() after xhr.dtdisabled: (raw) => …
{ extend: "collection", buttons: [...] }button.group({ items })

Gotchas ​

  • Module resetStateButton requires state (DG007). Add stateSave() to use.
  • grid.button1 is undefined. Automatic names count across all grids on the page; pass name and use that key.
  • Module … is attached twice (DG009). Two custom buttons or groups were given the same name.

Released under the MIT License.