responsive
When the table no longer fits its frame, the least important columns collapse one by one, and every row gets an expand button that shows the collapsed values in a detail row.
import { Datagrid, column, responsive } from "@akinozgen17/datagrids";
import { agents, statusLabels, statusTones } from "./data.js";
const grid = new Datagrid("#mod-responsive", {
label: "Field agents (responsive)",
columns: [
column.text("name", { title: "Name" }),
column.text("ref", { title: "Ref" }),
column.text("unit", { title: "Unit" }),
column.text("task", { title: "Task" }),
column.text("place", { title: "Place" }),
column.badge("status", { title: "Status", labels: statusLabels, tones: statusTones }),
column.money("amount", { title: "Amount", currency: "USD", locale: "en-US" }),
column.date("date", { title: "Date", time: true }),
],
data: agents(40),
pageSize: 6,
use: [responsive({ priority: { name: 1, status: 2, amount: 3 } })],
});
const out = document.querySelector("#mod-responsive-out");
grid.on("collapse", ({ columns }) => (out.textContent = `collapsed: ${columns.join(", ") || "none"}`));<table id="mod-responsive"></table>
<div class="demo-output" id="mod-responsive-out" aria-live="polite">collapsed: none</div>When to use it
Use it for lists that must stay readable on phones and narrow panels without horizontal scrolling. Skip it when people compare many columns side by side — horizontal scrolling with a sticky first column is better there.
Usage
import { Datagrid, responsive } from "@akinozgen17/datagrids";
new Datagrid("#agents", {
label: "Field agents",
columns,
use: [responsive({ priority: { name: 1, status: 2, amount: 3 } })],
});Columns collapse from the highest priority number down; columns without a priority come after the listed ones, last column first. The first data column never collapses — it carries the expand button — and neither do sticky columns or the ones in keep:
responsive({ keep: ["status"] }); // never collapse Status
responsive({ details: false }); // just drop columns, no expand buttonsThe grid measures its frame with a ResizeObserver, after every render and after column changes. If your layout changes in a way the frame does not notice, call grid.responsive.refit().
Options
| Option | Type | Default | Description |
|---|---|---|---|
priority | Record<string, number> | definition order | Lower numbers are kept longer. |
keep | string[] | [] | Columns that never collapse (the first data column and sticky columns never do). |
details | boolean | true | Expand buttons and detail rows with the collapsed values. |
messages | { show, hide } | locale (en / tr) | Expand button names: "Show hidden columns" / "Hide hidden columns". |
can | boolean | true | Permission flag: false → nothing collapses. |
API added to the grid
Available as grid.responsive (typed: grid.module(responsive)).
| Member | Returns | Description |
|---|---|---|
collapsed() | string[] | Keys of the columns collapsed right now. |
refit() | void | Measures again. |
toggle(rowKey, open?) | void | Opens or closes a row's detail. |
Events
| Event | Payload | When |
|---|---|---|
collapse | { columns: string[] } | The set of collapsed columns changed. |
Pipelines it plugs into
- Render: after each render it marks collapsed cells (
data-dg-collapsed, hidden by the stylesheet), adds the expand button to the first data cell and re-creates open detail rows. - Header: header cells of collapsed columns are hidden the same way.
- Lifecycle: a
ResizeObserveron the frame; everything is removed ondestroy().
Works with
- With colVis: independent. colVis decides which columns exist on screen; responsive only collapses visible ones and never changes the user's choices.
- Export: collapsed columns are still visible columns, so exports include them.
- Virtual rows (
virtual: true): collapsing is switched off while rows are virtualized.
Accessibility
- The expand button is a native
<button>witharia-expanded,aria-controlsand a name ("Show hidden columns"); it is always visible, not on hover. - The detail row is a real table row holding a
<dl>: each collapsed column's title is a<dt>, its value a<dd>, so screen readers read "Place: Light Tower". - Collapsed cells are hidden, not removed — the table keeps its column semantics.
Replaces in DataTables
| DataTables (Responsive, 1.10 / 2.x / 3.x) | datagrids |
|---|---|
responsive: true | responsive() |
columns.responsivePriority | priority |
className: "all" | keep |
responsive.details: false | details: false |
responsive.recalc() | grid.responsive.refit() |
responsive-resize event | collapse event |
Gotchas
- Nothing collapses although the table overflows. The overflow happens outside the grid's frame — usually host CSS that turns
tableinto a scrolling block (display: block; overflow-x: auto). The grid measures its own frame. - A column I need on phones collapsed first. Give it a low
prioritynumber or list it inkeep. - The first column never collapses. It holds the expand button; reorder the columns if a different column should anchor the row.
Related
- colVis · Columns · Performance