我在AngularJs和TypeScript中使用Kendo Grid。我能够获得选定的行,但我不能将结果分配给局部变量,因为它似乎在不同的范围内运行。
我的网格有以下属性:
this.gridOptions = {
dataSource: {
data: this.items,
pageSize: 10
},
selectable: 'row',
filterable: {
mode: 'row'
},
change: this.onItemSelect,
...
}
typescript函数如下:onItemSelect(kendoEvent : any) {
var grid = kendoEvent.sender;
this.selectedItem = grid.dataItem(grid.select());
}
selectedItem在我的Controller类中定义:
export class ServicePackageModalController {
private selectedItem : any;
...
}
问题是这个。当我稍后在另一个按钮事件中检查selectedItem时,它是undefined。我认为这是因为当函数在Kendo Grid中被调用时,作用域是不同的,所以"this"意味着别的东西,而不是我的控制器类:
handleNext() {
console.debug(this.selectedItem) //this is undefined here?
}
所以我的问题是我如何将这个结果分配给我的控制器类,以便我以后可以访问它?
我能看到解决这个问题的唯一方法是将以下代码添加到我的handleNext函数:
var entityGrid = $("#EntitesGrid").data("kendoGrid");
var selectedItem = entityGrid.dataItem(entityGrid.select());
您还需要在kendo-grid指令中有一个名称为EntitiesGrid
的id
属性。