Skip to content

Commit edb8764

Browse files
committed
docs(tree): add guide to cdk-tree
1 parent 8dbeefd commit edb8764

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

src/cdk/tree/tree.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
The `<cdk-tree>` enable developers to build customized tree for structured data. The cdk-tree
2+
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 list
12+
of items from hierarchical data. The "level" of each tree node is read through the getLevel method
13+
of the TreeControl; this level can be used to style the node such that it is indented to the
14+
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 has an
32+
outlet to keep all the children nodes.
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+
#### Wring 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`. It also export the node context,
57+
which can be used for event and property bindings on the node element.
58+
59+
##### Flat tree template
60+
61+
Flat tree template uses tree node’s `level` information to render the hierarchy of the nodes.
62+
Optionally, `cdkNodePadding` can be used in flat tree to add padding before the content of the
63+
node to show the hierarchy. No tree node will be put inside another flat tree node, therefore
64+
there's no way to handle nested children in a flat tree node.
65+
66+
67+
##### Nested tree template
68+
69+
Users of nested tree node should put `<ng-container cdkTreeNodeOutlet></ng-container>` to the nested
70+
tree node template. The children of this nested tree node will be rendered at this position.
71+
72+
```
73+
<cdk-nested-tree-node *cdkNodeDef=“let node”>
74+
{{node.value}}
75+
<ng-container cdkTreeNodeOutlet>
76+
</cdk-nested-tree-node>
77+
78+
```
79+
80+
#### Trigger
81+
82+
A `cdkTreeNodeTrigger` can be added in the tree node template to expand/collapse the tree node.
83+
The trigger triggers the expand/collapse functions in TreeControl and is able to expand/collapse
84+
a tree node recursively by setting `[cdkTreeNodeTriggerRecursive]` to true.
85+
86+
```
87+
<cdk-tree-node *cdkNodeDef=“let node” cdkTreeNodeTrigger [cdkTreeNodeTriggerRecursive]="true">
88+
{{node.value}}
89+
</cdk-tree-node>
90+
```
91+
92+
The trigger can be placed anywhere in the tree node, and is only triggered by click action.
93+
94+
```
95+
<cdk-tree-node *cdkNodeDef=“let node”>
96+
<span cdkTreeNodeTrigger [cdkTreeNodeTriggerRecursive]="true">
97+
<mat-icon>expand</mat-icon>
98+
</span>
99+
{{node.value}}
100+
</cdk-tree-node>
101+
```
102+
103+
#### Padding (Flat tree only)
104+
105+
The cdkTreeNodePadding can be placed in a flat tree's node template to display the level
106+
information of a flat tree node.
107+
108+
```
109+
<cdk-tree-node *cdkNodeDef=“let node” cdkNodePadding>
110+
{{node.value}}
111+
</cdk-tree-node>
112+
113+
```
114+
115+
Nested tree does not need this padding since padding can be easily added to the hierarchy structure
116+
in DOM.
117+
118+
119+
#### When
120+
Different tree node templates can be defined via the `when:` with a function.
121+
Please note that the tree will not update the template it uses if the data object does not change.
122+
So the `when` function should no be based on a dynamic variable, and instead it should be based on
123+
a fixed property of the data object.
124+
125+
```
126+
<cdk-tree-node *cdkNodeDef=“let node” cdkTreeNodePadding>
127+
{{node.value}}
128+
</cdk-tree-node>
129+
<cdk-tree-node *cdkNodeDef=“let node; when: isSpecial” cdkTreeNodePadding>
130+
[ A special node {{node.value}} ]
131+
</cdk-tree-node>
132+
```
133+
134+
### Data Source
135+
136+
#### Connecting the tree to a data source
137+
138+
Similar to `cdl-table`, data is provided to the tree through a `DataSource`. When the tree receives
139+
a `DataSource` it will call its `connect()` method which returns an observable that emits an array
140+
of data. Whenever the data source emits data to this stream, the tree will render an update.
141+
142+
Because the data source provides this stream, it bears the responsibility of triggering tree
143+
updates. This can be based on anything: tree node expansion change, websocket connections, user
144+
interaction, model updates, time-based intervals, etc.
145+
146+
147+
#### Flat tree
148+
149+
The flat tree data source should be responsible for the node expansion/collapsing events, since when
150+
the expansion status changes, the data nodes feed to the tree are changed. A new list of visible
151+
nodes should be sent to tree component based on current expansion status.
152+
153+
154+
#### Nested tree
155+
156+
The data source for nested tree has an option to leave the node expansion/collapsing event for each
157+
tree node component to handle.

0 commit comments

Comments
 (0)