How to get the data of selected row in ag grid in angular2?
On template, e.g.:
(rowClicked)='onRowClicked($event)'(cellClicked)='onCellClicked($event)'(selectionChanged) = 'onSelectionChanged($event)'
and then on component class:
onRowClicked(event: any) { console.log('row', event); }onCellClicked(event: any) { console.log('cell', event); }onSelectionChanged(event: any) { console.log("selection", event); }
Use Chrome console to check the event object contents.
you can use api.getSelectedRows() that Returns a array of selected rows data.
public getSelectedRows(){ let rowsSelection = this.gridOptions.api.getSelectedRows(); console.info(rowsSelection); }
that's work for me.
On your HTML bind rowClicked
event to your own function as follows.
<ag-grid-angular #grid style="width: 100%; height: 500px;" class="ag-theme-balham" [rowData]="rowList" [columnDefs]="columnsList" [rowSelection]="selectionMode" (rowClicked)="onRowClick($event)" > </ag-grid-angular>
then on your TS or in your JS use the api as follows
onRowClick(event) { if (this.selectionMode === 'multiple') { this.selectedEntity = this.grid.api.getSelectedRows(); } else { this.selectedEntity = this.grid.api.getSelectedRows()[0]; }}
When your grid has a feature like multiple selections all the selected data won't pass with the event
parameter. It will always be the selected row only.
Reason I didn't encourage the selectionChanged
event was, It will always call the rowClicked
event before selectionChanged
event.