Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function rowSelected has been called twice(selected and deselected)

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:

enter image description here

So every time I click get the 2 values. pls help me.

like image 609
hudjoubert Avatar asked Nov 26 '19 14:11

hudjoubert


People also ask

How do I deselect all selected rows in Ag grid?

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 .

How do you get the selected row on Ag grid?

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.

How do I select multiple rows on Ag grid?

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 .

How do I turn off rows on Ag grid?

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.

What is the difference between single row selection and multiple row selection?

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.

How do I deselect a row in Excel?

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.

How to select multiple rows in a table?

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.

What is rowmultiselectwithclick in Salesforce?

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.


2 Answers

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

like image 154
Mike Gledhill Avatar answered Nov 09 '22 22:11

Mike Gledhill


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

like image 43
hudjoubert Avatar answered Nov 09 '22 22:11

hudjoubert