Skip to content

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.

js
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"}`));
html
<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 ​

js
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:

js
responsive({ keep: ["status"] });   // never collapse Status
responsive({ details: false });     // just drop columns, no expand buttons

The 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 ​

OptionTypeDefaultDescription
priorityRecord<string, number>definition orderLower numbers are kept longer.
keepstring[][]Columns that never collapse (the first data column and sticky columns never do).
detailsbooleantrueExpand buttons and detail rows with the collapsed values.
messages{ show, hide }locale (en / tr)Expand button names: "Show hidden columns" / "Hide hidden columns".
canbooleantruePermission flag: false → nothing collapses.

API added to the grid ​

Available as grid.responsive (typed: grid.module(responsive)).

MemberReturnsDescription
collapsed()string[]Keys of the columns collapsed right now.
refit()voidMeasures again.
toggle(rowKey, open?)voidOpens or closes a row's detail.

Events ​

EventPayloadWhen
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 ResizeObserver on the frame; everything is removed on destroy().

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> with aria-expanded, aria-controls and 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: trueresponsive()
columns.responsivePrioritypriority
className: "all"keep
responsive.details: falsedetails: false
responsive.recalc()grid.responsive.refit()
responsive-resize eventcollapse event

Gotchas ​

  • Nothing collapses although the table overflows. The overflow happens outside the grid's frame — usually host CSS that turns table into 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 priority number or list it in keep.
  • The first column never collapses. It holds the expand button; reorder the columns if a different column should anchor the row.

Released under the MIT License.