Nguyễn Hữu Hiếu Posted on Dec 21, 2021 • Edited on Dec 29, 2021 angular material tree: fix auto collapse tree each time data updated #angular #materialdesign #fix Problem Each time angular material tree refresh or update data source => tree auto collapse all row Solution Save tree expanded state before update data expandedNodeSet = new Set<any>(); private saveExpandedNode(): void { const mExpandedNode = this.treeControl?.dataNodes ?.filter((node) => { return this.treeControl.isExpanded(node); }) .map((node) => node.id) || []; this.expandedNodeSet = new Set(mExpandedNode); } private restoreExpandedNode(): void { this.treeControl.dataNodes.forEach((node) => { const id = node.id; const result = this.expandedNodeSet.has(id); if (result) { this.treeControl.expand(node); } }); } Enter fullscreen mode Exit fullscreen mode Top comments (0) Subscribe Personal Trusted User Create template Templates let you quickly answer FAQs or store snippets for re-use. Submit Preview Dismiss Code of Conduct • Report abuse Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)