# Rows & row IDs

> Pass rows as plain objects, give them stable IDs with getRowId, and understand the display rows the grid renders.

Rows are plain JavaScript objects passed as the `rows` option. The grid never clones or wraps them: the objects you pass are the objects you get back from `getRows()`, `getSelectedRows()` and every event payload. Type them once with the grid's generic parameter and the whole API follows.

```ts title="technicians-grid.ts"
import { createGrid, type ColumnDef } from '@birchui/core';

interface Technician {
  employeeId: string;
  name: string;
  region: string;
  skills: string[];
  openTickets: number;
  certifiedUntil: string;
}

const technicians: Technician[] = [
  { employeeId: 'T-0142', name: 'Amira Haddad', region: 'North', skills: ['HVAC', 'Electrical'], openTickets: 3, certifiedUntil: '2027-01-31' },
  { employeeId: 'T-0187', name: 'Lukas Brandt', region: 'Central', skills: ['Plumbing'], openTickets: 7, certifiedUntil: '2026-09-30' },
  { employeeId: 'T-0203', name: 'Priya Nair', region: 'South', skills: ['Electrical', 'Solar'], openTickets: 1, certifiedUntil: '2027-04-15' },
];

const columns: ColumnDef<Technician>[] = [
  { field: 'employeeId', header: 'ID', width: 100 },
  { field: 'name', flex: 1 },
  { field: 'region' },
  { id: 'skills', header: 'Skills', valueGetter: (t) => t.skills.join(', ') },
  { field: 'openTickets', header: 'Open tickets', type: 'number' },
  { field: 'certifiedUntil', header: 'Certified until', type: 'date' },
];

const grid = createGrid<Technician>(document.querySelector('#technicians')!, {
  columns,
  rows: technicians,
  getRowId: (t) => t.employeeId,
});
```

## Row IDs

Every row has a string ID. IDs drive selection, expansion state, transactions, `getRowById()` and focus restoration after data changes.

### Provide getRowId whenever rows have a key

`getRowId(row, index)` returns the ID for a row. Use your primary key:

```ts
createGrid<Order>(container, {
  columns,
  rows: orders,
  getRowId: (order) => order.id,
});
```

With `getRowId`, a row keeps its identity even when you replace the object, for example after re-fetching from the server. Selection survives a refresh and `applyTransaction({ update })` can match fresh objects to existing rows.

### Without getRowId

If you omit `getRowId`, the grid assigns IDs such as `r1`, `r2` and remembers them per object instance. Passing the same objects again keeps their IDs. Passing new objects, even with identical contents, creates new rows with new IDs, which clears their selection and expansion state.

> **Duplicate IDs**
>
> IDs must be unique. If `getRowId` returns the same value for two rows, the grid keeps both rows but suffixes the second ID with `#<index>`, so `getRowById()` finds only the first. Fix the key rather than relying on the suffix.

### Looking rows up

```ts
const id = grid.getRowId(technicians[0]);
const lukas = grid.getRowById('T-0187');
const all = grid.getRows();
```

**`GridApi`** (interface from `@birchui/core`, full reference: https://docs.birchui.com/api/core.md)

```ts
interface GridApi<TRow = RowData> extends Widget<GridOptions<TRow>, GridEvents<TRow>>, GridFeatureApi<TRow>
```

| Property | Type | Description |
| --- | --- | --- |
| `getRows` | `() => readonly TRow[]` |  |
| `getRowId` | `(row: TRow) => string` |  |
| `getRowById` | `(id: string) => TRow \| undefined` |  |
| `getDisplayRows` | `() => readonly DisplayRow<TRow>[]` |  |
| `getAllDisplayRows` | `() => readonly DisplayRow<TRow>[]` |  |
| `getRenderedRows` | `() => readonly DisplayRow<TRow>[]` |  |
| `getDisplayRow` | `(rowIndex: number) => DisplayRow<TRow> \| undefined` |  |

## Source rows and display rows

The grid keeps two lists:

- **Source rows** are your objects, in the order you passed them. `getRows()` returns them.
- **Display rows** are what the grid renders after filtering, sorting, pagination and any module stages such as grouping. `getDisplayRows()` returns them, and every `rowIndex` in the API and in events is an index into this list.
- **All display rows** are the same list before pagination slices it into pages. `getAllDisplayRows()` returns them. Without pagination the two lists are identical; with pagination, `getDisplayRows()` holds only the current page.

```ts
const onThisPage = grid.getDisplayRows().length;
const matchingFilters = grid.getAllDisplayRows().filter((row) => row.kind === 'data').length;
pageInfo.textContent = `${onThisPage} shown, ${matchingFilters} technicians match`;
```

Each display row is a `DisplayRow` that wraps your data with rendering state:

```ts
const first = grid.getDisplayRow(0);
if (first?.kind === 'data') {
  console.info(first.id, first.data?.name, first.sourceIndex);
}
```

| Kind | Produced by | `data` |
| --- | --- | --- |
| `data` | Every source row that passes the filters | The source row |
| `group` | Row grouping and tree data (Grid Pro) | `undefined`, see `group` and `aggregates` |
| `detail` | Master/detail (Grid Pro) | The master row |
| `total` | Group totals and pivot totals (Grid Pro) | `undefined`, see `aggregates` |

**`DisplayRow`** (interface from `@birchui/core`, full reference: https://docs.birchui.com/api/core.md)

```ts
interface DisplayRow<TRow = RowData>
```

| Property | Type | Description |
| --- | --- | --- |
| `kind` | `DisplayRowKind` |  |
| `id` | `string` |  |
| `data` | `TRow \| undefined` |  |
| `depth` | `number` |  |
| `sourceIndex` | `number` |  |
| `expandable` | `boolean` |  |
| `expanded` | `boolean` |  |
| `parentId?` | `string` |  |
| `group?` | `DisplayRowGroup<TRow>` |  |
| `aggregates?` | `Record<string, unknown>` |  |
| `fullWidth?` | `boolean` |  |
| `height?` | `number` |  |

`getRenderedRows()` returns only the display rows that currently have DOM elements, which is the visible window plus the overscan. See [Virtualization & performance](https://docs.birchui.com/grid/virtualization.md).

## Converting between indexes and rows

Events report both the row and its `rowIndex`. To go the other way:

```ts
grid.on('cellClick', ({ row, rowIndex, colId }) => {
  const displayRow = grid.getDisplayRow(rowIndex);
  console.info(colId, row?.name, displayRow?.id);
});

const index = grid.getDisplayRows().findIndex((r) => r.id === 'T-0203');
if (index >= 0) grid.scrollToRow(index, 'center');
```

## Row classes and stripes

`rowClass(displayRow)` returns classes for a row element. It runs for every rendered row, so keep it cheap.

```ts
createGrid<Technician>(container, {
  columns,
  rows: technicians,
  getRowId: (t) => t.employeeId,
  stripes: true,
  rowClass: (row) => (row.data && row.data.openTickets > 5 ? 'row-overloaded' : undefined),
});
```

```css
.lq-grid .lq-row.row-overloaded {
  background: #fff8e6;
}
```

Every row element also carries built-in classes you can target: `lq-row`, `lq-row-even` or `lq-row-odd`, `lq-row-data`, `lq-row-group`, `lq-row-detail` or `lq-row-total` by kind, `lq-row-selected` and `lq-row-focused`.

`stripes: true` shades odd rows with `--lq-row-alt`. Because the grid's own row rules use two classes, give your overrides at least three (as above) so they win regardless of stylesheet order. See [Theming](https://docs.birchui.com/grid/theming.md#overriding-styles).

## Row height

`rowHeight` is a number for uniform rows or a function of the display row for variable heights. Without it, the height follows `density`: 28 pixels for `compact`, 34 for `normal` and 42 for `comfortable`.

```ts
createGrid<Technician>(container, {
  columns,
  rows: technicians,
  rowHeight: (row) => (row.data && row.data.skills.length > 2 ? 56 : 36),
});
```

Uniform heights are the fastest path. Variable heights are supported at any scale; the grid measures offsets once per data change, not per scroll. If the function throws or returns something that is not a positive number, that row falls back to the default height and a thrown error is reported with `console.error`. See [Virtualization & performance](https://docs.birchui.com/grid/virtualization.md#variable-row-heights).

## Empty grids

When no display rows remain, either because `rows` is empty or because filters removed everything, the grid shows `emptyMessage` in the body. The default is "No rows to show".

```ts
createGrid(container, {
  columns,
  rows: [],
  emptyMessage: 'No technicians match these filters',
});
```

## Related

- [Updating data](https://docs.birchui.com/grid/updating-data.md) covers `setRows`, `applyTransaction` and `update()`.
- [Selection](https://docs.birchui.com/grid/selection.md) relies on stable row IDs.
- [Tree data](https://docs.birchui.com/grid-pro/tree-data.md) and [Row grouping](https://docs.birchui.com/grid-pro/row-grouping.md) add `group` rows.
