Skip to main content

Vanilla JS API

Complete reference for @alaarab/ogrid-js — the framework-free data grid.

OGrid Class

import { OGrid } from '@alaarab/ogrid-js';
import '@alaarab/ogrid-js/styles'; // optional default theme

const grid = new OGrid(container, options);

Constructor

ParameterTypeDescription
containerHTMLElementDOM element to render into. OGrid fills this container.
optionsOGridOptions<T>Configuration object (see below).

Instance Methods

MethodDescription
grid.apiThe imperative API object (see API Methods below).
grid.on(event, handler)Subscribe to a grid event. Returns void.
grid.off(event, handler)Unsubscribe from a grid event.
grid.destroy()Tear down the grid — removes DOM, listeners, and ResizeObservers.

OGridOptions<T>

All configuration is passed to the constructor.

Data

OptionTypeDefaultDescription
columns(IColumnDef<T> | IColumnGroupDef<T>)[]requiredColumn definitions.
getRowId(item: T) => RowIdrequiredReturns a unique string | number per row.
dataT[]Client-side data. Mutually exclusive with dataSource.
dataSourceIDataSource<T>Server-side data source. Mutually exclusive with data.

Pagination & Sorting

OptionTypeDefaultDescription
pagenumber1Initial page (1-based).
pageSizenumber20Rows per page.
sort{ field: string; direction: 'asc' | 'desc' }Initial sort state.
filtersIFiltersInitial filter state.

Interaction

OptionTypeDefaultDescription
editablebooleanfalseEnable inline cell editing.
cellSelectionbooleanfalseEnable spreadsheet-style cell/range selection.
rowSelection'single' | 'multiple'Row checkbox selection mode.
selectedRowsSet<RowId>Controlled selected row IDs.
pinnedColumnsRecord<string, 'left' | 'right'>Pin columns to left/right edges.
visibleColumnsSet<string>allInitially visible column IDs.

Layout

OptionTypeDefaultDescription
layoutMode'fill' | 'content''fill''fill' stretches to container; 'content' sizes to data.
suppressHorizontalScrollbooleanfalsePrevent horizontal scrollbar.
sideBarboolean | ISideBarDefShow sidebar with columns/filters panels.
emptyMessagestring'No data'Custom empty state text.
aria-labelstringAccessible label for the grid.

Callbacks

OptionTypeDescription
onCellValueChanged(event: ICellValueChangedEvent<T>) => voidFired when a cell edit is committed.
onSelectionChange(event: IRowSelectionChangeEvent<T>) => voidFired when row selection changes.
onError(error: unknown) => voidFired on server-side data source failures.
onFirstDataRendered() => voidFired once when the first data render completes.

API Methods

Access via grid.api:

Data

MethodSignatureDescription
setRowData(data: T[]) => voidReplace the entire dataset.
getDisplayedRows() => T[]Get the currently visible (filtered, sorted, paginated) rows.
getRowNode(id: RowId) => T | undefinedFind a row by its ID.
refreshData() => voidRe-fetch from dataSource (server-side only).

Sorting & Filtering

MethodSignatureDescription
setSort(field: string, direction: 'asc' | 'desc') => voidSet sort programmatically.
setFilter(key: string, value: FilterValue | undefined) => voidSet or clear a filter.

Columns

MethodSignatureDescription
getColumnState() => IGridColumnState[]Get column widths, visibility, and order.
setColumnVisible(columnId: string, visible: boolean) => voidShow/hide a column.

Selection

MethodSignatureDescription
selectAll() => voidSelect all rows (requires rowSelection: 'multiple').
deselectAll() => voidDeselect all rows.
getSelectedRows() => T[]Get the currently selected row data.

Export

MethodSignatureDescription
exportToCsv(filename?: string) => voidExport displayed rows to CSV and trigger a browser download.

Pagination

MethodSignatureDescription
setPage(page: number) => voidNavigate to a specific page.
setPageSize(size: number) => voidChange rows per page.

Events

Subscribe with grid.on(eventName, handler).

EventPayload TypeFired When
cellValueChangedICellValueChangedEvent<T>A cell edit is committed
selectionChangeIRowSelectionChangeEvent<T>Row selection changes
sortChange{ field: string; direction: 'asc' | 'desc' }Sort state changes
filterChange{ filters: IFilters }Filter state changes
pageChange{ page: number }Current page changes

ICellValueChangedEvent<T>

{
item: T; // The row data object
columnId: string; // Which column was edited
oldValue: unknown; // Previous cell value
newValue: unknown; // New cell value
}

IColumnDef<T> (JS-specific)

The JS package extends the core IColumnDef<T> with DOM-aware fields:

FieldTypeDescription
renderCell(cell: HTMLTableCellElement, item: T, value: unknown) => voidCustom cell renderer — mutate the <td> DOM directly.
cellStylePartial<CSSStyleDeclaration>Inline styles applied to each cell.
cellEditorstring | (container: HTMLElement, params: object) => { getValue: () => unknown; destroy: () => void }Built-in editor name ('text', 'select', 'richSelect', 'checkbox', 'date') or a factory function for custom editors.

All other IColumnDef fields (columnId, name, sortable, filterable, type, editable, valueFormatter, valueGetter, valueParser, minWidth, defaultWidth, idealWidth, pinned, required, defaultVisible) are identical to the React version — see Column Definitions.


CSS Classes

The JS package renders semantic HTML with ogrid-* class names. Import the default theme or write your own CSS targeting these selectors:

Layout

ogrid-container, ogrid-toolbar, ogrid-body-area, ogrid-table-container, ogrid-sidebar-container, ogrid-status-bar-container, ogrid-pagination-container

Table

ogrid-wrapper, ogrid-table, ogrid-header-cell, ogrid-group-header, ogrid-sortable, ogrid-sort-indicator, ogrid-resize-handle, ogrid-filter-icon, ogrid-row, ogrid-cell, ogrid-checkbox-header, ogrid-checkbox-cell, ogrid-fill-handle, ogrid-empty-state

Data Attributes

[data-active-cell], [data-in-range], [data-drag-range], [data-row-selected], [data-pinned="left|right"], [data-column-id], [data-row-index], [data-col-index]

CSS Variables

Override --ogrid-bg, --ogrid-fg, --ogrid-border, --ogrid-primary, --ogrid-selection, --ogrid-bg-subtle, --ogrid-bg-hover, --ogrid-muted, and more. See Theming.