[英]How to scroll to selected node in Angular Tree
我有一个带有 Angular 树组件的页面。 单击主仪表板上的特定元素时,树节点会“聚焦”。 我通过使用 this.tree.treeModel.setFocusedNode(node) 来做到这一点,但它也应该滚动到匹配的节点。 不幸的是,滚动没有发生。 是否有任何方法可以滚动到突出显示/聚焦的元素?
对此是否有任何帮助表示赞赏
它应该与类似的东西一起工作
scroll(el: HTMLElement) {
el.scrollIntoView({behavior: 'smooth'});
}
或使用外部依赖ngx-scroll-to
Oussama 建议的 scrollIntoView 实际上有效,让该元素调用 scrollIntoView 只是有点棘手。 一种方法是将一个类分配给选定的节点,然后使用 document.querySelector 找到它,例如
在树组件中:
set selected(value: YourObjectType) {
if (this._selected !== value) {
this._selected = value;
if (this._selected) {
//implement findallParents in your model, then expand tree up to the item to be selected
this._selected.findAllParents.forEach(m => this.treeControl.expand(m));
//wait till all nodes are expanded - there might be a better way to do this
setTimeout(() => {
const element = document.querySelector('.selected-node');
if (element) {
element.scrollIntoView({behavior: 'smooth'});
}
});
}
}
}
isSelected(node: YourObjectType) {
return this._selected === node;
}
在模板中:
<mat-tree [dataSource]="dataSource" [treeControl]="treeControl">
<mat-nested-tree-node *matTreeNodeDef="let node;">
<li [ngClass]="{'selected-node': isSelected(node)}">
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.