Skip to content

selection ​

Adds a checkbox to every row, keeps the selection across pages, offers Select all N matching once a page is fully selected, and shows a bulk-action bar above the table.

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

const out = document.querySelector("#mod-selection-bulk-out");

// POST the selection payload; grid.action adds the CSRF header, notifies and reloads
const bulk = (verb) => (payload, grid) => {
  out.textContent = `POST /api/action/agents/bulk/${verb}\n${JSON.stringify(payload)}`;
  return grid.action(`/api/action/agents/bulk/${verb}`, { body: payload });
};

new Datagrid("#mod-selection-bulk", {
  label: "Field agents (bulk actions)",
  columns: [
    column.select(),
    { key: "ref", title: "Ref" },
    { key: "name", title: "Name" },
    { key: "unit", title: "Unit" },
    column.badge("status", { title: "Status", labels: statusLabels, tones: statusTones }),
  ],
  pageSize: 10,
  host: { notify: (kind, message) => (out.textContent += `\n→ ${kind}: ${message}`) },
  use: [
    serverSide({ url: "/api/agents", protocol: yajra(), csrf: true }),
    selection({
      bulk: {
        actions: [
          { id: "approve", label: "Approve", icon: "check", onClick: bulk("approve") },
          {
            id: "archive", label: "Archive", icon: "trash", tone: "danger",
            onClick: async (payload, grid, sel) => {
              const ok = await grid.confirm(`Archive ${sel.count()} records?`, { tone: "danger", confirmLabel: "Archive" });
              if (!ok) return;
              await bulk("archive")(payload, grid);
              sel.clear();
            },
          },
        ],
      },
    }),
  ],
});
html
<table id="mod-selection-bulk"></table>
<div class="demo-output" id="mod-selection-bulk-out" aria-live="polite">Select rows, then press a bulk action.</div>

Single mode — one row at a time, no bulk bar:

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

const grid = new Datagrid("#mod-selection-single", {
  label: "Field agents (single selection)",
  columns: [
    column.select(),
    column.text("name", { title: "Name" }),
    column.text("unit", { title: "Unit" }),
    column.text("place", { title: "Place" }),
  ],
  data: agents(40),
  pageSize: 6,
  use: [selection({ mode: "single", bulk: false })],
});

const out = document.querySelector("#mod-selection-single-out");
grid.on("selectionchange", () => {
  const [row] = grid.selection.get();
  out.textContent = row ? `Assigned to: ${row.name} (${row.unit})` : "Nobody selected.";
});
html
<table id="mod-selection-single"></table>
<div class="demo-output" id="mod-selection-single-out" aria-live="polite">Nobody selected.</div>

When to use it ​

Use it when people act on many records at once: approve, export, assign, delete. For one record at a time, row actions are simpler. Single mode fits "pick one" flows (assign to an agent, choose a template).

Usage ​

Add column.select() where the checkboxes go and attach the module:

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

const grid = new Datagrid("#agents", {
  label: "Field agents",
  columns: [column.select(), { key: "name", title: "Name" }],
  use: [
    serverSide({ url: "/api/agents", protocol: yajra(), csrf: true }),
    selection({
      bulk: {
        actions: [
          {
            id: "approve", label: "Approve", icon: "check",
            onClick: (payload) =>
              grid.action("/agents/bulk-approve", { method: "POST", body: payload }),
          },
        ],
      },
    }),
  ],
});

Returning a promise from onClick keeps the bar's buttons disabled until it settles. grid.action() adds the CSRF header, shows the result and reloads.

The payload ​

Bulk actions receive what the selection stands for, never a huge list of loaded rows:

js
{ all: false, keys: ["12", "15", "31"] }                       // picked rows
{ all: true, except: ["15"], query: { search, sort, filters, params } } // "select all N matching", minus unticked rows

query.params holds the page parameters of the request behind the rows on screen when "all matching" was chosen (the same as grid.lastRequestParams()): filterPanel values, the softDeletes flag, the source's params and, with ssp / yajra, non-column filters and the additions of a data function. The CSRF token and the lazyColumns parameter are left out. A change of any of them clears an "all matching" selection.

With all: true, the endpoint re-runs the query and skips except:

php
$ids = $request->boolean('all')
    ? $this->agentQuery($request->input('query'))->whereNotIn('id', $request->input('except', []))->pluck('id')
    : $request->input('keys', []);

Selecting ​

  • Click a checkbox, or anywhere in its cell. Shift-click another row to apply the same state to the whole range.
  • The header checkbox selects or clears the current page (indeterminate when partly selected).
  • Once the page is fully selected and more rows match, the bar offers Select all N matching.
  • Changing the search, filters or page parameters (or a different matching count) clears an "all matching" selection and announces it; picked keys survive paging and filtering.

Options ​

OptionTypeDefaultDescription
mode"multi" | "single""multi"single: one row at a time, no header checkbox, no "select all".
acrossPagesbooleantrueKeep selections when the page changes; false drops off-page keys.
selectAllMatchingbooleantrueOffer "Select all N matching" (multi mode with acrossPages).
label(row) => stringthe first text column's valueRow name in the checkbox's accessible name ("Select: Jill Valentine").
bulkfalse | { actions?: BulkAction[] }a bar without actionsThe persistent bar above the table; false removes it.
messagesPartial<SelectionMessages>locale (en / tr)Bar texts, checkbox names and announcements.
canbooleantruePermission flag: false → no checkboxes, no bar.

BulkAction ​

PropertyTypeDescription
idstringUnique id (data-action on the button).
labelstringButton text and accessible name.
iconicon name or SVG stringBuilt-in icon (check, trash, download, send, …) or a trusted inline SVG.
tone"danger"Red once something is selected.
canbooleanfalse → the button does not exist.
onClick(payload, grid, selection) => void | PromiseRuns with the current payload.

API added to the grid ​

Available as grid.selection (typed: grid.module(selection)).

MemberReturnsDescription
payload()SelectionPayloadWhat a bulk action should send (see above).
count()numberSelected rows (in "all" mode: matching count minus exceptions).
get()Row[]Selected rows the grid knows (rows from other pages are kept as copies; on client grids "all" returns every matching row).
keys()string[]Selected keys (in "all" mode only the loaded ones — use payload()).
isAll()booleantrue while "all matching" is selected.
isSelected(key)booleanOne row's state.
select(keys) / deselect(keys) / toggle(key)voidChange rows by key.
selectPage() / deselectPage()voidThe current page.
selectAllMatching()voidSame as the bar's button.
clear()voidClears everything.

Events ​

EventPayloadWhen
selectionchange{ count, keys, all }After every change (clicks, API calls, an "all" selection cleared by a new query).

Pipelines it plugs into ​

  • Row mutator: a checkbox in each column.select() cell; data-selected on selected rows.
  • Header: the page checkbox (multi mode).
  • Above the table: the bulk bar.
  • Lifecycle: checks before each load and after each render whether an "all" selection still stands for the same query; on client grids it drops keys of rows that no longer exist.

Works with ​

  • Requires column.select() in the columns (a console warning tells you when it is missing) and a row key: rowKey, id or DT_RowId.
  • With actions: row actions for one record, the bar for many.
  • With filterPanel on server grids: panel values are request parameters; an all: true payload carries them in query.params, as they were when "all matching" was chosen (a later change clears that selection).

Accessibility ​

  • Native checkboxes with names: "Select: Jill Valentine" per row, "Select all on this page" in the header (with the indeterminate state).
  • The bar is a labelled group ("Selection") and stays in place even with nothing selected, so the layout never jumps; its actions are disabled until something is selected.
  • Changes are announced politely: "3 rows selected", "All 941 matching rows selected", "Selection cleared".
  • When a bar button is replaced (select all ↔ clear), focus moves to its neighbour instead of getting lost.

Replaces in DataTables ​

DataTablesdatagrids
Select extension, select: { style: "multi" } (1.10 / 2.x / 3.x)selection()
select: { style: "single" }selection({ mode: "single" })
DataTable.render.select() checkbox column (2.x / 3.x)column.select()
table.rows({ selected: true }).data()grid.selection.get() / payload()
select / deselect eventsselectionchange
Hand-made id arrays that reset on every drawKeys kept across pages; "select all N matching" with except

Gotchas ​

  • Selections vanish when the page changes. The rows have no key (a console warning says so); set rowKey. Or acrossPages: false is set.
  • "Select all N matching" never shows up. It appears only when the whole page is selected and more rows match than are on the page, in multi mode.
  • The "all" selection was cleared after typing a search. By design: it stood for a different set of rows. Picked keys are kept.
  • keys() returns only 10 keys in "all" mode. Only the loaded rows are known; send payload() to the server.

Released under the MIT License.