I have a grid with Ag-Grid and is working very well, but I needed put event click at a row with some specific condition(error condition callback). So when a click on the row the first time work as normal but the second time onwards get last time value and current value, so print the current value. Changing the order.
My template file:
<!-- <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>
My .ts file
onRowSelected(event) {
console.log(event.node.data.erros);
this.rowErrorMessage = '';
this.rowErrorMessage = event.node.data.erros;
setTimeout(() => {
this.anchorErrorMessage.nativeElement.scrollIntoView({ behavior: 'smooth' });
}, 20);
}
My log:
So every time I click get the 2 values. pls help me.
To programatically deselect a single row, use rowNode. setSelected(false) . rowNode. setSelected(isSelected, clearSelection) can be used to select rows as well, and will deselect all rows other than the subject rowNode if clearSelection is true .
Select a row by clicking on it. Selecting a row will remove any previous selection unless you hold down Ctrl while clicking. Selecting a row and holding down Shift while clicking a second row will select the range.
Property rowSelection='multiple' is set to enable multiple row selection. Selecting multiple rows can be achieved by holding down Ctrl and mouse clicking the rows. A range of rows can be selected by using Shift .
There is no option in the library to make a single row disabled(both visually and keyboard event based), the only way we could make a single row disabled is by using a customCellRenderer for both header and subsequent cell checkboxes, this allows full control over the checkbox.
rowSelection: Type of row selection, set to either 'single' or 'multiple' to enable selection. 'single' will use single row selection, such that when you select a row, any previously selected row gets unselected. 'multiple' allows multiple rows to be selected. rowMultiSelectWithClick: Set to true to allow multiple rows to be selected with clicks.
For example, if you click to select one row and then click to select another row, the first row will stay selected as well. Clicking a selected row in this mode will deselect the row. This is useful for touch devices where Ctrl and Shift clicking is not an option.
Property rowSelection='single' is set to enable single row selection. It is not possible to select multiple rows. The example below shows multi-row selection. Property rowSelection='multiple' is set to enable multiple row selection. Selecting multiple rows can be achieved by holding down Ctrl and mouse clicking the rows.
Property rowMultiSelectWithClick=true is set to enable multiple row selection with clicks. Clicking multiple rows will select multiple rows without needing to press Ctrl or Shift keys. Clicking a selected row will deselect that row. Checkbox selection can be used in two places: row selection and group selection.
You're right, annoyingly, agGrid calls the "onRowSelected" event twice, once for the row being selected, and once for the row being unselected.
(This second event really should've been provided separately, under an event called "onRowUnselected".)
Also, both times, the event.type
value is set as "rowSelected" (sigh) so you actually need to pay attention to the event.node.selected
value instead.
So, to run some code, just based on a row becoming selected, you would use:
onRowSelected(event) {
if (!event.node.selected)
return;
let id = event.node.data.YourPrimaryKeyField;
console.log('Selected row: ' + id);
. . .
}
Why doesn't the agGrid documentation mention this crucial event.node.selected
value ?!
https://www.ag-grid.com/javascript-grid-events/#selection
I got it. My problem was this event:
(rowSelection)="onRowSelected(event)"
calls two methods, selected and deselected, so I just put a condition to do whatever I want inside as selected.
https://www.ag-grid.com/javascript-grid-events/#reference-selection
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With