In Angular Material's `mat-table`, how do you pin the SKU column while scrolling, add an expandable detail row, and what does `multiTemplateDataRows` change?
answer
- sticky goes on the column definition
- header row sticks with sticky: true
- only one default row without it
- every matching row def per item
- dataIndex and renderIndex
basics
~20 sAdd sticky to the SKU column's matColumnDef (stickyEnd pins a column to the end, sticky: true in matHeaderRowDef pins the header). For detail rows, add a second matRowDef and set multiTemplateDataRows so each item can render more than one row.
solid answer
~40 sStickiness is declared, not styled: `sticky` on `<ng-container matColumnDef="sku" sticky>` keeps the SKU column at the start during horizontal scroll, `stickyEnd` pins an actions column to the end, and `*matHeaderRowDef="columns; sticky: true"` keeps the header at the top, all relative to a scrolling container around the table. For an expandable detail row, define a column whose cell spans the table with `colspan`, and a second `matRowDef` that uses it. Without `multiTemplateDataRows`, each item renders exactly one row: the first whose `when` predicate passes, otherwise the single default, and two defaults throw. With `multiTemplateDataRows`, each item renders every row definition whose `when` passes or that has none, and the row context replaces `index` with `dataIndex` and `renderIndex`.
code
html · 24 lines<div class="inventory-scroll">
<table mat-table [dataSource]="dataSource" multiTemplateDataRows aria-label="Inventory">
<ng-container matColumnDef="sku" sticky>
<th mat-header-cell *matHeaderCellDef>SKU</th>
<td mat-cell *matCellDef="let item">
<button type="button" (click)="toggle(item)" [attr.aria-expanded]="expanded() === item">{{ item.sku }}</button>
</td>
</ng-container>
<ng-container matColumnDef="stock">
<th mat-header-cell *matHeaderCellDef>Stock</th>
<td mat-cell *matCellDef="let item">{{ item.stock }}</td>
</ng-container>
<ng-container matColumnDef="detail">
<td mat-cell *matCellDef="let item" [attr.colspan]="columns.length">
Bins: {{ item.bins.join(', ') }}
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="columns; sticky: true"></tr>
<tr mat-row *matRowDef="let item; columns: columns"></tr>
<tr mat-row *matRowDef="let item; columns: ['detail']"
class="detail-row" [class.collapsed]="expanded() !== item"></tr>
</table>
</div>go deeper
Recall that sticky and stickyEnd go on column definitions and that sticky: true on the header row definition pins the header.
Explain how the table chooses one row definition per item by default, and what changes when multiTemplateDataRows is set.
Build an accessible expandable detail row, choose between collapsing with a class and a when predicate with renderRows(), and diagnose sticky cells that do not stick.
Judge when the same behaviour belongs on CdkTable with your own styles, and how detail rows interact with windowing and dense data screens across a product.
## Sticky rows and columns Angular Material's table applies CSS `position: sticky` for you; you only say what should stick. | Goal | Where the input goes | |---|---| | Pin a column to the start | `sticky` on its `matColumnDef` container | | Pin a column to the end | `stickyEnd` on its `matColumnDef` container | | Pin the header row to the top | `sticky: true` in `*matHeaderRowDef` | | Pin the footer row to the bottom | `sticky: true` in `*matFooterRowDef` | Sticky positioning is relative to the nearest scrolling ancestor, so wrap the table in a container with a fixed height or width and `overflow: auto`. The table measures cell widths to compute the offsets of each stuck column; on native tables it re-measures when the data changes. The `fixedLayout` input enforces uniform column widths so those measurements can be cached, which cuts rendering work on large native tables. The component guide records browser quirks: with the flex-based table on mobile Safari, a cell stuck in two directions (the top-left corner of a sticky header plus sticky column) can jitter while scrolling, and a sticky footer in a native table on Safari only sticks when every rendered footer row is sticky. ## One row per item, or several By default, `CdkTable` and therefore `mat-table` render **one row per data item**. With several `matRowDef` templates, the table picks the first whose `when` predicate returns true for that item, and otherwise the **default** row, the one with no `when`. Declaring two rows without a `when` throws: "There can only be one default row without a when predicate function. Or set `multiTemplateDataRows`." Adding `multiTemplateDataRows` to the table changes the rule: - each item renders **every** row definition whose `when` passes, plus every definition that has no `when`; - the rows render in the order the definitions are declared, item by item; - the cell and row context replaces `index` with `dataIndex` (the item's position in the data) and `renderIndex` (the rendered row's position), because one item now occupies several rendered rows. ## The expandable detail row For the warehouse screen, clicking a SKU reveals its bin locations and reorder history underneath. 1. Define a column, for example `matColumnDef="detail"`, whose cell has `[attr.colspan]="columns.length"` so it spans the full width. 2. Keep the main row, `*matRowDef="let item; columns: columns"`, and put a real `<button>` in the SKU cell that toggles an `expanded` signal and reports `aria-expanded`, so keyboard and screen-reader users can open the detail too. 3. Add the detail row: `*matRowDef="let item; columns: ['detail']"`. 4. Set `multiTemplateDataRows` on the table. Two ways to show and hide it: - **Render it for every item and collapse it with a class** bound to the expanded state, for example `[class.collapsed]="expanded() !== item"` with zero height. Nothing in the table needs to re-evaluate, and an expand animation is easy. - **Use a `when` predicate**, such as `when: isExpanded`. The table evaluates `when` while it renders rows, so toggling the state needs a `renderRows()` call to add or remove the detail row. With virtual scrolling enabled, conditional row templates through `when` are not supported and the table throws in development builds; the always-rendered pattern avoids that. ## Doing it without Material's styles `CdkTable` from `@angular/cdk/table` offers the same features with `cdk-` names: `cdkColumnDef` with `sticky` and `stickyEnd`, `cdkHeaderRowDef` with `sticky`, `cdkRowDef` with `when`, and `multiTemplateDataRows` on the table. The CDK computes and applies the sticky offsets itself; you supply every visual style, including backgrounds for stuck cells. ## Row recycling and detail rows By default the table creates and destroys an Angular view for each row, which is what lets rows switch between templates through `when` and take part in animations. The `recycleRows` input caches and reuses row views instead, which saves work on large tables that re-render often, but the CDK guide offers it for tables that do not need those features. A table that swaps detail rows in and out through `when` should therefore stay on the default; the always-rendered, class-collapsed detail row has no such conflict. ## Mistakes to avoid - Putting `sticky` on the header row and expecting a column to pin. - Adding a second default `matRowDef` without `multiTemplateDataRows`. - Reading `index` in a multi-template table and getting `undefined`. - Forgetting the `colspan`, which squeezes the detail into the first column.
- Why does a sticky column in an Angular Material table not stick even though sticky is set?Sticky positioning is relative to the nearest scrolling ancestor. If the page scrolls rather than a container around the table, or an ancestor has `overflow: hidden`, there is nothing for the cell to stick within. Wrap the table in a container with a bounded width or height and `overflow: auto`.
- In a multi-template Angular Material table, how do you stripe only the main rows when detail rows sit between them?The `even` and `odd` context values count rendered rows, so detail rows shift the pattern. Use `dataIndex` from the row context instead, for example `let i = dataIndex` in the main `matRowDef` and `[class.striped]="i % 2"`, which counts data items rather than rendered rows.
- Put sticky on the header row definition to pin the first column.
- Two matRowDef templates without when render both rows automatically.
- Toggling the state behind a when predicate updates rows on its own.
- In a multiTemplateDataRows table, index still gives the item's position.
- Sticky columns need hand-written position: sticky CSS on the cells.