简体   繁体   English

我需要在用打字稿编写的angularjs控制器中引用kendo网格

[英]I need to reference kendo grid in angularjs controller written in typescript

In the html template I have 在html模板中

<div kendo-grid="vm.myGrid" k-options="vm.defaultGridOptions" />

In the related component class (implements ng.IComponentOptions) I specify the 在相关的组件类(实现ng.IComponentOptions)中,我指定了

this.controllerAs = 'vm';

And in the controller code 并在控制器代码中

class MyController{
    ...
    static $inject = ['$log', '$scope']
    constructor(protected $log: ng.ILogService,
        protected $scope: ng.IScope){

        this.defaultDataSource = new kendo.data.DataSource(<kendo.data.DataSourceOptions>{ ...}

        this.defaultGridOptions = {
            dataSource: this.defaultDataSource,
            columns: [
                {
                    field: 'someField',
                    title: 'Some field',
                    width: 50
                },
                {
                    command: [{
                            template: '<span class="k-button" ng-click="vm.onSomeEvent($event)"> Click Here</span>'
                        }],
                    width: 60
                }

    }

and in the onSomeEvent method I want to retrieve the selected item, using something like this 在onSomeEvent方法中,我想使用类似这样的方法来检索选定的项目

    private onSomeEvent($event): void{
        var grid = $(this).data("kendoGrid");//well, I tried some other approaches too
        var selectedItem = grid.dataItem(grid.select());
        //use the selectedItem
    }

Which does not work, because the 'grid' is undefined. 这是行不通的,因为未定义“网格”。 This raises two questions: 这就提出了两个问题:

  • How to properly reference the myGrid? 如何正确引用myGrid?

  • Is there a better way to get the selected item, considering that I have access to both defaultGridOptions and defaultDataSource? 考虑到我可以同时访问defaultGridOptions和defaultDataSource,是否有更好的方法来获取所选项目?

Maybe a trivial question, but I am new to angular/typescript/kendo and I have spent already some non-trivial time on this. 也许这是一个琐碎的问题,但是我对angular / typescript / kendo不熟悉,因此我已经花了一些不平凡的时间。 Thanks 谢谢

Ok, it seems that the good solution is to add a kendoGrid property into the controller: 好的,看来不错的解决方案是在控制器中添加kendoGrid属性:

private myGrid: kendo.ui.Grid;

and later use it in the handler as a normal property 然后在处理程序中将其用作常规属性

private onSomeEvent($event): void{

    var selectedItem = this.myGrid.dataItem($($event.currentTarget).closest("tr"));
    //use the selectedItem
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM