angular / angular/components

Use of custom TrackByFunction in MatTree does not preserve expansion state when nodes change

未关闭
#15,872 8 条评论 17 个 reaction 已指派 0 人 在 GitHub 查看
area: material/tree G P2
主要语言
TypeScript
星标
25k
派生
6.8k
平均合并
1 天 8 小时
30 天内合并 PR
91

描述

#### What is the expected behavior?

Use of trackBy should also ensure that the expansion state does not get lost when nodes are updated.

#### What is the current behavior?

Use of trackBy does not ensure that the expansion state does not get lost when nodes are updated. This is presumably since TreeControl uses an expansionModel which is a SelectionModel where the SelectionModel has no awareness of the TrackByFunction.

#### What are the steps to reproduce?

Relying upon the TreeControl expansionModel:
https://stackblitz.com/edit/angular-5wpr9o-kjnmsn?file=app%2Ftree-flat-overview-example.ts

#### Which versions of Angular, Material, OS, TypeScript, browsers are affected?

all

#### Is there anything else we should know?

Trying to store expansion state in the nodes themselves as a workaround doesn't work (unless you transfer the state):
https://stackblitz.com/edit/angular-5wpr9o-ahptjs?file=app%2Ftree-flat-overview-example.ts

Having a custom expansionModel that is aware of the trackBy state works to preserve state, since the TreeControl's expansion-related methods apparently aren't actually used internally by the tree component:
https://stackblitz.com/edit/angular-5wpr9o-pbmk1g?file=app%2Ftree-flat-overview-example.ts

贡献指南

打开贡献指南

调研方向

从链接的 StackBlitz 复现开始,尤其是 app/tree-flat-overview-example.ts,并检查 MatTree、TreeControl、TrackByFunction 和 expansionModel 如何交互。使用自定义 trackBy 更新节点时,复现展开状态丢失的问题。在这些更新后展开状态仍得以保留,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
angular, typescript
领域
frontend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。