Grids in tabs & modals
Goal: a page with several tabs or a dialog that contains a grid. Hidden grids must not fire requests on page load, and when they appear their columns must line up — without "redraw" or "adjust" calls in your tab code.
import { Datagrid, serverSide, yajra } from "@akinozgen17/datagrids";
const $ = (sel) => document.querySelector(sel);
const log = (line) => ($("#rcp-tabs-log").textContent += `\n${line}`);
const columns = [
{ key: "ref", title: "Ref" },
{ key: "name", title: "Name" },
{ key: "unit", title: "Unit" },
{ key: "status_html", title: "Status", sortable: false, searchable: false },
];
// In a tab: no request until the panel is shown
const inTab = new Datagrid("#rcp-tabs", {
label: "Agents (tab)",
columns,
pageSize: 5,
load: "visible",
use: [serverSide({ url: "/api/agents", protocol: yajra(), csrf: true })],
});
// In a dialog: you decide when the first request goes out
const inDialog = new Datagrid("#rcp-tabs-dialog-grid", {
labelledBy: "rcp-tabs-dialog-title",
columns,
pageSize: 5,
load: "manual",
use: [serverSide({ url: "/api/agents", protocol: yajra(), csrf: true, params: { status: "pending" } })],
});
inTab.on("load", () => log("tab grid: first page loaded"));
inDialog.on("load", () => log("dialog grid: first page loaded"));
// plain tabs: toggle `hidden` — the grid notices on its own
for (const tab of document.querySelectorAll("#rcp-tabs-list [role=tab]")) {
tab.addEventListener("click", () => {
for (const t of document.querySelectorAll("#rcp-tabs-list [role=tab]")) {
t.setAttribute("aria-selected", String(t === tab));
$(`#${t.getAttribute("aria-controls")}`).hidden = t !== tab;
}
});
}
$("#rcp-tabs-open").addEventListener("click", () => {
$("#rcp-tabs-dialog").showModal();
if (!inDialog.status().loaded) inDialog.load();
});<style>
#rcp-tabs-list [aria-selected="true"] { border-color: var(--vp-c-brand-1); color: var(--vp-c-brand-1); }
#rcp-tabs-dialog { inline-size: min(720px, 92vw); padding: 20px; border: 1px solid var(--vp-c-divider); border-radius: 12px; background: var(--vp-c-bg); color: var(--vp-c-text-1); }
#rcp-tabs-dialog::backdrop { background: rgb(0 0 0 / 0.4); }
</style>
<div class="demo-buttons" id="rcp-tabs-list" role="tablist" aria-label="Agent views">
<button type="button" role="tab" aria-selected="true" aria-controls="rcp-tabs-p1">Overview</button>
<button type="button" role="tab" aria-selected="false" aria-controls="rcp-tabs-p2">Agents</button>
<button type="button" id="rcp-tabs-open">Open dialog…</button>
</div>
<div id="rcp-tabs-p1" role="tabpanel">
<div class="demo-output">The agents grid lives in the second tab. Check the Request tab: nothing has been sent yet.</div>
</div>
<div id="rcp-tabs-p2" role="tabpanel" hidden>
<table id="rcp-tabs"></table>
</div>
<dialog id="rcp-tabs-dialog" aria-labelledby="rcp-tabs-dialog-title">
<strong id="rcp-tabs-dialog-title">Pending agents</strong>
<table id="rcp-tabs-dialog-grid"></table>
<form method="dialog" class="demo-buttons"><button>Close</button></form>
</dialog>
<pre class="demo-output" id="rcp-tabs-log" aria-live="polite">Log:</pre>Open the Request tab first: nothing was sent. Switch to Agents: the first request goes out the moment the panel becomes visible. Open dialog… loads the second grid on demand.
Steps
1. A grid in a tab: load: "visible"
new Datagrid("#agents", {
label: "Agents",
columns,
load: "visible",
use: [serverSide({ url: "/agents/data", protocol: yajra(), csrf: true })],
});The grid watches itself with an IntersectionObserver and sends its first request when it comes within 200 px of the viewport — when its tab is shown, its accordion opens or the user scrolls down to it. Your tab code only toggles visibility (hidden, a CSS class, Bootstrap's tab plugin…); it never talks to the grid. After the first load the grid behaves normally: reloads, filters and paging go out immediately.
load: "visible" also pays off for grids far below the fold on long pages.
2. A grid in a dialog: load: "manual"
const pending = new Datagrid("#pending", {
labelledBy: "pending-title",
columns,
load: "manual",
use: [serverSide({ url: "/agents/data", protocol: yajra(), csrf: true, params: { status: "pending" } })],
});
openButton.addEventListener("click", () => {
dialog.showModal();
if (!pending.status().loaded) pending.load();
});With load: "manual" the grid renders its frame in the idle state and waits for grid.load(). Choose it when you know the moment better than visibility does — typically when the request depends on what the user clicked:
let agentId = null;
const history = new Datagrid("#history", {
label: "Assignment history",
columns,
load: "manual",
use: [serverSide({ url: "/agents/history", protocol: yajra(), csrf: true, params: () => ({ agent_id: agentId }) })],
});
function openHistory(id) {
agentId = id;
dialog.showModal();
history.reload(); // first call loads; later calls refresh page 1 for the new record
}load: "visible" works in a dialog too (it becomes visible when opened); it just can't wait for data you set while opening.
3. Nothing to do when it appears
Column widths come from CSS, not from measurements taken while the table was hidden, so a grid that was display: none lays out correctly the moment it is shown. A ResizeObserver keeps the rest in step with the container: the narrow layout of the pager and the horizontal-scroll shadows. Resizing the dialog or the window needs no call either.
Notes
- Narrow dialogs: add responsive to collapse the least important columns into an expandable detail row instead of scrolling sideways.
- Name the grid after the dialog title with
labelledBy, as the demo does, so screen readers hear one consistent name. - Keep the grid when the dialog closes. Closing only hides it; the next open shows the same state instantly. Destroy it only if your page throws the dialog's markup away — see Destroy, re-init & Livewire.
- Reload a grid in another tab after an action with
reload("agents")— by gridname, no reference needed. - The demo's dialog grid is in the idle state (
data-state="idle") until opened; style or test against that attribute rather than against timing.
Related
- Lifecycle —
load,reload,destroy - States & errors
- Destroy, re-init & Livewire