Skip to content

Commit fee9eba

Browse files
committed
docs(tree): add guide to cdk-tree (#9040)
1 parent 21513ab commit fee9eba

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

src/cdk-experimental/tree/tree.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
The `<cdk-tree>` enables developers to build a customized tree experience for structured data. The
2+
`<cdk-tree>` provides a foundation to build other features such as filtering on top of tree.
3+
For a Material Design styled tree, see `<mat-tree>` which builds on top of the `<cdk-tree>`.
4+
5+
There are two types of trees: flat tree and nested Tree. The DOM structures are different for
6+
these two types of trees.
7+
8+
#### Flat tree
9+
10+
In a flat tree, the hierarchy is flattened; nodes are not rendered inside of each other, but instead
11+
are rendered as siblings in sequence. An instance of `TreeFlattener` is used to generate the flat
12+
list of items from hierarchical data. The "level" of each tree node is read through the `getLevel`
13+
method of the `TreeControl`; this level can be used to style the node such that it is indented to
14+
the appropriate level.
15+
16+
```
17+
<cdk-tree>
18+
<cdk-tree-node> parent node </cdk-tree-node>
19+
<cdk-tree-node> -- child node1 </cdk-tree-node>
20+
<cdk-tree-node> -- child node2 </cdk-tree-node>
21+
</cdk-tree>
22+
23+
```
24+
25+
Flat trees are generally easier to style and inspect. They are also more friendly to scrolling
26+
variations, such as infinite or virtual scrolling.
27+
28+
29+
#### Nested tree
30+
31+
In nested tree, children nodes are placed inside their parent node in DOM. The parent node contains
32+
a node outlet into which children are projected.
33+
34+
```
35+
<cdk-tree>
36+
<cdk-nested-tree-node>
37+
parent node
38+
<cdk-nested-tree-node> -- child node1 </cdk-tree-node>
39+
<cdk-nested-tree-node> -- child node2 </cdk-tree-node>
40+
</cdk-nested-tree-node>
41+
</cdk-tree>
42+
```
43+
44+
Nested trees are easier to work with when hierarchical relationships are visually represented in
45+
ways that would be difficult to accomplish with flat nodes.
46+
47+
### Using the CDK tree
48+
49+
#### Writing your tree template
50+
51+
The only thing you need to define is the tree node template. There are two types of tree nodes,
52+
`<cdk-tree-node>` for flat tree and `<cdk-tree-nested-node> for nested tree`. The tree node
53+
template defines the look of the tree node, expansion/collapsing control and the structure for
54+
nested children nodes.
55+
56+
A node definition is specified via any element with `cdkNodeDef`. This directive exports the node
57+
data to be used in any bindings in the node template.
58+
59+
```
60+
<cdk-tree-node *cdkNodeDef=“let node”>
61+
{{node.key}}: {{node.value}}
62+
</cdk-tree-node>
63+
```
64+
65+
##### Flat tree node template
66+
67+
Flat tree uses each node's `level` to render the hierarchy of the nodes.
68+
The "indent" for a given node is accomplished by adding spacing to each node based on its level.
69+
Spacing can be added either by applying the `cdkNodePadding` directive or by applying custom styles.
70+
71+
72+
##### Nested tree node template
73+
74+
When using nested tree nodes, the node template must contain a `cdkTreeNodeOutlet`, which marks
75+
where the children of the node will be rendered.
76+
77+
```
78+
<cdk-nested-tree-node *cdkNodeDef=“let node”>
79+
{{node.value}}
80+
<ng-container cdkTreeNodeOutlet></ng-container>
81+
</cdk-nested-tree-node>
82+
83+
```
84+
85+
#### Adding expand/collapse
86+
87+
A `cdkTreeNodeToggle` can be added in the tree node template to expand/collapse the tree node.
88+
The toggle toggles the expand/collapse functions in TreeControl and is able to expand/collapse
89+
a tree node recursively by setting `[cdkTreeNodeToggleRecursive]` to true.
90+
91+
```
92+
<cdk-tree-node *cdkNodeDef=“let node” cdkTreeNodeToggle [cdkTreeNodeToggleRecursive]="true">
93+
{{node.value}}
94+
</cdk-tree-node>
95+
```
96+
97+
The toggle can be placed anywhere in the tree node, and is only toggled by click action.
98+
For best accessibility, `cdkTreeNodeToggle` should be on a button element and have an appropriate
99+
`aria-label`.
100+
101+
```
102+
<cdk-tree-node *cdkNodeDef=“let node”>
103+
<button cdkTreeNodeToggle aria-label="toggle tree node" [cdkTreeNodeToggleRecursive]="true">
104+
<mat-icon>expand</mat-icon>
105+
</button>
106+
{{node.value}}
107+
</cdk-tree-node>
108+
```
109+
110+
#### Padding (Flat tree only)
111+
112+
The cdkTreeNodePadding can be placed in a flat tree's node template to display the level
113+
information of a flat tree node.
114+
115+
```
116+
<cdk-tree-node *cdkNodeDef=“let node” cdkNodePadding>
117+
{{node.value}}
118+
</cdk-tree-node>
119+
120+
```
121+
122+
Nested tree does not need this padding since padding can be easily added to the hierarchy structure
123+
in DOM.
124+
125+
126+
#### Conditional template
127+
The tree may include multiple node templates, where a template is chosen
128+
for a particular data node via the `when` predicate of the template.
129+
130+
131+
```
132+
<cdk-tree-node *cdkNodeDef=“let node” cdkTreeNodePadding>
133+
{{node.value}}
134+
</cdk-tree-node>
135+
<cdk-tree-node *cdkNodeDef=“let node; when: isSpecial” cdkTreeNodePadding>
136+
[ A special node {{node.value}} ]
137+
</cdk-tree-node>
138+
```
139+
140+
### Data Source
141+
142+
#### Connecting the tree to a data source
143+
144+
Similar to `cdk-table`, data is provided to the tree through a `DataSource`. When the tree receives
145+
a `DataSource` it will call its `connect()` method which returns an observable that emits an array
146+
of data. Whenever the data source emits data to this stream, the tree will render an update.
147+
148+
Because the data source provides this stream, it bears the responsibility of toggling tree
149+
updates. This can be based on anything: tree node expansion change, websocket connections, user
150+
interaction, model updates, time-based intervals, etc.
151+
152+
153+
#### Flat tree
154+
155+
The flat tree data source is responsible for the node expansion/collapsing events, since when
156+
the expansion status changes, the data nodes feed to the tree are changed. A new list of visible
157+
nodes should be sent to tree component based on current expansion status.
158+
159+
160+
#### Nested tree
161+
162+
The data source for nested tree has an option to leave the node expansion/collapsing event for each
163+
tree node component to handle.

0 commit comments

Comments
 (0)