Angular-Kendo treeview focus()方法

时间:2018-04-11 15:52:55

标签: kendo-ui-angular2 kendo-treeview

使用ElementRef尝试在此plunker中的树视图节点上focus()。我评论了底部的两条线因为它们不起作用。

以下是代码清单的插件:

https://plnkr.co/edit/aUnechu6glXk5CMFsMex?p=preview



import { Component, ElementRef, ViewChild } from '@angular/core';


 @Component({
     selector: 'my-app',
     template: `
     <kendo-treeview
         #treeview
         [nodes]="data"
         textField="text"
         kendoTreeViewCheckable
         kendoTreeViewExpandable
         kendoTreeViewSelectable

         kendoTreeViewHierarchyBinding
         childrenField="items">
     </kendo-treeview>
 `
 })
 export export class AppComponent {
   @ViewChild("treeview") treeview: ElementRef;
     public data: any[] = [
         {
             text: "Furniture", items: [
                 { text: "Tables & Chairs" },
                 { text: "Sofas" },
                 { text: "Occasional Furniture" }
             ]
         },
         {
             text: "Decor", items: [
                 { text: "Bed Linen" },
                 { text: "Curtains & Blinds" },
                 { text: "Carpets" }
             ]
         }
     ]; 
     //let tree = this.treeview.nativeElement;
     //this.treeview.focus('0');
 }
&#13;
&#13;
&#13;

根据他们的文档和示例focus()方法:

https://www.telerik.com/kendo-angular-ui/components/treeview/api/TreeViewComponent/#toc-focus

如何在我的组件代码中以编程方式聚焦(),而不是在其文档中发布的click()事件?

1 个答案:

答案 0 :(得分:1)

代码需要在Angular's lifecycle挂钩之一或方法中执行。我们需要一个ngAfterViewInit或一个自定义AppComponent.focus()方法ViewChild有时间实例化TreeView实例:

ngAfterViewInit(){
   //XXX: this.treeview is the TreeViewComponent instance set by ViewChild decorator 
   this.treeview.focus('1');
}