0

I have a cell renderer which is a text box.

 <input type="text"[(ngModel)]="textVal" class="form-control mb-2 mr-sm-2 mb-sm-0"
id="measurename" (input)="onValChange()">

Whenever i write something in text box i update the data in other rows based on some logic

for (const node of this.selectedRowNodes) {
  if (this.parameterDefMap[this.parameterDefName]) {
    node.data.parameters[this.gridParams.colDef.field] = this.textVal;
  }
}

After that i try to refresh the cells/view to update the data in grid

const cell: GridCell = this.gridParams.api.getFocusedCell();
this.gridParams.api.refreshCells(params);


if (cell) {
  this.gridParams.api.setFocusedCell(cell.rowIndex, cell.column);
  const col = cell.column.getColDef().field;
}

The grid is getting refreshed successfully, but my issue is after refresh focus looses and when i am trying to set the focus to my cell which has a text field, the focus is not getting set.

James Z
  • 12,209
  • 10
  • 24
  • 44
Deepender Sharma
  • 460
  • 1
  • 5
  • 25
  • Find the cell and call focus on the relevant HTML element related to that cell. – Lansana Camara May 14 '18 at 15:56
  • Can you please give me some example, I have actually multiple textboxes it can be any at that point. – Deepender Sharma May 14 '18 at 17:42
  • In the place where you are looping through `this.selectedRowNodes`, is `selectedRowNodes` a list of HTML element nodes? If so, just do `node.focus()` or `node.nativeElement.focus()` or something like that. – Lansana Camara May 14 '18 at 18:17
  • Not sure buddy, but plunker would be great if you can support the answer with that. I am pretty much new with the ag grid – Deepender Sharma May 14 '18 at 18:33
  • I don't know anything about ag grid either. I am just looking at the code you gave above in an example. Read your code, then read my previous comment again and it should make sense. – Lansana Camara May 14 '18 at 18:38
  • Well that's fine. Thanks for your help. – Deepender Sharma May 14 '18 at 18:51
  • ...? Do you want help or not? The answer I gave you will solve your problem. Your question has very little to do with ag grid specifically, it is a general JavaScript/HTML question. If you have a list of elements (i.e. ag grid nodes), and you want to focus one of those, then you need to loop through them or directly select one of them based on a given value and then call focus on the respective input field. What part of this does not answer your question? – Lansana Camara May 14 '18 at 18:55
  • I don't know what's wrong here? I asked you for the plunker, because the solution you are giving me. I have already tried those before posting the question here. I thought you might be doing something additional which I am missing, but as you said you also not sure about AG grid, which is fine. Telling the solution is not the only way to help, because solutions which you have mentioned, may already been tried by the questioner. Secondly, in ideal situation this should solve with the grid itself. There is no need to go with html focus. – Deepender Sharma May 14 '18 at 19:03
  • Okay, good point. And when you tried them what error did you receive? If focus is lost, you must refocus. There is no other way to focus an element in JavaScript programmatically except to call `focus` on it again. So if you received an error with this solution we need to resolve that error. – Lansana Camara May 14 '18 at 19:05
  • Apparently, there was no error. If you see my code, in the last line setFocusedCell should focus but that also not working. – Deepender Sharma May 14 '18 at 19:08
  • Can you try one thing: call `this.cd.detectChanges()` after that line of code. Import `ChangeDetectorRef` using `private cd: ChangeDetectorRef` in your constructor. – Lansana Camara May 14 '18 at 19:09

0 Answers0