Skip to content

stickyHeader ​

Keeps the column header in view while the page scrolls. It works where CSS position: sticky cannot: inside the grid's horizontal scroll frame.

js
import { Datagrid, column, stickyHeader } from "@akinozgen17/datagrids";
import { agents, statusLabels, statusTones } from "./data.js";

new Datagrid("#mod-sticky-header", {
  label: "Field agents (sticky header)",
  columns: [
    column.text("ref", { title: "Ref" }),
    column.text("name", { title: "Name" }),
    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" }),
  ],
  data: agents(80),
  pageSize: 25,
  // this site has a 64px fixed top bar
  use: [stickyHeader({ offset: 64 })],
});
html
<table id="mod-sticky-header"></table>

Scroll the page down past the demo's header: a copy of the header stays under the site's top bar and follows the table's horizontal scroll.

When to use it ​

Use it for long pages of wide rows (25+ rows per page, many columns) where people lose track of which column is which. For a grid with its own fixed height, use the core maxHeight option instead — its header already sticks inside the frame, and this module stays idle.

Usage ​

js
import { Datagrid, stickyHeader } from "@akinozgen17/datagrids";

new Datagrid("#agents", { label: "Field agents", columns, data: rows, use: [stickyHeader()] });

Below a fixed app bar, pass its height (a function is read on every frame):

js
stickyHeader({ offset: () => document.querySelector("#app-bar").offsetHeight });

Options ​

OptionTypeDefaultDescription
offsetnumber | () => number0Distance from the top of the viewport, in px.
canbooleantruePermission flag: false → no copied header.

API added to the grid ​

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

MemberReturnsDescription
stuck()booleantrue while the copied header is showing.
refresh()voidCopies and measures again (after a host layout change).

Events ​

None of its own. While the copy shows, the grid root carries data-dg-header-stuck.

Accessibility ​

The copied header is aria-hidden and its controls are out of the tab order: assistive technology and keyboard users keep using the real header, which is never removed. Pointer clicks on the copy are forwarded to the real header (sort, filter, select all). The copy is not printed.

Replaces in DataTables ​

DataTablesdatagrids
FixedHeader extension, fixedHeader: true (1.10 / 2.x / 3.x)stickyHeader()
fixedHeader: { headerOffset: 64 }offset: 64

Gotchas ​

  • The copy hides under my top bar. Set offset to the bar's height.
  • Nothing happens on a grid with maxHeight. The header already sticks inside that frame.
  • Column widths are off after my layout changed. Call grid.stickyHeader.refresh(); window resizes and table size changes are picked up automatically.

Released under the MIT License.