lazyColumns
Some columns are expensive to compute on the server (a balance, a joined count). Mark them lazy: true: the request lists only the lazy columns that are visible, and showing one from the column menu reloads the same page with its data.
import { Datagrid, column, serverSide, yajra, colVis, lazyColumns } from "@akinozgen17/datagrids";
import { statusLabels, statusTones } from "./data.js";
new Datagrid("#mod-lazy-columns", {
label: "Field agents (lazy columns)",
columns: [
{ key: "ref", title: "Ref" },
{ key: "name", title: "Name" },
column.badge("status", { title: "Status", labels: statusLabels, tones: statusTones }),
// expensive on the server: computed only while visible
{ key: "task", title: "Task", lazy: true, visible: false },
{ key: "place", title: "Place", lazy: true, visible: false },
],
pageSize: 8,
use: [
serverSide({ url: "/api/agents", protocol: yajra(), csrf: true }),
colVis(),
lazyColumns(),
],
});<table id="mod-lazy-columns"></table>Show Task from the Columns menu, then open the Request tab: the body now carries lazy[]=task, and the grid stayed on the same page. (The demo server ignores the parameter and always sends every field; a real endpoint would compute the column only when asked.)
When to use it
Use it on server grids where a few columns are costly — a computed balance, an aggregate over a relation, a call to another service — and hidden by default. Skip it on client grids: every column is already loaded there (the module warns in the console).
Usage
import { Datagrid, column, serverSide, yajra, colVis, lazyColumns } from "@akinozgen17/datagrids";
new Datagrid("#agents", {
label: "Field agents",
columns: [
{ key: "name", title: "Name" },
column.money("balance", { title: "Balance", lazy: true, visible: false }),
],
use: [serverSide({ url: "/api/agents", protocol: yajra(), csrf: true }), colVis(), lazyColumns()],
});The endpoint computes only what is asked for:
$table = DataTables::eloquent($query);
if (in_array('balance', (array) $request->input('lazy', []))) {
$table->addColumn('balance', fn ($agent) => $agent->balance());
}
return $table->toJson();Mark columns by key instead of on the column, or rename the parameter:
lazyColumns({ keys: ["balance"], param: "with" });Options
| Option | Type | Default | Description |
|---|---|---|---|
keys | string[] | [] | Lazy column keys, in addition to columns with lazy: true. |
param | string | "lazy" | Request parameter that lists the visible lazy columns (their data path, else the key). |
can | boolean | true | Permission flag: false → no parameter, no reloads. |
API added to the grid
Available as grid.lazyColumns (typed: grid.module(lazyColumns)).
| Member | Returns | Description |
|---|---|---|
param | string (read-only) | The request parameter carrying the lazy keys ("lazy" or your param). |
requested() | string[] | Lazy columns sent with the last request. |
visible() | string[] | Lazy columns visible right now. |
Events
None of its own. Showing a lazy column that the last response did not include triggers grid.reload({ keepPage: true }) (beforeLoad / load).
Pipelines it plugs into
- Request middleware: adds
lazy=[…]with the visible lazy columns; an empty list is left out. ssp / yajra sendslazy[]=balance, the datagrids protocol puts it underparams. - Lifecycle: listens to
columnchange; hiding a lazy column costs no request, showing one reloads the current page.
Works with
- Requires a server data source (serverSide).
- Enhanced by colVis (users show and hide the columns) and stateSave (a lazy column the user keeps visible is requested from the first load on).
- The column itself still travels in
columns[]like every other column; only the extra parameter tells the server whether to fill it.
Accessibility
No UI of its own. Showing a lazy column reloads like any other reload: the table is aria-busy until the data arrives.
Replaces in DataTables
No direct equivalent in 1.10, 2.x or 3.x. It replaces a column-visibility.dt handler that called ajax.reload(null, false) together with an ajax.data function listing visible columns.
Gotchas
- The column stays empty after showing it. The endpoint ignores the parameter; read
lazy(or yourparam) on the server. - The console says the data source is not in server mode. The module does nothing on client grids; remove it there.
- The server receives
balance_totalinstead ofbalance. The parameter lists each column'sdatapath when it has one.