Skip to content

Server protocols ​

A server grid is serverSide({ url, protocol }): one transport (fetch, abort, timeout, CSRF, Laravel headers, error normalization) plus one protocol that turns the grid's neutral query into a request and the response back into rows.

js
import { serverSide, yajra, ssp, dgp, laravelPaginator } from "@akinozgen17/datagrids";

use: [serverSide({ url: "/agents/data", protocol: yajra(), csrf: true })]

Which protocol? ​

ssp()yajra()laravelPaginator()dgp()
Server sideany server speaking the classic draw/start/length/columns[] wireyajra/laravel-datatablespaginate(), simplePaginate(), API resourcesthe datagrids protocol v1
Method / bodyGET query (POST form with method)POST, form-encodedGET queryPOST JSON (GET optional)
Untyped cellstextHTML (server escapes)texttext
Sortingcolumn indexcolumn indexfirst sort key (-date)keys, multiple
Searchsearch[value]search[value]searchq
Column filtersequality as anchored regex in columns[i][search]; typed: ColumnControl columns[i][columnControl] (no between / notIn)sameplain params (eq, in only)typed { op, value }
Countingalwaysalwayspaginate yes, simplePaginate noexact, estimate or none
ErrorsLaravel/HTTP shapes, error keysameLaravel/HTTP shapesRFC 9457 problem details

Use yajra() (or ssp()) to keep an existing endpoint untouched. Use laravelPaginator() for plain Eloquent pagination and API resources. Use dgp() for new endpoints: typed filters, optional counting, real error documents.

Transport options ​

OptionTypeDefaultDescription
urlstring | (query) => string—endpoint
protocolProtocol—ssp(), yajra(), laravelPaginator(), dgp() or your own
method"GET" | "POST"protocol'soverrides the protocol default
paramsobject | (query) => object—extra parameters on every request (page filters)
headersRecord<string, string>—extra headers
csrfbooleanconfigure().csrf (false)X-CSRF-TOKEN from <meta name="csrf-token">, plus _token in form bodies
timeoutnumber (ms)30000then a timeout error with Retry
fetchtypeof fetchconfigure().fetch → globalThis.fetchcustom transport (resolved at call time, so stubs work)
seedresponse body—rendered as the first page without a request (server-embedded first page)

serverSide(...).params(fn) returns a new definition with an extra params function — module definitions are immutable, so the original stays reusable:

js
const agents = serverSide({ url: "/agents/data", protocol: yajra(), csrf: true });
const active = agents.params(() => ({ status: "open" }));

What the transport adds ​

  • X-Requested-With: XMLHttpRequest on every request (Laravel's $request->ajax()), unless configure({ laravel: false }).
  • With csrf: X-CSRF-TOKEN and, for non-GET form bodies, _token.
  • credentials: "same-origin", an abort signal (a newer request aborts the older one) and the timeout.
  • Debug keys some servers leak in development (queries, input) are deleted from the response before any event or module sees it.
  • Responses are matched to requests by an internal sequence number — never by draw — so a stale response can't overwrite a newer page.

The classic SSP wire: ssp() and yajra() ​

The endpoint receives the full parameter set on every request, encoded exactly like jQuery's $.param: draw, start, length, search[value], search[regex], order[i][column|dir] and columns[i][data|name|searchable|orderable|search][value|regex] — columns in definition order, hidden ones included. The demo sends an extra unit parameter from the select; open the Request tab.

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

new Datagrid("#guide-yajra", {
  label: "Agents (yajra)",
  pageSize: 5,
  sort: [["name", "asc"]],
  columns: [
    { key: "ref", title: "Ref" },
    { key: "name", title: "Name" },
    { key: "unit", title: "Unit" },
    // untyped columns follow the protocol default: yajra escapes values on the server and sends raw HTML columns
    { key: "status_html", title: "Status", sortable: false, searchable: false },
  ],
  use: [
    serverSide({
      url: "/api/agents",
      protocol: yajra(),
      csrf: true,
      params: () => ({ unit: document.querySelector("#guide-yajra-unit").value }), // ajax.data style extra parameter
    }),
  ],
});

document.querySelector("#guide-yajra-unit").addEventListener("change", () => {
  void Datagrid.get("#guide-yajra").reload();
});
html
<form class="demo-form" onsubmit="return false">
  <label>Unit
    <select id="guide-yajra-unit">
      <option value="">All units</option>
      <option>S.T.A.R.S. Alpha</option>
      <option>S.T.A.R.S. Bravo</option>
      <option>R.P.D.</option>
      <option>Umbrella Corp.</option>
    </select>
  </label>
</form>
<table id="guide-yajra"></table>

yajra() is ssp() with two defaults changed: method: "POST" and untyped cells render as HTML, because yajra escapes every value on the server and sends rawColumns as markup. That is why every string column in the demo is a plain { key, title } object, including the server-rendered status badge. Typed columns that parse their value (column.money, date, badge) work as usual; avoid column.text() here — it would print the server's entities (&amp;) literally.

ssp() / yajra() optionDefaultDescription
data—extra payload: an object (deep-merged) or (d) => void | object (mutate d or return a replacement)
dataSrc"data"path to the rows, "" for a bare array, or (json) => rows
cells"text" / "html"default render mode for untyped columns
method"GET" / "POST"
allowAllfalseallow length=-1 ("All" in pageSizes); otherwise sizes are capped by maxPageSize

Extra top-level response keys (yajra's ->with([...])) arrive in grid.lastResponse(), in the response event's raw, and can be bound to page elements with metaBind. A response error string shows a non-blocking banner (rows that did arrive stay visible). The exact rules, with the tests that pin them, are in the SSP wire reference.

Laravel paginator: laravelPaginator() ​

php
return AgentResource::collection(
    Agent::query()->search($request->search)->sortBy($request->sort)->paginate($request->integer('per_page', 25))
);
Simulate
js
import { Datagrid, column, serverSide, laravelPaginator } from "@akinozgen17/datagrids";

new Datagrid("#guide-paginator", {
  label: "Agents (Laravel paginator)",
  pageSize: 5,
  sort: [["date", "desc"]],
  columns: [
    column.text("ref", { title: "Ref" }),
    column.text("name", { title: "Name" }),
    column.text("place", { title: "Place" }),
    column.date("date", { title: "Date" }),
  ],
  // GET /api/paginator/agents?page=1&per_page=5&sort=-date&search=…
  use: [serverSide({ url: "/api/paginator/agents", protocol: laravelPaginator() })],
});
html
<table id="guide-paginator"></table>

The response shape is detected automatically: paginate() (total, last_page), simplePaginate() (no total — the pager switches to "total unknown" and enables Next from next_page_url), cursorPaginate() (the next page's cursor is learned from next_cursor, so pages are reached in order) and JsonResource::collection() (data + meta + links).

OptionDefaultDescription
query{ page: "page", perPage: "per_page", sort: "sort", search: "search", cursor: "cursor" }parameter names
sortFormat"-key"or "key:dir" (date:desc)

Page filters (grid.filters() keys that are not columns, params) are sent as plain query parameters. Details: Laravel paginator reference.

datagrids protocol: dgp() ​

json
{ "v": 1, "page": { "size": 25, "number": 1 }, "sort": [{ "key": "amount", "dir": "desc" }],
  "q": "jill", "filters": { "status": { "op": "in", "value": ["open", "pending"] } },
  "fields": ["name", "status", "amount", "date"], "count": "exact", "requestId": "r-3" }
Simulate
js
import { Datagrid, column, serverSide, dgp } from "@akinozgen17/datagrids";
import { statusLabels, statusTones } from "./data.js";

const grid = new Datagrid("#guide-dgp", {
  label: "Agents (datagrids protocol)",
  pageSize: 5,
  sort: [["amount", "desc"]],
  columns: [
    column.text("name", { title: "Name" }),
    column.badge("status", { title: "Status", labels: statusLabels, tones: statusTones }),
    column.money("amount", { title: "Amount", currency: "USD", locale: "en-US" }),
    column.date("date", { title: "Date" }),
  ],
  use: [serverSide({ url: "/api/dgp/agents", protocol: dgp(), csrf: true })],
});

// typed filters travel as { key: { op, value } }
document.querySelector("#guide-dgp-open").addEventListener("click", () =>
  grid.filters({ status: { op: "in", value: ["open", "pending"] } }));
document.querySelector("#guide-dgp-big").addEventListener("click", () =>
  grid.filters({ amount: { op: "gte", value: 15000 } }));
document.querySelector("#guide-dgp-clear").addEventListener("click", () =>
  grid.filters({ status: null, amount: null }));
html
<div class="demo-buttons">
  <button type="button" id="guide-dgp-open">status in (open, pending)</button>
  <button type="button" id="guide-dgp-big">amount ≥ 15,000</button>
  <button type="button" id="guide-dgp-clear">clear filters</button>
</div>
<table id="guide-dgp"></table>

A JSON request with key-based sorting, typed filters (eq ne gt gte lt lte between in notIn contains notContains starts ends empty notEmpty), the field list, and a count mode. Switch the demo to count: none and the pager stops showing totals; Next follows page.hasMore. Errors are application/problem+json (RFC 9457) with code, retryable and field errors.

OptionDefaultDescription
method"POST""GET" sends page[size], sort=-amount, filter[status][in]=open,pending
count"exact""estimate" or "none"

The normative spec and JSON Schemas: datagrids protocol v1.

Errors and session expiry ​

Every failure — network, timeout, HTTP status, an error key in a 200 response, unreadable JSON — becomes one DatagridError:

ts
{ kind, status, message, detail?, retryable, raw? }
// kind: network | timeout | abort | http | server | session | forbidden | validation | parse | protocol
Server sayskindShown messageRetry
network failurenetwork"The server could not be reached…"yes
no answer in timeouttimeout"The server took too long to respond."yes
419 or 401, or a redirect to a login pagesession"Your session has expired…"no
403, or a redirect elsewhereforbiddenserver message or "You are not allowed…"no
422validationthe first validation messageno
429httpserver messageyes
5xxhttp"The server returned an error (500)."yes
200 with errorserver"The server returned an error (200)."yes

The grid shows it in place (blocking error state when there are no rows, a banner when rows are already on screen), fires error, and for session also fires sessionexpired and calls configure({ onSessionExpired }). Server-provided details (an exception message, SQL) are never shown to users; they go to the console as DG012 and appear in a collapsed "Details" block only while warnings are enabled. errorMode: "silent" | "throw" changes the in-place behaviour per grid. See States & errors.

js
configure({
  csrf: true,
  onSessionExpired: () => location.assign("/login"),
});

Writing your own protocol ​

A protocol is an object with name, cells, encode(req, query, grid) → { url, init, params? } and decode(body, query, grid) → { rows, total, filtered, hasMore?, meta? }. The transport keeps doing CSRF, headers, abort, timeout and error handling for you. grid is the grid the request belongs to (keep per-grid state, such as cursors, in a WeakMap keyed by it). Optional fields:

FieldDescription
methoddefault HTTP method ("GET" or "POST"); serverSide({ method }) overrides it
filterOpscolumn filter operators the wire can carry; the column filter menu offers only these (omitted: all)
allowAlltrue: page size -1 ("All") is accepted

To send a server default order when the sort is empty, check query.sortDefault in encode: it is false only when the grid was created with sort: [].

encode may also return params: the page parameters that actually reached the wire beyond the protocol's own fields. grid.lastRequestParams() (and with it the "all matching" payload of selection) reports them; without it, the request's params are reported. The built-in ssp / yajra return everything on the top level except draw, columns, order, start, length and search, so additions of a data function count too.

Released under the MIT License.