7

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

这是我的网格:

<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(){ ... }

}
4

3 回答 3

9

您需要设置的选项是selectable,有效值是true,并且false目前仅支持单行选择。所以你的网格应该是这样的

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

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

于 2016-12-01T09:13:11.110 回答
4

通过可选选项启用 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);
    }
}
于 2017-05-09T16:37:18.400 回答
1

只是对原始答案的补充,除了 plunker 的一个变化之外,这仍然是正确的

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

如果其他人来到这个页面,可以得到最新的信息。

于 2018-04-13T13:47:12.377 回答