简体   繁体   中英

Trigger event when row is selected in a Kendo UI Grid (Angular 2)

In Kendo UI (beta) for Angular 2, how does one trigger an event when a specific row is selected? There are no directives or components for the rows themselves; therefore, a (click)="triggeredFunction()" can't work if there is no row element.

Here is my grid:

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

Here is my component:

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

}

The option that you need to set is selectable and the valid values are true and false as currently only single row selection is supported. So your grid should look like this

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

For the event you need to attach a (selectionChange) event handler. Here is a plunkr

Selection in a Kendo UI enabled through the selectable option. The selected row index and selected state are provided through the selectionChange event. If you are also sorting or paging data in the grid then you will be bound to a GridDataResult. To get the bound item of the selected row in the grid, use the data property of the GridDataResult. See the following code sample:

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);
    }
}

Just addition to original answer which is still true except one change in plunker

It should say this.gridView.data[e.index]

If someone else comes to this page, can get the latest information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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