【问题标题】:Angular-Kendo treeview focus() methodAngular-Kendo treeview focus() 方法
【发布时间】:2018-04-11 15:52:55
【问题描述】:

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

这是下面代码清单的 plunker:

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');
 }

还有focus() 方法,根据他们的文档和此处的示例:

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

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

【问题讨论】:

    标签: kendo-ui-angular2 kendo-treeview


    【解决方案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');
    }
    

    【讨论】:

    • 如果我focus('0_1'),有没有办法扩展子元素?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    相关资源
    最近更新 更多