如何以编程方式聚焦到PrimeNG树中的节点?

时间:2017-07-05 10:38:07

标签: angular primeng

使用PrimeNG,我可以滚动到TreeNode:

在html中:

<p-tree #mytreeid id="mytree"></p-tree>

在Angular中:

@ViewChild("mytree") mytree: Tree;
// selection is the TreeNode you want to scroll into view
scrollToSelectionPrimeNgDataTree(selection, tree, elementIdName) {
      if (tree.value !== null) {
          let index = tree.value.indexOf(selection);
              let ele = document.getElementById(elementIdName).querySelectorAll("p-treenode")[index];
              ele.scrollIntoView();
              //ele.focus();
      }
  }

问题:如何使TreeNode(即&#39;选择&#39;)集中注意力?我试图调用focus()方法,但focus()不是Element的方法。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,我们只需要将[(选择)]设置为所选节点:

<p-tree #mytree id="mytree" [value]="componentGroups" selectionMode="single" [(selection)]="selectedNode">
相关问题