Angular custom table layouts and view modes
views() gives a table view modes. Every views table has two built-in
modes, table and cards, and you add your own: a list, a grid of tiles,
any layout you draw in a template. A custom mode replaces only the table’s
body. The toolbar, search, filters, chips, the pager and selection keep
working, and your template receives rows that are already searched, filtered,
sorted and paged. A table that does not compose views never loads it.
views, injectViews and the layout directives come from
@adapttable/angular/features.
A template mode
Section titled “A template mode”A host mode’s render is a TemplateRef whose implicit context is the
layout’s CustomLayoutView. Read the template with viewChild and build the
features once it exists:
import type { ColumnDef } from "@adapttable/angular";import { AdaptViewCell, AdaptViewItem, AdaptViewItems, type CustomLayoutView, injectTableLink, injectViews, views,} from "@adapttable/angular/features";import { AdaptDataTable } from "@adapttable/angular-unstyled";import { ChangeDetectionStrategy, Component, computed, type TemplateRef, viewChild,} from "@angular/core";
interface Invoice { id: string; number: string; customer: string; amount: number;}
@Component({ selector: "app-invoices", imports: [AdaptDataTable, AdaptViewItems, AdaptViewItem, AdaptViewCell], changeDetection: ChangeDetectionStrategy.OnPush, template: ` <ng-template #tiles let-view> @if (view.loading) { <p>Loading invoices…</p> } @else if (view.noResults) { <p> {{ view.labels.noResults }} <button type="button" (click)="view.clearSearchAndFilters()"> {{ view.labels.clearAll }} </button> </p> } @else if (view.empty) { <p>{{ view.labels.noData }}</p> } @else { <div class="tiles" [adaptViewItems]="view"> @for (row of view.rows; track row.id; let i = $index) { <article [adaptViewItem]="{ view: view, row: row, index: i }"> <h3>{{ row.number }}</h3> <p>{{ row.customer }}</p> <!-- The amount exactly as the table's amount column renders it. --> <p> <adapt-view-cell [view]="view" [row]="row" column="amount" /> </p> </article> } </div> } </ng-template>
<div role="group" aria-label="View"> @for (mode of modes.modes(); track mode.key) { <button type="button" [attr.aria-pressed]="modes.mode() === mode.key" (click)="modes.setMode(mode.key)" > {{ mode.label }} </button> } </div> <adapt-data-table [data]="rows" [columns]="columns" [rowKey]="rowKey" [features]="features()" /> `,})export class Invoices { readonly link = injectTableLink<Invoice>(); readonly modes = injectViews(this.link); private readonly tiles = viewChild<TemplateRef<{ $implicit: CustomLayoutView<Invoice> }>>("tiles");
readonly rows: Invoice[] = [ { id: "1", number: "INV-0001", customer: "Acme", amount: 1200 }, { id: "2", number: "INV-0002", customer: "Globex", amount: 480 }, ]; readonly columns: ColumnDef<Invoice>[] = [ { key: "number", header: "Invoice" }, { key: "customer" }, { key: "amount" }, ]; readonly rowKey = (row: Invoice) => row.id;
/** The mode needs its template, so the features wait for it. */ readonly features = computed(() => { const tiles = this.tiles(); if (!tiles) return []; return [ views<Invoice>({ modes: [ { key: "tiles", label: "Tiles", itemsPerLine: 3, render: tiles }, ], }), this.link.feature, ]; });}.tiles { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem;}The table does not draw a mode switcher: draw it from injectViews(link)
where your page wants it. See host-drawn controls for
the link.
Template helpers
Section titled “Template helpers”| Helper | Put it on | What it does |
|---|---|---|
[adaptViewItems]="view" |
the element holding the items | role="list", the table’s accessible name, and the measuring ref that reads items per line. |
[adaptViewItem]="{ view, row, index }" |
each item | role="listitem", roving tabindex, aria-current, data-selected, and the keyboard, click and focus handling. |
[adaptViewLine]="{ view, line }" |
each line of a windowed layout | Measures the line’s height and marks its index. |
<adapt-view-cell [view] [row] column="key" /> |
wherever a cell belongs | The column’s cell for that row, drawn by the table’s own cell renderer. |
views<TRow>({ modes?, defaultMode? }) takes your modes, offered after
table and cards in declared order, and the mode shown when the URL names
none ("table" by default). Each mode:
| Field | Type | Description |
|---|---|---|
key |
string |
Written to the URL as view=<key>. table and cards are taken. |
label |
string |
The mode’s name in your switcher. |
render |
TemplateRef<{ $implicit: CustomLayoutView<TRow> }> |
Drawn in place of the body. |
itemsPerLine |
number |
Items on one line; measured from your layout’s first line when omitted. |
estimateLineSize |
number |
A line’s height in px before it is measured. Defaults to 120. |
A built-in or repeated key, or a defaultMode that names no mode, throws.
table draws the table, with the kit’s mobile cards on a phone; cards
draws those cards at every width (mobile). The built-in names
come from labels.viewModeTable and labels.viewModeCards, localized in
every locale.
injectViews(link) returns signals ready, modes, mode and
activeRowId, with setMode(key) and setActiveRow(id). An undeclared mode
warns in development.
CustomLayoutView
Section titled “CustomLayoutView”| Field | Meaning |
|---|---|
rows |
The rows to draw: after search, filters, sort and paging. |
rowId(row) |
The row’s id, from the table’s rowKey. |
containerProps |
What adaptViewItems applies. |
itemProps(row, index) |
What adaptViewItem applies. |
selection |
{ isSelected(row), toggle(row) } when the table composes selection; otherwise undefined. |
activeRowId |
The active row. |
setActiveRow(id) |
Sets or clears it. |
loading |
The first page is still loading. |
empty |
Nothing is loading and there are no rows. |
noResults |
The rows are empty because search or filters hid them. |
clearSearchAndFilters() |
Clears both, as the table’s own empty state does. |
labels |
The table’s strings in its locale. |
phone |
The viewport is a phone’s. |
cell(row, key) |
The column and the row’s index that adapt-view-cell draws; undefined when key names no column. |
virtual |
The window, when the table composes virtualize() and is not paged; otherwise undefined. |
The view is a plain value in the template context, refreshed whenever the table changes.
Keyboard
Section titled “Keyboard”Items use a roving tab stop: Tab enters the layout on one item. → and ← step through items in reading order, ↓ and ↑ move a line, Home and End go to the first and last row of the data, Page Down and Page Up move a screen of items. Enter makes the item’s row active and Space toggles its selection when the table composes selection. In a right-to-left table ← and → swap. A click on an item makes its row active unless it lands on a control inside the item, and keys typed into such a control stay with it.
Windowing a large layout
Section titled “Windowing a large layout”Compose virtualize() from your kit on a table that is not paged — for
example paginationMode="infinite" — and view.virtual holds the window.
Draw its lines, each with adaptViewLine, between spacers of before and
after pixels:
import type { ColumnDef } from "@adapttable/angular";import { AdaptViewCell, AdaptViewItem, AdaptViewItems, AdaptViewLine, type CustomLayoutView, views,} from "@adapttable/angular/features";import { AdaptDataTable } from "@adapttable/angular-unstyled";import { virtualize } from "@adapttable/angular-unstyled/virtualize";import { ChangeDetectionStrategy, Component, computed, input, type TemplateRef, viewChild,} from "@angular/core";
interface Invoice { id: string; number: string; amount: number;}
@Component({ selector: "app-invoice-archive", imports: [ AdaptDataTable, AdaptViewItems, AdaptViewItem, AdaptViewLine, AdaptViewCell, ], changeDetection: ChangeDetectionStrategy.OnPush, template: ` <ng-template #grid let-view> @if (view.virtual; as virtual) { <div [adaptViewItems]="view"> <div [style.height.px]="virtual.before"></div> @for (line of virtual.lines; track line.key) { <div class="tiles" [adaptViewLine]="{ view: view, line: line }"> @for (entry of line.items; track entry.row.id) { <article [adaptViewItem]="{ view: view, row: entry.row, index: entry.index, }" > <h3>{{ entry.row.number }}</h3> <adapt-view-cell [view]="view" [row]="entry.row" column="amount" /> </article> } </div> } <div [style.height.px]="virtual.after"></div> </div> } </ng-template> <adapt-data-table [data]="rows()" [columns]="columns" [rowKey]="rowKey" paginationMode="infinite" [maxHeight]="560" [features]="features()" /> `,})export class InvoiceArchive { readonly rows = input.required<readonly Invoice[]>(); private readonly grid = viewChild<TemplateRef<{ $implicit: CustomLayoutView<Invoice> }>>("grid");
readonly columns: ColumnDef<Invoice>[] = [ { key: "number" }, { key: "amount" }, ]; readonly rowKey = (row: Invoice) => row.id; readonly features = computed(() => { const grid = this.grid(); if (!grid) return []; return [ virtualize(), views<Invoice>({ defaultMode: "grid", modes: [{ key: "grid", label: "Grid", itemsPerLine: 3, render: grid }], }), ]; });}A list is the same with itemsPerLine: 1. Pass each item’s absolute index
from line.items, so the keyboard moves through the whole data.
adaptViewLine measures every line, so lines of any height work. With
maxHeight the layout scrolls in its own box, otherwise the page scrolls;
reaching the last line of an infinite table loads the next page. End and
Page Down scroll the target into the window and focus it once it is
drawn; virtual.scrollToIndex(index) does the same for your own code.
The active row and a master-detail page
Section titled “The active row and a master-detail page”Enter or a click sets the active row; its item gets
aria-current="true" and the URL gets active=<id>, so the row survives
mode switches, reloads and shared links. Read it inside the template with
view.activeRowId, or anywhere with injectViews(link).activeRowId():
import type { ColumnDef } from "@adapttable/angular";import { AdaptViewItem, AdaptViewItems, type CustomLayoutView, injectTableLink, injectViews, views,} from "@adapttable/angular/features";import { AdaptDataTable } from "@adapttable/angular-unstyled";import { ChangeDetectionStrategy, Component, computed, type TemplateRef, viewChild,} from "@angular/core";
interface Invoice { id: string; number: string; customer: string; amount: number;}
@Component({ selector: "app-invoice-inbox", imports: [AdaptDataTable, AdaptViewItems, AdaptViewItem], changeDetection: ChangeDetectionStrategy.OnPush, template: ` <ng-template #list let-view> <ul class="invoice-list" [adaptViewItems]="view"> @for (row of view.rows; track row.id; let i = $index) { <li [adaptViewItem]="{ view: view, row: row, index: i }"> <strong>{{ row.number }}</strong> {{ row.customer }} </li> } </ul> </ng-template> <div class="inbox"> <adapt-data-table [data]="rows" [columns]="columns" [rowKey]="rowKey" [features]="features()" /> <aside aria-label="Invoice details"> @if (open(); as invoice) { <h2>{{ invoice.number }}</h2> <p>{{ invoice.customer }}</p> <button type="button" (click)="modes.setActiveRow(undefined)"> Close </button> } @else { <p>Select an invoice.</p> } </aside> </div> `,})export class InvoiceInbox { readonly link = injectTableLink<Invoice>(); readonly modes = injectViews(this.link); private readonly list = viewChild<TemplateRef<{ $implicit: CustomLayoutView<Invoice> }>>("list");
readonly rows: Invoice[] = [ { id: "1", number: "INV-0001", customer: "Acme", amount: 1200 }, { id: "2", number: "INV-0002", customer: "Globex", amount: 480 }, ]; readonly columns: ColumnDef<Invoice>[] = [ { key: "number" }, { key: "customer" }, { key: "amount" }, ]; readonly rowKey = (row: Invoice) => row.id; readonly open = computed(() => this.rows.find((row) => row.id === this.modes.activeRowId()) ); readonly features = computed(() => { const list = this.list(); if (!list) return []; return [ views<Invoice>({ defaultMode: "list", modes: [{ key: "list", label: "List", itemsPerLine: 1, render: list }], }), this.link.feature, ]; });}Related
Section titled “Related”- Host-drawn controls — the link, and your own filter, sort and search controls.
- Mobile — the card layout
cardsshows at every width. - Virtualization —
virtualize()for the table body. - Toolbar & view controls — what the built-in toolbar draws.