Selection + bulk action
Goal: users tick rows on any page — or pick "all matching" without loading thousands of rows — and one button sends a single request the server can act on.
import { Datagrid, column, serverSide, yajra, selection } from "@akinozgen17/datagrids";
import { statusLabels, statusTones } from "./data.js";
const out = document.querySelector("#rcp-bulk-payload");
const note = document.querySelector("#rcp-bulk-note");
const grid = new Datagrid("#rcp-bulk", {
label: "Field agents (bulk)",
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: 5,
// demo only: show notifications on the page (usually configure({ notify }) once, see Row actions)
host: { notify: (kind, message) => (note.textContent = `${kind}: ${message}`) },
use: [
serverSide({ url: "/api/agents", protocol: yajra(), csrf: true }),
selection({
bulk: {
actions: [
{
id: "remind",
label: "Send reminder",
icon: "send",
// the returned promise keeps the button busy until the request settles
onClick: async (payload, grid, sel) => {
const result = await grid.action("/api/action/agents/bulk/remind", {
body: payload, // { all: false, keys } or { all: true, except, query }
confirm: `Send a reminder to ${sel.count()} agents?`,
success: "Reminders queued.",
reload: false, // nothing in the list changed
});
if (result !== undefined) sel.clear(); // undefined = the user cancelled
},
},
],
},
}),
],
});
grid.on("selectionchange", () => (out.textContent = JSON.stringify(grid.selection.payload(), null, 2)));<table id="rcp-bulk"></table>
<div class="demo-output" id="rcp-bulk-note" aria-live="polite">Notifications appear here.</div>
<pre class="demo-output" id="rcp-bulk-payload">Select rows to see the payload.</pre>Try both shapes: tick two rows on different pages, then tick the header checkbox and press Select all 941 matching and untick one row. The payload under the grid is exactly what Send reminder posts; the Request tab shows it on the wire.
Steps
1. Add the checkbox column and the selection module
columns: [column.select(), /* … */],
use: [
serverSide({ url: "/api/agents", protocol: yajra(), csrf: true }),
selection({ bulk: { actions: [/* … */] } }),
],column.select() places the checkbox column; selection() fills it, adds the header checkbox and the bulk bar above the table. Selection survives paging, sorting and searching: rows are tracked by key (id, else DT_RowId, or your rowKey).
2. Post the payload with grid.action()
Each bulk action gets (payload, grid, selection). Hand the payload to grid.action() — it asks for confirmation, sends JSON with the CSRF header, reports the result and reloads the grid (keeping the page) unless you say reload: false:
onClick: async (payload, grid, sel) => {
const result = await grid.action("/agents/bulk/remind", {
body: payload,
confirm: `Send a reminder to ${sel.count()} agents?`,
});
if (result !== undefined) sel.clear(); // undefined = the user cancelled
},Return the promise (an async function does): the bar keeps its buttons disabled until it settles, so nobody double-submits.
3. Handle both shapes on the server
payload | Meaning |
|---|---|
{ all: false, keys: ["1998", "2000"] } | exactly these rows |
{ all: true, except: ["1999"], query: { search, sort, filters, params } } | every row matching query, minus except |
Keys are strings. query is the grid's state when "all matching" was chosen — paging left out. query.params holds the page parameters of the request behind the rows on screen at that moment (grid.lastRequestParams()): values from a filter panel, softDeletes or the source's params, without _token.
// routes/web.php
Route::post('/agents/bulk/remind', [AgentBulkController::class, 'remind']);// app/Http/Controllers/AgentBulkController.php
public function remind(Request $request)
{
$data = $request->validate([
'all' => ['required', 'boolean'],
'keys' => ['exclude_if:all,true', 'required', 'array', 'max:1000'],
'keys.*' => ['integer'],
'except' => ['array'],
'except.*' => ['integer'],
'query.search' => ['nullable', 'string', 'max:200'],
]);
$agents = $data['all']
? Agent::query()->listSearch($data['query']['search'] ?? null)->whereKeyNot($data['except'] ?? [])
: Agent::query()->whereKey($data['keys']);
$count = 0;
$agents->chunkById(500, function ($chunk) use (&$count) {
foreach ($chunk as $agent) {
$this->authorize('remind', $agent);
SendReminder::dispatch($agent);
$count++;
}
});
return response()->json(['message' => "Reminder queued for {$count} agents."]);
}// app/Models/Agent.php — one search definition, used by the list endpoint and the bulk endpoint
public function scopeListSearch(Builder $query, ?string $term): void
{
$query->when($term, fn ($q) => $q->where(fn ($q) => $q
->where('name', 'like', "%{$term}%")
->orWhere('ref', 'like', "%{$term}%")));
}Rebuild "all matching" with the same search and filter code the list uses. If the bulk endpoint searches differently, it acts on rows the user never saw.
Notes
- Page filters are in
payload.query.params. Rebuild "all matching" from them on the server together withsearchandfilters; no need to send the form values separately. - "All matching" resets itself when the search, filters or page parameters change, or when the matching count changes on reload, and the live region says so. A stale "all" never reaches your endpoint.
- The success message:
grid.action()showssuccessif you pass it, else themessageof the JSON response, else a generic "Done.". Passsuccess: falseto stay silent. The demo passessuccessbecause its server returns no message. - Notifications go to your
notifyadapter (toast library) when one is configured; without one they are announced to screen readers and errors also appear as a banner. See Row actions. can: falseon a bulk action removes its button — for the UI only; the endpoint must still authorize every row, as above.- Only the current page is in the browser on a server grid:
grid.selection.get()returns the rows it has seen,payload()is what describes the whole selection.
Related
- selection module — modes, options, API and events
- Row actions with confirm
- Page filter panel