我有一个带有Angular Tree组件的页面。单击主仪表板上的特定元素时,树节点将变得“集中”。我通过使用this.tree.treeModel.setFocusedNode(node)来做到这一点,但它也应该滚动到匹配的节点。不幸的是,滚动没有发生。有什么方法可以滚动到突出显示/突出显示的元素?
对此有任何帮助吗?
答案 0 :(得分:0)
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)}">
....
答案 1 :(得分:-1)