General
Tree Grid
Usage#
import { WDS_TREE_GRID } from '@wds/angular/tree-grid';columns on wds-tree-grid and render a wds-tree-grid-row per item, with a wds-tree-grid-cell per column (column is the column key). The tree comes from the order of the rows and their level: render a parent, then its sub-rows with a higher level. The grid adds the indent and the expand toggle to the tree column and hides the sub-rows of a collapsed row. Cells hold your own markup and components, so a cell can be a picker, a button or a link. To show plain records with paging, filtering and editing built in, use Data Table.
Examples#
Default#
level under a row become its sub-rows: the first column gets the indent and the toggle. Collapse a row with the toggle or with ← on the focused row.
import { Component } from '@angular/core';
import { WdsTreeGridCellComponent, WdsTreeGridComponent, WdsTreeGridRowComponent } from '@wds/angular/tree-grid';
import type { WdsTreeGridColumn } from '@wds/core/tree-grid';
interface Step {
id: string;
name: string;
owner: string;
due: string;
children?: Step[];
}
const PLAN: Step[] = [
{
id: 'plan',
name: 'Planning',
owner: 'Dana Pike',
due: 'Nov 3',
children: [
{ id: 'site', name: 'Shortlist three sites', owner: 'Dana Pike', due: 'Oct 27' },
{ id: 'lease', name: 'Sign the lease', owner: 'Omar Bell', due: 'Nov 3' },
],
},
{
id: 'fit',
name: 'Fit-out',
owner: 'Lena Fox',
due: 'Nov 28',
children: [
{
id: 'net',
name: 'Network',
owner: 'Theo Marsh',
due: 'Nov 20',
children: [
{ id: 'cable', name: 'Run the cabling', owner: 'Theo Marsh', due: 'Nov 14' },
{ id: 'wifi', name: 'Mount the access points', owner: 'Theo Marsh', due: 'Nov 20' },
],
},
{ id: 'desks', name: 'Order desks and chairs', owner: 'Lena Fox', due: 'Nov 21' },
],
},
{ id: 'move', name: 'Moving day', owner: 'Omar Bell', due: 'Dec 5' },
];
/** The tree as rows in display order, each with its level. */
function flatten(steps: Step[], level = 1): { step: Step; level: number }[] {
return steps.flatMap((step) => [{ step, level }, ...flatten(step.children ?? [], level + 1)]);
}
@Component({
selector: 'app-tree-grid-default-demo',
host: { class: 'block w-full' },
imports: [WdsTreeGridComponent, WdsTreeGridRowComponent, WdsTreeGridCellComponent],
template: `
<wds-tree-grid label="Office move" [columns]="columns">
@for (row of rows; track row.step.id) {
<wds-tree-grid-row [value]="row.step.id" [level]="row.level" [label]="row.step.name">
<wds-tree-grid-cell column="name">{{ row.step.name }}</wds-tree-grid-cell>
<wds-tree-grid-cell column="owner">{{ row.step.owner }}</wds-tree-grid-cell>
<wds-tree-grid-cell column="due">{{ row.step.due }}</wds-tree-grid-cell>
</wds-tree-grid-row>
}
</wds-tree-grid>
`,
})
export class TreeGridDefaultDemo {
protected readonly rows = flatten(PLAN);
protected readonly columns: WdsTreeGridColumn[] = [
{ key: 'name', header: 'Name', width: 320 },
{ key: 'owner', header: 'Owner', width: 160 },
{ key: 'due', header: 'Due', width: 100, align: 'end' },
];
}Groups and row actions#
wds-tree-grid-group has a heading (label, count and actions slots), its own column headers and a footer. Here tasks are grouped by the status of the top-level task, and sub-tasks stay under their parent whatever their own status. Buttons in the actions slot of a cell and the menu slot of a row show on hover and focus; selected keeps them visible.
import { Component, computed, signal } from '@angular/core';
import { WdsBadgeComponent } from '@wds/angular/badge';
import { WdsButtonComponent } from '@wds/angular/button';
import { WdsDropdownComponent, WdsDropdownItemComponent } from '@wds/angular/dropdown';
import { WdsTreeGridCellComponent, WdsTreeGridComponent, WdsTreeGridGroupComponent, WdsTreeGridRowComponent } from '@wds/angular/tree-grid';
import type { WdsTreeGridColumn } from '@wds/core/tree-grid';
import '@wds/core/icons/lucide/ellipsis';
import '@wds/core/icons/lucide/plus';
type Status = 'doing' | 'todo';
interface Task {
id: string;
title: string;
status: Status;
/** Parent task of a sub-task. */
parent?: string;
}
const STATUSES: { status: Status; label: string; variant: 'default' | 'secondary' }[] = [
{ status: 'doing', label: 'In progress', variant: 'default' },
{ status: 'todo', label: 'To do', variant: 'secondary' },
];
let nextId = 1;
@Component({
selector: 'app-tree-grid-groups-demo',
host: { class: 'block w-full' },
imports: [
WdsBadgeComponent,
WdsButtonComponent,
WdsDropdownComponent,
WdsDropdownItemComponent,
WdsTreeGridComponent,
WdsTreeGridGroupComponent,
WdsTreeGridRowComponent,
WdsTreeGridCellComponent,
],
template: `
<wds-tree-grid label="Tasks" [columns]="columns">
@for (group of groups(); track group.status) {
<wds-tree-grid-group [value]="group.status" [label]="group.label">
<wds-badge slot="label" [variant]="group.variant">{{ group.label }}</wds-badge>
<span slot="count">{{ group.count }}</span>
<wds-button slot="actions" variant="ghost" size="xs" icon="lucide-plus" [label]="'Add task to ' + group.label" (click)="add(group.status)" />
@for (row of group.rows; track row.task.id) {
<wds-tree-grid-row [value]="row.task.id" [level]="row.level" [label]="row.task.title" [selected]="selected() === row.task.id" (click)="selected.set(row.task.id)">
<wds-tree-grid-cell column="title">
{{ row.task.title }}
<wds-button slot="actions" variant="outline" size="xs" icon="lucide-plus" [label]="'Add sub-task to ' + row.task.title" (click)="add(row.task.status, row.task.id)" />
</wds-tree-grid-cell>
<wds-tree-grid-cell column="status">{{ label(row.task.status) }}</wds-tree-grid-cell>
<wds-dropdown slot="menu" align="end" (wdsSelect)="remove(row.task.id)">
<wds-button slot="trigger" variant="ghost" size="xs" icon="lucide-ellipsis" [label]="'Actions for ' + row.task.title" />
<wds-dropdown-item value="delete" variant="destructive">Delete</wds-dropdown-item>
</wds-dropdown>
</wds-tree-grid-row>
}
<wds-button slot="footer" variant="ghost" size="sm" icon-start="lucide-plus" (click)="add(group.status)">Add task</wds-button>
</wds-tree-grid-group>
}
</wds-tree-grid>
`,
})
export class TreeGridGroupsDemo {
protected readonly selected = signal('');
protected readonly tasks = signal<Task[]>([
{ id: 'a', title: 'Write the release notes', status: 'doing' },
{ id: 'a1', title: 'Collect the merged changes', status: 'todo', parent: 'a' },
{ id: 'a2', title: 'Draft the summary', status: 'doing', parent: 'a' },
{ id: 'b', title: 'Update the onboarding guide', status: 'doing' },
{ id: 'c', title: 'Plan the next sprint', status: 'todo' },
{ id: 'c1', title: 'Sort the backlog', status: 'todo', parent: 'c' },
]);
protected readonly columns: WdsTreeGridColumn[] = [
{ key: 'title', header: 'Name', width: 360 },
{ key: 'status', header: 'Status', width: 140 },
];
/** Groups by the status of top-level tasks; sub-tasks stay under their parent whatever their status. */
protected readonly groups = computed(() => {
const tasks = this.tasks();
const below = (task: Task, level: number): { task: Task; level: number }[] => [
{ task, level },
...tasks.filter((t) => t.parent === task.id).flatMap((t) => below(t, level + 1)),
];
return STATUSES.map((s) => {
const roots = tasks.filter((t) => !t.parent && t.status === s.status);
return { ...s, count: roots.length, rows: roots.flatMap((t) => below(t, 1)) };
});
});
protected label(status: Status): string {
return STATUSES.find((s) => s.status === status)!.label;
}
protected add(status: Status, parent?: string): void {
const task: Task = { id: `new-${nextId++}`, title: parent ? 'New sub-task' : 'New task', status, parent };
// A sub-task goes after the last row of its parent's branch.
this.tasks.update((list) => {
if (!parent) return [...list, task];
const index = list.reduce((last, t, i) => (t.id === parent || t.parent === parent ? i : last), -1);
return [...list.slice(0, index + 1), task, ...list.slice(index + 1)];
});
}
/** Removes the task with its sub-tasks. */
protected remove(id: string): void {
this.tasks.update((list) => {
const gone = new Set([id]);
for (const t of list) if (t.parent && gone.has(t.parent)) gone.add(t.id);
return list.filter((t) => !gone.has(t.id));
});
}
}Dragging, sorting and columns#
row-reorderable adds a grip to the rows and nestable lets a row be dropped onto another to become its sub-row. The grid does not move rows: wds-row-reorder gives the dragged row, the target row and the position (before, after or inside), and you render the new order; dropped onto a group outside its rows (also an empty group), target is empty and the row goes to the end of the group. Sortable headers fire wds-sort-change; resizable and reorderable change the column widths and order for all rows and groups at once.
import { Component, computed, signal } from '@angular/core';
import { WdsTreeGridCellComponent, WdsTreeGridComponent, WdsTreeGridRowComponent } from '@wds/angular/tree-grid';
import type { WdsTreeGridColumn, WdsTreeGridRowReorderDetail, WdsTreeGridSort } from '@wds/core/tree-grid';
interface Item {
id: string;
name: string;
owner: string;
hours: number;
parent?: string;
}
const ITEMS: Item[] = [
{ id: 'design', name: 'Design', owner: 'Ivy Chen', hours: 24 },
{ id: 'wire', name: 'Wireframes', owner: 'Ivy Chen', hours: 10, parent: 'design' },
{ id: 'proto', name: 'Clickable prototype', owner: 'Max Ortiz', hours: 14, parent: 'design' },
{ id: 'build', name: 'Build', owner: 'Rae Lund', hours: 60 },
{ id: 'api', name: 'API endpoints', owner: 'Rae Lund', hours: 26, parent: 'build' },
{ id: 'ui', name: 'Screens', owner: 'Max Ortiz', hours: 34, parent: 'build' },
{ id: 'qa', name: 'Testing', owner: 'Noor Haddad', hours: 16 },
];
/** The ids of an item and all items under it. */
function branch(items: Item[], id: string): Set<string> {
const ids = new Set([id]);
for (const item of items) if (item.parent && ids.has(item.parent)) ids.add(item.id);
return ids;
}
/** Moves an item with its sub-items before, after or into the target. */
function move(items: Item[], { value, target, position }: WdsTreeGridRowReorderDetail): Item[] {
const moved = branch(items, value);
const goal = items.find((i) => i.id === target);
if (!goal || moved.has(target)) return items;
const parent = position === 'inside' ? goal.id : goal.parent;
const block = items.filter((i) => moved.has(i.id)).map((i) => (i.id === value ? { ...i, parent } : i));
const rest = items.filter((i) => !moved.has(i.id));
// Before the target, or after its whole branch (also for `inside`: as the last sub-item).
const below = branch(rest, target);
const index = position === 'before' ? rest.indexOf(goal) : Math.max(...[...below].map((id) => rest.findIndex((i) => i.id === id))) + 1;
return [...rest.slice(0, index), ...block, ...rest.slice(index)];
}
@Component({
selector: 'app-tree-grid-reorder-demo',
host: { class: 'block w-full' },
imports: [WdsTreeGridComponent, WdsTreeGridRowComponent, WdsTreeGridCellComponent],
template: `
<wds-tree-grid
label="Project"
[columns]="columns"
row-reorderable
nestable
resizable
reorderable
[sortKey]="sort()?.key ?? ''"
[sortDirection]="sort()?.direction ?? 'asc'"
[columnOrder]="order()"
(wdsSortChange)="sort.set($event.sort)"
(wdsColumnReorder)="order.set($event.order)"
(wdsRowReorder)="reorder($event)"
>
@for (row of rows(); track row.item.id) {
<wds-tree-grid-row [value]="row.item.id" [level]="row.level" [label]="row.item.name">
<wds-tree-grid-cell column="name">{{ row.item.name }}</wds-tree-grid-cell>
<wds-tree-grid-cell column="owner">{{ row.item.owner }}</wds-tree-grid-cell>
<wds-tree-grid-cell column="hours">{{ row.item.hours }} h</wds-tree-grid-cell>
</wds-tree-grid-row>
}
</wds-tree-grid>
`,
})
export class TreeGridReorderDemo {
protected readonly items = signal(ITEMS);
protected readonly sort = signal<WdsTreeGridSort | null>(null);
// Keep the column order (such as in localStorage) and pass it back in columnOrder.
protected readonly order = signal(['name', 'owner', 'hours']);
protected readonly columns: WdsTreeGridColumn[] = [
{ key: 'name', header: 'Name', width: 280, sortable: true },
{ key: 'owner', header: 'Owner', width: 160, sortable: true },
{ key: 'hours', header: 'Estimate', width: 110, sortable: true, align: 'end' },
];
/** Rows in display order: sub-items under their parent, siblings sorted by the sorted column. */
protected readonly rows = computed(() => {
const items = this.items();
const sort = this.sort();
const children = (parent?: string) => {
const list = items.filter((i) => i.parent === parent);
if (!sort) return list;
const key = sort.key as keyof Item;
const sign = sort.direction === 'asc' ? 1 : -1;
return [...list].sort((a, b) => sign * String(a[key]).localeCompare(String(b[key]), undefined, { numeric: true }));
};
const walk = (parent: string | undefined, level: number): { item: Item; level: number }[] =>
children(parent).flatMap((item) => [{ item, level }, ...walk(item.id, level + 1)]);
return walk(undefined, 1);
});
/** Dragging sets a manual order, so sorting is turned off. */
protected reorder(detail: WdsTreeGridRowReorderDetail): void {
this.items.set(move(this.rows().map((r) => r.item), detail));
this.sort.set(null);
}
}API References#
wds-tree-grid#
wds-tree-grid-row elements with wds-tree-grid-cell children, so cells can hold any markup or framework
component, and wrap them in wds-tree-grid-group elements to show them in groups.
Rows form the tree by their order and level: a row followed by rows with a higher level is their parent
and gets an expand toggle in the tree column. Collapsing a row hides its sub-rows.
Columns come from columns. Every group repeats the header row, and all rows share the column widths.
Sorting, resizing and reordering columns update sort-key, columnWidths and column-order and fire events;
the grid does not sort or move rows itself: with row-reorderable it fires wds-row-reorder and you
render the rows in the new order.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
| hidden-columns.hiddenColumns | string[] | [] | |
| column-order.columnOrder | string[] | [] | columns order. |
| tree-column.treeColumn | string | '' | |
| sort-key.sortKey | string | '' | |
| sort-direction.sortDirection | WdsTreeGridSortDirection | 'asc' | |
| resizable | boolean | false | |
| reorderable | boolean | false | |
| row-reorderable.rowReorderable | boolean | false | wds-row-reorder. |
| nestable | boolean | false | row-reorderable: a row can also be dropped onto another row to become its sub-row. |
| density | WdsTreeGridDensity | 'default' | |
| indent | number | 24 | |
| label | string | '' |
Properties
| Property | Description |
|---|---|
| columnsWdsTreeGridColumn[] | |
| columnWidthsRecord<string, number> | width. |
Events
| Event | Description |
|---|---|
| wds-sort-changeCustomEvent<WdsTreeGridSortChangeDetail> | |
| wds-column-resizeCustomEvent<WdsTreeGridColumnResizeDetail> | resizable), once the drag ends. |
| wds-column-reorderCustomEvent<WdsTreeGridColumnReorderDetail> | reorderable). |
| wds-row-reorderCustomEvent<WdsTreeGridRowReorderDetail> | target is empty), with row-reorderable; move it in your data. |
| wds-row-toggleCustomEvent<WdsTreeGridRowToggleDetail> | |
| wds-group-toggleCustomEvent<WdsTreeGridGroupToggleDetail> |
Slots
| Slot | Description |
|---|---|
| (default) | wds-tree-grid-group elements, or wds-tree-grid-row elements without groups. |
CSS parts (::part)
| Part | Description |
|---|---|
| scroller | |
| header |
CSS custom properties
| Property | Description |
|---|---|
| --wds-tree-grid-indent | indent, 24px). |
| --wds-tree-grid-group-gap |
wds-tree-grid-group#
wds-tree-grid: a heading with an expand toggle, the column headers and the rows.
Every group repeats the column headers, and all groups share the column widths and order.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
| value | string | '' | |
| label | string | '' | value). |
| expanded | boolean | true |
Events
| Event | Description |
|---|---|
| wds-group-toggleCustomEvent<WdsTreeGridGroupToggleDetail> |
Slots
| Slot | Description |
|---|---|
| (default) | wds-tree-grid-row elements. |
| label | |
| count | |
| actions | |
| footer |
CSS parts (::part)
| Part | Description |
|---|---|
| heading | |
| header | |
| footer |
wds-tree-grid-row#
wds-tree-grid: wds-tree-grid-cell elements, one per column. level places the row in the tree;
a row followed by rows with a higher level is their parent and gets an expand toggle in the tree column.
Elements in the actions slot of a cell and in the menu slot of the row show while the pointer is over
the row or focus is inside it.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
| value | string | '' | |
| level | number | 1 | |
| expanded | boolean | true | |
| selected | boolean | false | |
| label | string | '' | value). |
Events
| Event | Description |
|---|---|
| wds-row-toggleCustomEvent<WdsTreeGridRowToggleDetail> |
Slots
| Slot | Description |
|---|---|
| (default) | wds-tree-grid-cell elements. |
| menu |
CSS parts (::part)
| Part | Description |
|---|---|
| grip | row-reorderable on the grid). |
CSS custom properties
| Property | Description |
|---|---|
| --wds-tree-grid-row-hover | |
| --wds-tree-grid-row-selected | selected row. |
wds-tree-grid-cell#
wds-tree-grid-row, placed in the column named by column. The cell of the tree column also holds
the indent of the row's level and the expand toggle. A cell of a hidden or unknown column is not shown.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
| column | string | '' | |
| colspan | string | '' | end for all the remaining width of the row. |
Slots
| Slot | Description |
|---|---|
| (default) | |
| actions |
CSS parts (::part)
| Part | Description |
|---|---|
| content | |
| actions |
Accessibility#
Screen reader#
role="treegrid" and each group role="rowgroup"; rows are row with aria-level and, for parents, aria-expanded. Cells are gridcell and headers columnheader with aria-sort.
label: the grip and the move announcements use it instead of value. Moving rows and columns is announced in a live region.
Keyboard support#
| Key | Function |
|---|---|
row-reorderable, moves the row before its previous sibling. | |
row-reorderable, moves the row after its next sibling. | |
nestable, makes the row the last sub-row of its previous sibling. | |
nestable, moves the row out of its parent, after it. |