You need to enable JavaScript to run this app. Angular使用Kendo Grid时如何获取选中行完整对象而非仅ID
优惠活动
大模型
产品
解决方案
定价
更多

Angular使用Kendo Grid时如何获取选中行完整对象而非仅ID

Kendo Grid 获取选中行完整对象的实现方案

你当前只能拿到ID的本质原因是:Kendo Grid的选中状态是基于配置的唯一键(也就是你写的kendoGridSelectBy="id")做追踪的,selectedKeysChange事件返回的本来就是对应主键值的集合,不会直接携带整行的完整数据对象。

可以通过以下两种可落地的方式拿到选中行的完整对象:

方案一:从本地绑定的数据源匹配(最轻量)

因为你已经把全量网格数据存在gridData变量里了,拿到选中的ID集合后,直接从本地数据中过滤匹配的行对象即可,不需要改动原有模板代码。

修改editHandler方法的代码:

editHandler($event) {
  // $event 是选中行的id值数组
  // 从gridData中匹配所有id命中选中集合的完整行对象
  const selectedFullRows = this.gridData.filter(row => $event.includes(row.id));
  console.log(selectedFullRows); // 此处即可获取选中行的全部字段
}
  • 该方案适配本地全量绑定、前端分页、当前页数据绑定的场景,没有额外依赖。
  • 如果你的网格配置为单选模式,拿到的结果数组长度为1,直接取数组第一个元素即可拿到单个选中行的完整对象。

方案二:调用Grid实例官方API(更通用)

Kendo Grid本身提供了直接获取选中完整行对象的内置方法,不需要手动写匹配逻辑,哪怕后续调整了选中的唯一键配置,也不需要修改业务代码。

  1. 先在模板上给Grid加模板引用变量:
<kendo-grid 
  #grid // 新增这个引用标记
  [kendoGridBinding]="gridData"
  [selectable]="true"
  kendoGridSelectBy="id"
  [selectedKeys]="scriptID" 
  (selectedKeysChange)="editHandler($event)"
>
<!-- 原有列配置保持不变 -->
<kendo-grid-column field="product" title="PRODUCT"> </kendo-grid-column>
<kendo-grid-column field="page" title="PAGE"> </kendo-grid-column>
<kendo-grid-column field="script" title="SCRIPT"> </kendo-grid-column>
</kendo-grid>
  1. 在TypeScript代码中通过ViewChild拿到Grid组件实例,直接调用内置方法获取选中对象:
// 引入需要的依赖
import { Component, ViewChild, OnInit } from '@angular/core';
import { GridComponent } from '@progress/kendo-angular-grid';

// 组件类内部逻辑
@ViewChild('grid') gridInstance: GridComponent;

ngOnInit() {
  this.getAllScripts();
}

getAllScripts(){
  this.scriptService.getAllRepairScripts().subscribe( data => {
    this.gridData = data;
  })
}

editHandler($event){
  // 直接调用官方方法拿到所有选中行的完整数据对象
  const selectedFullRows = this.gridInstance.selectedDataItems();
  console.log(selectedFullRows);
}
  • 该方案不需要手动维护匹配逻辑,适配所有绑定模式(包括远程分页、虚拟滚动等),稳定性更高。

内容的提问来源于stack exchange,提问作者hiba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.24 09:15:05