在Kendo UI Grid中选择行时的触发事件(Angular 2)

时间:2016-11-30 16:46:36

标签: events angular kendo-ui kendo-grid kendo-ui-angular2

在Angular 2的Kendo UI(beta)中,如何在选择特定行时触发事件?行本身没有指令或组件;因此,a(click)=" triggeredFunction()"如果没有row元素,它就无法工作。

这是我的网格:

<kendo-grid [data]="gridData" [selectable]="true">

  <kendo-grid-column field="ProductName">
    <template kendoHeaderTemplate let-column let-columnIndex="columnIndex">
     {{column.field}}({{columnIndex}})
    </template>
  </kendo-grid-column>     

  <kendo-grid-column field="ProductName">
    <template kendoCellTemplate let-dataItem>
      <kendo-dropdownlist [data]="listItems"></kendo-dropdownlist>
    </template>
  </kendo-grid-column>

</kendo-grid>

这是我的组件:

@Component({
 selector: "ultron",
 styleUrls: [String("./ultron.component.less")],
 templateUrl: "./ultron.component.html",
 })
 export class UltronComponent {

   private gridData: any[] = [{
      "ProductID": 1,
      "ProductName": "Chai",
      "UnitPrice": 18.0000,
      "Discontinued": true,
    }, {
      "ProductID": 2,
      "ProductName": "Chang",
      "UnitPrice": 19.0000,
      "Discontinued": false,
    }
  }];

  private listItems: Array<string> = ["@", "$", "#", "%"];

  public triggeredFunction(){ ... }

}

3 个答案:

答案 0 :(得分:9)

您需要设置的选项为selectable,有效值为truefalse,因为目前仅支持单行选择。所以你的网格看起来应该是这样的

<kendo-grid
      [data]="gridView"
      [selectable]="true"
    >
  </kendo-grid>

对于该事件,您需要附加(selectionChange)事件处理程序。这是plunkr

答案 1 :(得分:3)

通过可选选项启用的Kendo UI中的选择。通过selectionChange事件提供所选行索引和选定状态。如果您还在网格中对数据进行排序或分页,那么您将被绑定到GridDataResult。要获取网格中所选行的绑定项,请使用GridDataResult的data属性。请参阅以下代码示例:

import { Component } from '@angular/core';
import { GridDataResult, SelectionEvent } from "@progress/kendo-angular-grid";
import { SortDescriptor, orderBy } from "@progress/kendo-data-query";


@Component({
    selector: 'my-app',
    template: `
            <kendo-grid [data]="gridDataResult" [selectable]="true" [height]="500" 
                [sortable]="true" (selectionChange)="selectedRowChange($event)" 
                [sort]="sort" (sortChange)="sortChange($event)">
                <kendo-grid-column field="ProductID" title="Product ID" [width]="300"></kendo-grid-column>
                <kendo-grid-column field="ProductName" title="Product Name"></kendo-grid-column>
                <kendo-grid-column field="UnitPrice" title="UnitPrice"></kendo-grid-column>
                <kendo-grid-column field="Discontinued" title="Discontinued"></kendo-grid-column>
            </kendo-grid>
            `
})

export class AppComponent {
    public sort: SortDescriptor[] = [{ dir: "asc", field: "ProductID" }];

    private gridDataResult: GridDataResult;

    public products: any[] = [{
        "ProductID": 1,
        "ProductName": "Chai",
        "UnitPrice": 18.0000,
        "Discontinued": true,
    }, {
        "ProductID": 2,
        "ProductName": "Chang",
        "UnitPrice": 19.0000,
        "Discontinued": false,
    }
    ];

    protected sortChange(sort: SortDescriptor[]): void {
       this.sort = sort;
       this.gridDataResult = {
            data: orderBy(this.products, this.sort),
            total: this.products.length
        };
    }

    public selectedRowChange(selectionEvent: SelectionEvent) {
        let selectedItem = this.gridDataResult.data[selectionEvent.index];
        console.log(selectedItem);
    }
}

答案 2 :(得分:1)

除了原始答案之外,除了一个改变之外仍然是真的

应该说 this.gridView.data [e.index]

如果其他人访问此页面,则可以获取最新信息。