actions
Defines a row's actions once and renders them the same way on every row: up to two inline buttons, or a primary button plus a ⋯ menu. Each action is a link, a request or a handler, with an optional confirm dialog and per-row permissions.
import { Datagrid, column, serverSide, yajra, actions } from "@akinozgen17/datagrids";
import { statusLabels, statusTones } from "./data.js";
const out = document.querySelector("#mod-actions-menu-out");
const grid = new Datagrid("#mod-actions-menu", {
label: "Field agents (row actions)",
columns: [
{ key: "ref", title: "Ref" },
{ key: "name", title: "Name" },
column.badge("status", { title: "Status", labels: statusLabels, tones: statusTones }),
column.money("amount", { title: "Amount", currency: "USD", locale: "en-US" }),
column.actions({ title: "Actions" }),
],
pageSize: 10,
host: { notify: (kind, message) => (out.textContent = `${kind}: ${message}`) },
use: [
serverSide({ url: "/api/agents", protocol: yajra(), csrf: true }),
actions({
rowLabel: (row) => row.name,
items: [
{
id: "approve", label: "Approve", icon: "check", primary: true,
disabled: (row) => row.status === "done" && "Already done",
request: { url: "/api/action/agents/{id}/approve" },
},
{ id: "remind", label: "Send reminder", icon: "send", request: { url: "/api/action/agents/{id}/remind" }, success: "Reminder sent to {name}." },
{ id: "open", label: "Open record", icon: "eye", group: "View", handler: (row) => (out.textContent = `open ${row.ref}`) },
{
id: "delete", label: "Delete", icon: "trash", tone: "danger",
can: (row) => row.status !== "done",
confirm: "Delete this record? This cannot be undone.",
request: { url: "/api/action/agents/{id}", method: "DELETE" },
},
],
}),
],
});
grid.on("actionerror", ({ id, error }) => console.info(`${id} failed`, error));<table id="mod-actions-menu"></table>
<div class="demo-output" id="mod-actions-menu-out" aria-live="polite">Notifications appear here.</div>With two actions or fewer, they render inline. Here a per-row permission map from the server narrows them (drafts can only be edited, cancelled records can't be deleted — with the reason):
import { Datagrid, column, actions } from "@akinozgen17/datagrids";
import { agents, statusLabels, statusTones } from "./data.js";
// a per-row permission map, as a server would send it
const rows = agents(30).map((r) => ({
...r,
permissions: r.status === "cancelled" ? { delete: { disabled: "archived" } } : r.status === "draft" ? ["edit"] : true,
}));
const out = document.querySelector("#mod-actions-inline-out");
new Datagrid("#mod-actions-inline", {
label: "Field agents (inline actions)",
columns: [
column.text("name", { title: "Name" }),
column.text("unit", { title: "Unit" }),
column.badge("status", { title: "Status", labels: statusLabels, tones: statusTones }),
column.actions({ title: "Actions" }),
],
data: rows,
pageSize: 8,
use: [
actions({
field: "permissions",
reasons: { archived: "Cancelled records are kept for the audit log." },
items: [
{ id: "edit", label: "Edit", icon: "edit", handler: (row) => (out.textContent = `edit ${row.name}`) },
{
id: "delete", label: "Delete", icon: "trash", tone: "danger",
confirm: { title: "Delete record", message: "Delete {name}?" },
handler: (row, { grid, key }) => grid.removeRow(key),
},
],
}),
],
});<table id="mod-actions-inline"></table>
<div class="demo-output" id="mod-actions-inline-out" aria-live="polite"></div>When to use it
Use it for anything a user does to one row: view, edit, approve, delete, send. The definitions live in one place, so every row looks and behaves the same, and destructive actions always ask first. For actions on many rows at once use selection and its bulk bar.
Usage
Add column.actions() where the buttons go (a sticky end column by default) and describe the items. Every item does one thing: href, request or handler.
import { Datagrid, column, serverSide, yajra, actions } from "@akinozgen17/datagrids";
new Datagrid("#agents", {
label: "Field agents",
columns: [
{ key: "name", title: "Name" },
column.actions({ title: "Actions" }),
],
use: [
serverSide({ url: "/api/agents", protocol: yajra(), csrf: true }),
actions({
items: [
{ id: "edit", label: "Edit", icon: "edit", href: "/agents/{id}/edit" },
{
id: "delete", label: "Delete", icon: "trash", tone: "danger",
confirm: "Delete {name}?",
request: { url: "/agents/{id}", method: "DELETE" },
},
],
}),
],
});{field} placeholders read the row (dotted paths work). In href and request.url the values are URL-encoded; in confirm and success texts they are inserted as plain text.
hrefnavigates (target: "_blank"opens a new tab withnoopener). An enabledhrefaction is a real<a href>, inline and in the menu, so middle-click, Ctrl/⌘-click, "Copy link" and the status-bar URL work as on any link. A plain click still goes throughconfirmandbeforeAction; a modified or middle click opens the link directly, without them. A disabledhrefaction stays a button that explains why.requestrunsgrid.action(): same-origin only,X-CSRF-TOKENfrom<meta name="csrf-token">, JSON body, then a success notice and a reload that keeps the page.handler(row, { grid, key })runs your code; return a promise to keep the row busy until it settles.
Layout: inline or menu
| Visible actions for the row | Rendered as |
|---|---|
≤ inline (default 2) | Labelled inline buttons |
more, one has primary: true | The primary as a labelled button + an icon-only ⋯ menu with the rest |
| more, no primary | One "Actions ▾" menu button |
In the menu, group adds a heading, danger actions sink to the end of their group behind a separator, and items with confirm end with "…".
Permissions and reasons
actions({
field: "permissions", // optional per-row map from the server
reasons: { archived: "Archived records are read-only." },
items: [
{ id: "edit", label: "Edit", can: user.canEdit }, // hide for everyone
{ id: "approve", label: "Approve", can: (row) => row.status !== "done" }, // hide per row
{ id: "send", label: "Send", disabled: (row) => !row.email && "No e-mail address" }, // show, but inactive
],
});can: false(or a function returningfalse) hides the action.disabledkeeps it visible but inactive; a string becomes the reason, shown in a small note when the button is pressed and under the label in the menu.fieldnames a row field holding a permission map. It can only narrow:["edit"]allows just those ids;{ delete: false }hides one;{ delete: { disabled: "archived" } }disables one with the reasonreasons.archived(or the code itself); ids not in the map stay allowed.
can is UX only
Hiding a button is not authorization. The endpoint behind every request must check permissions itself.
Confirm and notifications
confirm opens the grid's built-in modal <dialog> (title, message, the row's label as the subject; danger actions get a red confirm button labelled with the action and start focused on "Cancel"). Plug in your own dialog and toasts once for every grid:
import { configure } from "@akinozgen17/datagrids";
configure({
confirm: (message, o) => myDialog.ask({ title: o.title, text: message, danger: o.tone === "danger" }),
notify: (kind, message) => myToasts.show(kind, message),
});Without a notify adapter, success messages go to the grid's live region and errors also show a banner above the table.
Options
actions(options)
| Option | Type | Default | Description |
|---|---|---|---|
items | ActionItem[] | — (required) | The actions, in display order. |
inline | number | 2 | Up to this many visible actions render inline; more switch to primary + menu. |
buttonGroup | boolean | true | Inline actions (and primary + ⋯) form one joined button group. Items that can turn disabled keep room for their ⓘ, so the groups line up from row to row. false: separate buttons. |
sticky | "end" | false | "end" | false lets the actions column scroll with the others. |
field | string | none | Row field with a permission map (array of ids or { id: true | false | { disabled: code } }). |
reasons | Record<string, string> | {} | Reason codes from the map → text. |
rowLabel | (row) => string | first visible column's value | Row name used in accessible names and as the confirm subject. |
messages | { actions, more, forRow(label) } | locale (en / tr) | "Actions", "More actions", "Actions: {row}". |
can | boolean | true | Permission flag: false → no action buttons at all. |
ActionItem
| Property | Type | Description |
|---|---|---|
id | string | Unique id (events, permission maps, grid.actions.run()). |
label | string | Visible text and accessible name. |
icon | icon name | Built-in icon: edit, trash, eye, send, check, copy, download, print, more, … |
tone | "danger" | "warning" | "success" | danger is styled red, sorted last and confirmed with a red button. |
group | string | Menu group heading. |
primary | boolean | The labelled button next to ⋯ when the actions don't fit inline. |
can | boolean | (row) => boolean | Hides the action. |
disabled | (row) => boolean | string | null | Visible but inactive; a string is the reason. |
href | string | URL template; rendered as a real link (see above). |
target | string | Window target for href. |
request | { url, method?, body? } | method defaults to POST; body is an object or (row) => object. |
handler | (row, { grid, key }) => unknown | Your code; may return a promise. |
confirm | string | { title?, message, confirmLabel?, cancelLabel? } | Ask first. |
success | string | false | request only: success text (template), or false for none. Default: the response's message, else "Done." |
reload | false | { keepPage? } | request only: reload after success (default { keepPage: true }). |
API added to the grid
Available as grid.actions.
| Member | Returns | Description |
|---|---|---|
run(id, key) | Promise<void> | Runs an action on a row (with its confirm, permissions and events). |
open(key) | void | Opens a row's menu (rows with a menu only). |
items(key) | Array<{ item, reason }> | The actions visible on a row after permissions; reason is null when active. |
Events
| Event | Payload | When |
|---|---|---|
beforeAction | { id, key, row, cancel() } | Before the confirm dialog. cancel() stops the action. |
action | { id, key, row } | After a request or handler finished successfully (not for href). |
actionerror | { id, key, row, error } | The request failed or the handler threw. The user already saw the error notice. |
Pipelines it plugs into
- Row mutator: fills the
column.actions()cell of every rendered row; the cell is re-rendered after an action finishes. - Lifecycle: one shared menu and one note popover per grid, removed on
destroy().
Works with
- Requires
column.actions()in the columns (a console warning tells you when it is missing). - With softDeletes: a "Restore" item with
can: (row) => !!row.deleted_at. - With selection: row actions for one record, the bulk bar for many.
Accessibility
- Every button is a native
<button>; icon-only ones (⋯) have an accessible name with the row: "More actions: Jill Valentine". - The menu follows the APG menu-button pattern:
aria-haspopup="menu", arrows /Home/Endand type-ahead move between items,Enter/Spacerun one,Esccloses,Tableaves. - Disabled actions use
aria-disabled(they stay focusable) and carry the reason in their description or name. - While an action runs, its row is
aria-busyand its buttons are disabled; a second click is ignored. - The confirm dialog is a modal
<dialog>with a labelled title and message; destructive actions focus "Cancel" first.
Replaces in DataTables
| DataTables (1.10 / 2.x / 3.x) | datagrids |
|---|---|
columns.render returning button HTML | column.actions() + items |
$("#t").on("click", ".btn-delete", …) + table.row(tr).data() | handler(row) / request |
confirm() + $.ajax + table.ajax.reload(null, false) | confirm + request (reload keeps the page) |
Hand-made permission checks in render | can, disabled, field + reasons |
Gotchas
- "Edit" leaves the page.
hrefnavigates; addtarget: "_blank"or use ahandler. - The row didn't change after a
handler. Onlyrequestreloads; callgrid.reload({ keepPage: true })orgrid.refreshRow(key, row)in your handler. - "action URL must be same-origin".
requestonly talks to your own origin; use ahandlerwith your ownfetchfor anything else. - A
{field}in the URL came out as%20. URL templates encode values; that is intended.