# Grid Pro in an Angular standalone app

> Use the birchui-grid component in a standalone, zoneless Angular app with signal inputs, a computed options object, outputs and Grid Pro modules.

This recipe builds a sales order screen in Angular with standalone components and signals: grouping with totals, range selection, clipboard, a status bar and Excel export. The `examples/angular-grid-pro` project in the Birch UI repository is the complete version.

## 1. Install

```sh
npm install @birchui/core @birchui/angular @birchui/grid-pro @birchui/license
```

## 2. Set the license key at startup

```ts title="src/main.ts"
import { bootstrapApplication } from '@angular/platform-browser';
import { setLicenseKey } from '@birchui/license';
import { App } from './app/app';
import { appConfig } from './app/app.config';
import { environment } from './environments/environment';

setLicenseKey(environment.birchLicenseKey);

bootstrapApplication(App, appConfig).catch((error: unknown) => console.error(error));
```

Without a key every feature works; on hosts other than `localhost` a small watermark appears. You can also pass a key to one grid with the `[licenseKey]` input.

## 3. The component

```ts title="src/app/app.ts"
import { ChangeDetectionStrategy, Component, computed, signal, viewChild } from '@angular/core';
import { BirchGridComponent } from '@birchui/angular';
import { AllProModules } from '@birchui/grid-pro';
import type { CellValueChangedEvent, ColumnDef, GridOptions } from '@birchui/core';
import { createOrders, type Order } from './orders';

const usd = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });

type Layout = 'region' | 'category' | 'flat';

const GROUPINGS: Record<Layout, string[]> = {
  region: ['region', 'country'],
  category: ['category', 'product'],
  flat: [],
};

@Component({
  selector: 'app-root',
  imports: [BirchGridComponent],
  changeDetection: ChangeDetectionStrategy.OnPush,
  templateUrl: './app.html',
})
export class App {
  private readonly grid = viewChild.required<BirchGridComponent<Order>>(BirchGridComponent);

  readonly orders = signal<Order[]>(createOrders(5000));
  readonly search = signal('');
  readonly layout = signal<Layout>('region');
  readonly lastEdit = signal('');

  readonly columns: ColumnDef<Order>[] = [
    { field: 'id', header: 'Order', width: 110, pinned: 'left' },
    { field: 'customer', header: 'Customer', flex: 1, minWidth: 180, filter: 'text', editable: true },
    { field: 'units', header: 'Units', type: 'number', aggFunc: 'sum', editable: true },
    { field: 'total', header: 'Total', type: 'number', aggFunc: 'sum', format: (value) => usd.format(Number(value)) },
    { field: 'region', header: 'Region', filter: 'set' },
    { field: 'country', header: 'Country', filter: 'set' },
    { field: 'category', header: 'Category', filter: 'set' },
    { field: 'product', header: 'Product', minWidth: 200 },
  ];

  readonly options = computed<Partial<GridOptions<Order>>>(() => ({
    modules: [...AllProModules],
    getRowId: (order) => order.id,
    groupBy: GROUPINGS[this.layout()],
    groupDefaultExpanded: 1,
    groupTotals: true,
    rangeSelection: true,
    clipboard: true,
    statusBar: true,
    undoRedo: true,
    theme: 'auto',
  }));

  setLayout(value: string): void {
    this.layout.set(value as Layout);
  }

  onEdit({ row, colId, newValue }: CellValueChangedEvent<Order>): void {
    this.lastEdit.set(`${row.id}: ${colId} set to ${String(newValue)}`);
  }

  exportExcel(): void {
    void this.grid().api?.exportExcel?.({ fileName: 'orders', sheetName: 'Orders' });
  }
}
```

```html title="src/app/app.html"
<div class="toolbar">
  <input type="search" aria-label="Search orders" (input)="search.set($any($event.target).value)" />
  <select aria-label="Group by" (change)="setLayout($any($event.target).value)">
    <option value="region">Group by region and country</option>
    <option value="category">Group by category and product</option>
    <option value="flat">No grouping</option>
  </select>
  <button type="button" (click)="exportExcel()">Export to Excel</button>
  <span>{{ lastEdit() }}</span>
</div>

<birchui-grid
  class="orders"
  [columns]="columns"
  [rows]="orders()"
  [quickFilter]="search()"
  [options]="options()"
  (cellValueChanged)="onEdit($event)"
/>
```

```css title="src/styles.css"
.orders {
  height: calc(100vh - 160px);
}
```

## How it works

- `[columns]`, `[rows]`, `[quickFilter]`, `[theme]` and `[licenseKey]` are shortcuts; everything else goes in `[options]`. After each change only the keys whose value changed by identity are passed to the grid's `update()`. The grid is never recreated, so switching the grouping keeps scroll position and selection.
- Every grid event is an output with a typed payload, including `stateChanged` and `columnGroupOpened`.
- The `api` property of the component is the `GridApi`. It is `undefined` until the first browser render, so read it in event handlers or after `(ready)`.
- The grid is created in `afterNextRender`, so the component works with `@angular/ssr` without changes.

## Pitfalls

- **Pass new arrays.** `this.orders.update((orders) => [...orders, order])` is detected; `orders().push(order)` is not. For frequent row changes use `this.grid().api?.applyTransaction({ update: [order] })`.
- **Give the host a height.** `<birchui-grid>` is a block element with no intrinsic height; without one it renders no rows.
- **Pro methods are optional.** Call `exportExcel?.()` with optional chaining, as Pro API methods are optional on `GridApi`.

## Related

- [Angular](https://docs.birchui.com/frameworks/angular.md), with the [inputs](https://docs.birchui.com/frameworks/angular.md#inputs) and [outputs](https://docs.birchui.com/frameworks/angular.md#outputs) tables.
- [Grid Pro overview](https://docs.birchui.com/grid-pro/overview.md) and [Excel export](https://docs.birchui.com/grid-pro/excel-export.md).
