【问题标题】:I need to reference kendo grid in angularjs controller written in typescript我需要在用 typescript 编写的 angularjs 控制器中引用剑道网格
【发布时间】:2016-07-19 15:49:27
【问题描述】:

在我的html模板中

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

在相关的组件类(实现ng.IComponentOptions)中我指定了

this.controllerAs = 'vm';

在控制器代码中

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
                }

    }

在 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
    }

这不起作用,因为“网格”未定义。这提出了两个问题:

  • 如何正确引用 myGrid?

  • 考虑到我可以同时访问 defaultGridOptions 和 defaultDataSource,是否有更好的方法来获取所选项目?

也许是一个微不足道的问题,但我是 angular/typescript/kendo 的新手,我已经花了一些不平凡的时间。谢谢

【问题讨论】:

    标签: javascript angularjs typescript kendo-ui controller


    【解决方案1】:

    好的,看来不错的解决方案是在控制器中添加一个 kendoGrid 属性:

    private myGrid: kendo.ui.Grid;
    

    稍后在处理程序中将其用作普通属性

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多