diff --git a/docs/framework/angular/angular-table.md b/docs/framework/angular/angular-table.md index 041db8c64d..c99473a03b 100644 --- a/docs/framework/angular/angular-table.md +++ b/docs/framework/angular/angular-table.md @@ -2,7 +2,8 @@ title: Angular Table --- -The `@tanstack/angular-table` adapter is a wrapper around the core table logic. Most of it's job is related to managing state the "angular signals" way, providing types and the rendering implementation of cell/header/footer templates. +The `@tanstack/angular-table` adapter is a wrapper around the core table logic. Most of it's job is related to managing +state the "angular signals" way, providing types and the rendering implementation of cell/header/footer templates. ## Exports @@ -10,10 +11,10 @@ The `@tanstack/angular-table` adapter is a wrapper around the core table logic. ### `createAngularTable` -Takes an `options` object and returns a table. +Accepts an options function or a computed value that returns the table options, and returns a table. ```ts -import { createAngularTable } from '@tanstack/angular-table' +import {createAngularTable} from '@tanstack/angular-table' export class AppComponent { data = signal([]) @@ -24,13 +25,20 @@ export class AppComponent { getCoreRowModel: getCoreRowModel(), })) } + // ...render your table in template ``` ### `FlexRender` -A Angular component for rendering cell/header/footer templates with dynamic values. +An Angular structural directive for rendering cell/header/footer templates with dynamic values. + +FlexRender supports any type of content supported by Angular: + +- A string, or a html string via `innerHTML` +- A [TemplateRef](https://angular.dev/api/core/TemplateRef) +- A [Component](https://angular.dev/api/core/Component) wrapped into `FlexRenderComponent` Example: @@ -42,23 +50,168 @@ Example: ``` ```angular2html + - @for (row of table.getRowModel().rows; track row.id) { - - @for (cell of row.getVisibleCells(); track cell.id) { - - -
-
- - } - - } + > + + {{ cell }} + +
+ + +} + +} ``` + +#### Rendering a TemplateRef + +In order to render a TemplateRef into a specific column header/cell/footer, you can pass the TemplateRef into the column +definition. + +You can access the TemplateRef data via the `$implicit` property, which is valued based on what is passed in the props +field of flexRender. + +In most cases, each TemplateRef will be rendered with the $implicit context valued based on the cell type in this way: + +- Header: `HeaderContext` +- Cell: `CellContext`, +- Footer: `HeaderContext` + +```angular17html + + + + {{ cell }} + +
+
+ + + + +``` + +Full example: + +```ts +import type { + CellContext, + ColumnDef, + HeaderContext, +} from '@tanstack/angular-table' +import {Component, TemplateRef, viewChild} from '@angular/core' + +@Component({ + template: ` + + @for (row of table.getRowModel().rows; track row.id) { + + @for (cell of row.getVisibleCells(); track cell.id) { + + + + {{ cell }} + +
+
+ + } + + } + + + + {{ context.getValue() }} + + + {{ context.getValue() }} + + `, +}) +class AppComponent { + customHeader = + viewChild.required }>>( + 'customHeader' + ) + customCell = + viewChild.required }>>( + 'customCell' + ) + + columns: ColumnDef[] = [ + { + id: 'customCell', + header: () => this.customHeader(), + cell: () => this.customCell(), + }, + ] +} +``` + +#### Rendering a Component + +To render a Component into a specific column header/cell/footer, you can pass a `FlexRenderComponent instantiated with +your `ComponentType, with the ability to include optional parameters such as inputs and an injector. + +```ts +import {FlexRenderComponent} from "@tanstack/angular-table"; + +class AppComponent { + columns: ColumnDef[] = [ + { + id: 'customCell', + header: () => new FlexRenderComponent( + CustomCellComponent, + {}, // optional inputs + injector // optional injector + ), + cell: () => this.customCell(), + }, + ] +} +``` + +Underneath, this utilizes +the [ViewContainerRef#createComponent](https://angular.dev/api/core/ViewContainerRef#createComponent) api. +Therefore, you should declare your custom inputs using the @Input decorator or input/model signals. + +You can still access the table cell context through the `injectFlexRenderContext` function, which returns the context +value based on the props you pass to the `FlexRenderDirective`. + +```ts +@Component({ + // ... +}) +class CustomCellComponent { + // context of a cell component + readonly context = injectFlexRenderContext>(); + // context of a header/footer component + readonly context = injectFlexRenderContext>(); +} +``` + +