angular / angular/components

bug(cdk-tree): cdk tree with levelAccessor only renders all nodes and cant render based on the expanded state

オープン
#30,735 コメント 4 件 リアクション 5 件 担当者 0 名 GitHub で見る
area: cdk/tree P3
主要言語
TypeScript
スター
25k
フォーク
6.8k
平均マージ
1日 8時間
マージ済み PR(30日)
91

説明

### Is this a regression?

- [x] Yes, this behavior used to work in the previous version

### The previous version in which this bug was not present was

_No response_

### Description

The tree with `levelAccessor` and flat data can only render all nodes, and when I click on the `cdkTreeNodeToggle` component, it still only renders all nodes, and does not have the effect of expanding and collapsing nodes.

However, when I use tree with `childrenAccessor` and nested data, it will render specific nodes based on the tree expansion.

For example, for the follwing node structure:
```
- parent 1
- leaf 1-1
- leaf 1-2

- parent 2
- leaf 2-1
- leaf 2-2
```
**Demo 1** 👇

When I use `levelAccessor` with flat type node data passed in, it will always show the above structure, no matter if I click to collapse parent 1 or parent 2 or not, it will still be the same.
> link: https://stackblitz.com/edit/9hcrkwh5-mpxvoif7?file=src%2Fexample%2Fcdk-tree-flat-level-accessor-example.ts

**Demo 2** 👇

When I use `childrenAccessor` with nested node data passed in, it can dynamically render the nodes based on the expanded and collapsed state. For example, when I click to collapse parent 1, the structure of the tree view is re-rendered as:
```
- parent 1

- parent 2
- leaf 2-1
- leaf 2-2
```
> link: https://stackblitz.com/edit/qfnq49si-ubbjqylv?file=src%2Fexample%2Fcdk-tree-flat-children-accessor-example.ts

**Extra** 📝

Although I see that the [official demo](https://material.angular.io/cdk/tree/examples#cdk-tree-nested-level-accessor) hides nodes that don't need to be displayed by a combination of `style.display` and `shouldRender`.

I think this is still a problem. Because in Demo 1 you have to use `style.display` and `shouldRender` to render correctly, while in Demo 2 you can render correctly without them.

So I think there is a difference in this behaviour that is problematic.

The same problem exists for `TreeControl`, but we can pass only specific node data into the tree's `dataStream` based on the treeControl's record of the node's isExpanded status.

**Possible problem location** 🤔
https://github.com/angular/components/blob/538b8bcd6e14044cc3dce87409eeac40f27941cf/src/cdk/tree/tree.ts#L1093-L1103

The `flattenedNodes` and `renderNodes` are both equal to `nodes`,maybe we should create a method like `shouldRender` that determines which nodes should be rendered and assigns them to `renderNodes`?

### Reproduction

see **Desciption**

### Expected Behavior

Correctly render visible nodes in the tree when expanding or collapsing nodes

### Actual Behavior

When use `levelAccessor` with flat data, can not render visible nodes in the tree correctly when expanding or collapsing nodes. But use `childrenAccessor` with nested data, correctly.

### Environment

- Angular: 19.2.0
- CDK/Material: 19.2.0
- Browser(s): Chrome
- Operating System (e.g. Windows, macOS, Ubuntu): macOS

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

Start with src/cdk/tree/tree.ts at lines 1093–1103 and compare the flat levelAccessor path with the nested childrenAccessor behavior. Reproduce both StackBlitz demos, then verify that expanding or collapsing a node renders only the visible nodes while preserving the expected nested-tree behavior.

索引モデルが issue の本文から書いたものです。

評価

技術スタック
angular, typescript
領域
frontend
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。