MatTree
Themat-tree provides a Material Design styled tree component for displaying hierarchical data with expandable and collapsible nodes.
Basic Usage
API Reference
MatTree
Selector:mat-tree
Extends: CdkTree
Inputs
| Name | Type | Description |
|---|---|---|
dataSource | DataSource<T> | Observable<T[]> | T[] | Data source for the tree |
treeControl | TreeControl<T> | Tree control for managing tree state |
MatTreeNode
Directive:mat-tree-node
Represents a node in the tree.
Inputs
| Name | Type | Default | Description |
|---|---|---|---|
disabled | boolean | false | Whether the node is disabled |
tabIndex | number | 0 | Tabindex of the node |
MatNestedTreeNode
Directive:mat-nested-tree-node
Represents a nested node in the tree with children.
Inputs
| Name | Type | Default | Description |
|---|---|---|---|
disabled | boolean | false | Whether the node is disabled |
tabIndex | number | 0 | Tabindex of the node |
MatTreeNodeDef
Directive:[matTreeNodeDef]
Defines the template for tree nodes:
MatTreeNodeToggle
Directive:matTreeNodeToggle
Toggle for expanding/collapsing tree nodes.
MatTreeNodeOutlet
Directive:matTreeNodeOutlet
Outlet for nested tree nodes.
MatTreeNodePadding
Directive:matTreeNodePadding
Adds indentation padding to tree nodes based on level.
Examples
Flat Tree
Checkable Tree
Dynamic Tree
Accessibility
Keyboard Navigation
UP_ARROW: Move focus to previous nodeDOWN_ARROW: Move focus to next nodeLEFT_ARROW: Collapse node or move to parentRIGHT_ARROW: Expand node or move to first childHOME: Move to first nodeEND: Move to last nodeENTERorSPACE: Activate/select node
ARIA
The tree hasrole="tree" and nodes have role="treeitem". Use appropriate labels:
Screen Readers
Provide clear labels for toggle buttons:Styling
Data Sources
Array Data Source
Observable Data Source
MatTreeFlatDataSource
Best Practices
- Clear hierarchy: Use consistent indentation
- Visual feedback: Show expand/collapse state clearly
- Performance: Use flat tree for large datasets
- Loading states: Show progress for dynamic loading
- Keyboard support: Ensure full keyboard navigation
- ARIA labels: Provide descriptive labels