Skip to content

keyboardGrid ​

Turns the table into one tab stop with arrow-key navigation between cells, following the WAI-ARIA APG grid pattern — on the same native <table>.

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

const grid = new Datagrid("#mod-keyboard-grid", {
  label: "Field agents (keyboard grid)",
  columns: [
    column.link("name", { title: "Name", href: "#agent-{id}" }),
    column.text("unit", { title: "Unit" }),
    column.badge("status", { title: "Status", labels: statusLabels, tones: statusTones }),
    column.money("amount", { title: "Amount", currency: "USD", locale: "en-US" }),
  ],
  data: agents(40),
  pageSize: 6,
  use: [keyboardGrid()],
});

const out = document.querySelector("#mod-keyboard-grid-out");
grid.table.addEventListener("focusin", () => {
  const { row, col } = grid.keyboardGrid.position();
  out.textContent = `position() → row ${row}, col ${col}`;
});
html
<table id="mod-keyboard-grid"></table>
<div class="demo-output" id="mod-keyboard-grid-out" aria-live="polite">Tab into the table, then use the arrow keys, Home / End and PageUp / PageDown.</div>

When to use it ​

Use it for grids people work through with the keyboard all day: many rows, links or buttons in every row, where tabbing through each control is slow. Without it the table is a regular table: every link and button is its own tab stop, which is the better default for short lists and content pages.

Usage ​

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

new Datagrid("#agents", { label: "Field agents", columns, data: rows, use: [keyboardGrid()] });
KeyMoves to
← → ↑ ↓The neighbouring cell (left / right swap in right-to-left layouts); ↑ from the first body row reaches the header
Home / EndFirst / last cell of the row
Ctrl + Home / EndFirst cell of the header / last cell of the last row
PageUp / PageDownPrevious / next page (focus lands on the first row)
Enter / F2Into a cell that holds several controls; Esc goes back to the cell

A cell with exactly one control (a sort button, a checkbox, a link) passes focus straight to it. Clicking a cell makes it the active cell.

Options ​

OptionTypeDefaultDescription
canbooleantruePermission flag: false → a regular table.

API added to the grid ​

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

MemberReturnsDescription
position(){ row, col }The active cell. Row 0 is the header row; columns count visible cells.
focus(row, col)voidMoves the active cell and focuses it.

Events ​

None of its own. PageUp / PageDown go through the core beforePage / page events.

Accessibility ​

  • The table gets role="grid"; on destroy() the original role comes back.
  • Exactly one element in the table has tabindex="0" (the active cell or its single control); everything else is -1, so Tab enters and leaves the grid in one step.
  • Typing in a text field inside a cell is never hijacked: arrow keys only navigate outside inputs.
  • Collapsed responsive columns and detail rows are skipped.

Replaces in DataTables ​

DataTablesdatagrids
KeyTable extension, keys: true (1.10 / 2.x / 3.x)keyboardGrid()
key-focus eventFollow focusin on grid.table and read grid.keyboardGrid.position()

Gotchas ​

  • The arrow keys do nothing. Focus is in a text field inside the cell; press Esc (back to the cell) or Tab out.
  • PageDown scrolls the page instead of paging. Focus is outside the table.
  • With virtual: true the arrows stop at the rendered rows. The module warns about it in the console; use paging for keyboard-heavy grids.

Released under the MIT License.