hud*_*ert 6 ag-grid angular ag-grid-angular
我有一个带有 Ag-Grid 的网格并且工作得很好,但是我需要将事件单击放在具有某些特定条件(错误条件回调)的行中。因此,当第一次单击该行时正常工作,但第二次之后会获得上次值和当前值,因此打印当前值。改变顺序。
我的模板文件:
<!-- <button (click)="setDataValue()">rowNode.setDataValue</button> -->
<ag-grid-angular
#agGrid
style="width: 100%; height: 450px;"
class="ag-theme-material"
(gridReady)="onGridReady($event)"
(rowSelected)="onRowSelected($event)" // ----------The FUNCTION----------
[rowData]="rowData"
[gridOptions]="gridOptions"
[columnDefs]="columnDefs"
[defaultColDef]="defaultColDef"
[modules]="modules"
[pagination]="true"
[paginationPageSize]="paginationPageSize"
[pagination]="true"
[domLayout]="domLayout"
[rowHeight]="rowHeight"
[overlayLoadingTemplate]="overlayLoadingTemplate"
[overlayNoRowsTemplate]="overlayNoRowsTemplate"
[rowSelection]="rowSelection"
>
</ag-grid-angular>
<div #anchorErrorMessage class="container">
<div
[hidden]="!rowErrorMessage"
class="alert alert-danger"
role="alert"
>
<h4 class="alert-heading">Erro de inconsistência!</h4>
<hr />
<ul>
<li *ngFor="let error of rowErrorMessage">
{{ error }}
</li>
</ul>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我的 .ts 文件
onRowSelected(event) {
console.log(event.node.data.erros);
this.rowErrorMessage = '';
this.rowErrorMessage = event.node.data.erros;
setTimeout(() => {
this.anchorErrorMessage.nativeElement.scrollIntoView({ behavior: 'smooth' });
}, 20);
}
Run Code Online (Sandbox Code Playgroud)
我的日志:
所以每次我点击获取 2 个值。请帮助我。
你是对的,令人讨厌的是,agGrid两次调用“onRowSelected”事件,一次是针对被选中的行,一次是针对未被选中的行。
(第二个事件确实应该在名为“onRowUnselected”的事件下单独提供。)
此外,这两次,该event.type值都设置为“rowSelected”(叹气),因此您实际上需要注意该event.node.selected值。
因此,要运行一些代码,仅基于被选中的行,您将使用:
onRowSelected(event) {
if (!event.node.selected)
return;
let id = event.node.data.YourPrimaryKeyField;
console.log('Selected row: ' + id);
. . .
}
Run Code Online (Sandbox Code Playgroud)
为什么 agGrid 文档没有提到这个关键event.node.selected值?!
https://www.ag-grid.com/javascript-grid-events/#selection
我得到了它。我的问题是这个事件:
(rowSelection)="onRowSelected(event)"
Run Code Online (Sandbox Code Playgroud)
调用两个方法,选择和取消选择,所以我只是设置一个条件来在选择时执行我想要的任何操作。
https://www.ag-grid.com/javascript-grid-events/#reference-selection
| 归档时间: |
|
| 查看次数: |
2182 次 |
| 最近记录: |