Data grid
A data grid with sorting, search, typed filters, pinning, resizing, reordering, grouping, editing and export.
Example
<div class="full"> <focsd-data-grid id="grid-demo" label="Invoices" page-size="8" selection="multiple" quick-search toolbar></focsd-data-grid></div>
<script type="module">const grid = document.getElementById('grid-demo');grid.columns = [ { id: 'id', header: 'Invoice', type: 'number', pinned: 'start', width: 112, formatOptions: { useGrouping: false } }, { id: 'customer', header: 'Customer', editable: true, required: true }, { id: 'owner', header: 'Owner', display: 'avatar' }, { id: 'status', header: 'Status', type: 'enum', editable: true, options: [ { value: 'paid', label: 'Paid', tone: 'positive', icon: 'check' }, { value: 'sent', label: 'Sent', tone: 'info' }, { value: 'overdue', label: 'Overdue', tone: 'critical', icon: 'alert-circle' }, { value: 'draft', label: 'Draft' }, ] }, { id: 'region', header: 'Region', type: 'enum', editable: true, options: [{ value: 'eu', label: 'Europe' }, { value: 'us', label: 'Americas' }, { value: 'ap', label: 'Asia Pacific' }] }, { id: 'amount', header: 'Amount', type: 'number', aggregate: 'sum', editable: true, min: 0, formatOptions: { style: 'currency', currency: 'EUR' } }, { id: 'due', header: 'Due', type: 'date', aggregate: 'max', editable: true },];const customers = ['Northwind', 'Contoso', 'Fabrikam', 'Tailspin', 'Wingtip', 'Adatum'];const owners = ['Amara Okafor', 'Lukas Brandt', 'Priya Raman', 'Tomás Herrera', 'Mei Lin'];grid.rows = Array.from({ length: 40 }, (_, i) => ({ id: 1001 + i, customer: customers[i % customers.length], region: ['eu', 'us', 'ap'][i % 3], amount: ((i * 7919) % 9000) + 120, due: '2026-' + String((i % 12) + 1).padStart(2, '0') + '-' + String((i % 27) + 1).padStart(2, '0'), owner: owners[i % owners.length], status: ['paid', 'sent', 'overdue', 'draft', 'paid'][i % 5],}));</script>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/data-grid';</script>
<focsd-data-grid label="Invoices" page-size="8" selection="multiple" quick-search toolbar></focsd-data-grid>import { useEffect, useRef } from 'react';import '@focsd-dev/data-grid';
export function Example() { const ref = useRef(null); return <focsd-data-grid ref={ref} label="Invoices" page-size="8" selection="multiple" quick-search toolbar></focsd-data-grid>;}<script setup>import '@focsd-dev/data-grid';</script>
<template> <focsd-data-grid label="Invoices" page-size="8" selection="multiple" quick-search toolbar></focsd-data-grid></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/data-grid';
// template<focsd-data-grid label="Invoices" page-size="8" selection="multiple" quick-search toolbar></focsd-data-grid><script> import '@focsd-dev/data-grid';</script>
<focsd-data-grid label="Invoices" page-size="8" selection="multiple" quick-search toolbar></focsd-data-grid>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
label | label | string | — |
page-size | pageSize | number | 25 |
selection | selection | 'none' | 'single' | 'multiple' | none |
density | density | GridDensity | comfortable |
locale | locale | string | — |
external | external | boolean | false |
row-count | rowCount | number | 0 |
loading | loading | boolean | false |
quick-search | quickSearch | boolean | false |
toolbar | toolbar | boolean | false |
JavaScript properties
| Property | Type | Description |
|---|---|---|
columns | { id, field, header, type, display, width, minWidth, maxWidth, pinned, hidden, sortable, filterable, resizable, editable, groupable, searchable, aggregate, formatOptions, options, labels, render, validate, required, min, max, valueGetter, valueSetter }[] | Column definitions. type is text, number, date, boolean or enum (with options), or a type added with registerType(). An enum option with tone, color or icon shows its values as status chips; display: 'avatar' shows names with an avatar. |
rows | object[] | Row objects. Edits replace rows immutably, so keep your own copy in sync from focsd-cell-edit. |
rowId | string | (row) => id | Field or function giving a stable row id (default id). Needed for selection and editing. |
selectedIds | unknown[] (read-only) | Ids of the selected rows. |
Methods
| Method | Description |
|---|---|
getState() | The view as JSON: column order, visibility, widths and pinning; sort; filters; search; grouping and expanded groups; page and page size; density. |
setState(state) | Restore a saved view. Unknown columns and invalid filters are ignored. |
exportCsv({ selectedOnly, raw }) | CSV of the filtered, sorted, visible view (or the selection), with formula-injection protection. |
downloadCsv(filename, options) | Download that CSV (options as for exportCsv). The file name defaults to the label. |
setGroupBy(columnIds) | Group rows by these columns, outermost first. |
expandAll() / collapseAll() | Open or close every group. |
startEditing(rowId, columnId) | Move to a cell and start editing it. |
scrollToRow(rowId) | Bring a row into view (changing page if needed) and focus it in the active column. |
CSS parts
Style the inside of the component with ::part(), for example focsd-data-grid::part(grid).
| Part | Description |
|---|---|
grid | The scrolling grid element. |
row | A data row. |
cell | A data cell. |
pagination-page | Pagination parts, re-exported with a `pagination-` prefix (pagination-nav, pagination-list, pagination-page, pagination-previous, pagination-next, pagination-gap). |
header-row | The header row. |
header-cell | A column header. |
footer | The footer with paging and counts. |
Accessibility
- APG grid (or treegrid when grouped) with
aria-rowcount,aria-colcount,aria-rowindexandaria-colindexthat stay correct under paging, windowing, pinning and hidden columns. - One tab stop for all cells. Pinned columns are sticky cells inside each row, so every row is still one row to assistive technology.
- Sorting, filtering, grouping, selection, edits and column changes are announced in a polite live region ("Sorted by Amount, descending", "Filter applied: Region is Europe. 14 of 40 rows").
- Every pointer gesture has a keyboard path: the column menu resizes (Widen, Narrow, Autosize), pins and hides; the Columns panel shows, hides and reorders.
- Editors are labeled ("Edit Amount"); invalid input keeps the editor open with an error that is described and announced.
Keyboard
| Key | Action |
|---|---|
| Arrow keys | Move between cells |
| Home / End | First / last cell in the row |
| Ctrl + Home / End | First / last cell in the grid |
| Page Up / Down | Previous / next page, or a screen of rows when not paginated |
| Enter (header) | Sort; Shift + Enter adds to the sort |
| Alt + Arrow Down / Shift + F10 / Menu (header) | Open the column menu |
| Space | Select the row; Shift + Space selects a range |
| Ctrl / ⌘ + A | Select all rows (selection="multiple") |
| Enter / F2 / typing | Edit the cell (Enter saves and moves down, Tab saves and moves sideways, Escape cancels) |
| Enter (read-only cell) | Fire focsd-row-activate |
| Enter / Space / Arrow Right / Arrow Left (group row) | Toggle, expand or collapse the group (arrows in the first column) |
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.