EJ2 Grid单元格选中事件无触发问题求助
解决EJ2 Grid单元格点击事件无法触发的问题
问题核心排查点及修复方案
错误使用列模板导致列未正确初始化
你当前用<ng-template #eGridColumns>包裹了列定义,但EJ2 Angular Grid不需要这种写法,该模板未被Grid组件识别,导致列未正确渲染,进而单元格事件无法绑定。直接移除ng-template,将<e-columns>直接放在<ejs-grid>内部即可:修改后的模板代码:
<ejs-grid #grid [dataSource]='dmtable' [toolbar]="toolbarItems" [editSettings]="editSettings" [selectionSettings]='selectionOptions' height='410px' (toolbarClick)="toolbarClickHandler($event)" (cellSelected)="onCellSelected($event)"> <e-columns> <e-column *ngFor="let column of getColumnNames(dmtable)" [field]="column" [headerText]="column"></e-column> </e-columns> </ejs-grid>确认事件参数及Grid引用的正确性
- 确保已正确导入所需类型:
import { Grid, CellSelectEventArgs, Column } from '@syncfusion/ej2-angular-grids'; - 在组件类中通过
ViewChild获取Grid实例:import { ViewChild } from '@angular/core'; export class YourComponent { @ViewChild('grid') public grid?: Grid; // ... 其余代码 }
- 确保已正确导入所需类型:
验证选择模式的配置
虽然你在ngOnInit中设置了selectionOptions,可尝试直接在模板中绑定对象,避免初始化时机问题:[selectionSettings]="{ type: 'Single', mode: 'Cell' }"同时确保
editSettings等其他配置未覆盖单元格选择行为,比如开启allowEditing但未正确配置编辑模式,可能会干扰选择事件触发。
额外调试建议
- 在
getColumnNames(dmtable)方法中添加console.log,确认返回的列数组正确,确保列被渲染到Grid中。 - 检查浏览器控制台是否有隐性报错(如模块未找到、类型导入错误等),这类错误会导致事件绑定失效。
内容的提问来源于stack exchange,提问作者mcr.netcore
相关产品推荐
相关产品推荐

