Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh an ag-grid when a change occurs inside a custom Cell Renderer component?

Inside my grid, I have this column:
enter image description here

  {
      headerName: "Generic/Specific",
      field: "genericspecific",
      id: "6",
      cellRenderer:'genericSpecificCellRenderersComponent',
      cellStyle: params => {
        const colors: string[] = ["red", "orange"];
        const genericSpecific: string[] = ["Generic", "Specific"];
        return {
          "background-color": colors[genericSpecific.indexOf(this.selectedOptions[params.node.id])]
        };
      }
    }

As you can see, it has a custom Cell Renderer. Here's its definition:

 @Component({
  selector: "app-generic-specific-renderers",
  template: `
    <hr />
    <select class="form-control" id='0' (change)="updateChosenValue($event.target); refresh(params)">
      <option >{{ selectedOptions[params.node.id] }}</option>
      <option >Specific</option>
      <option >Generic</option>
    </select>
    <hr />
  `
})
export class GenericSpecificCellRenderersComponent implements OnInit {
  updateChosenValue(event) {
    if (event.value==='Specific'){
      this.selectedOptions[this.params.node.id]='Specific'
    }
    else if (event.value==='Generic'){
      this.selectedOptions[this.params.node.id]='Generic'
     }
    }
  selectedOptions:string[]=[];
  constructor(private controlAssessmentData: ControlAssessmentService) {
    this.controlAssessmentData.currentSelectedOptions.subscribe(
      receivedSelectedOptions =>
        (this.selectedOptions = receivedSelectedOptions)
    );
  }
  ngOnInit() {}
  public params: any;
  public gridApi:any;

  // called on init
  agInit(params: any): void {
    this.params = params;
    this.gridApi= params.api;
  }
  // called when the cell is refreshed
  refresh(params: any): boolean {
    this.params = params;
    this.gridApi.refreshCells()
    console.log('this.params: ', this.params);
    return true;
  }
}

I wanted to refresh the grid everytime the user chooses an option from the dropDown list.
I thought my code should work, because I used the same logic when refreshing the grid where it was defined.
However, it didn't.
Any idea why? And maybe how I can solve it?
Thank you!

like image 559
Ahmed Ghrib Avatar asked May 28 '19 11:05

Ahmed Ghrib


People also ask

How do you refresh ag-Grid cell renderer?

refreshCells() to inform grid data has changed (see Refresh). Refresh Cells: api. refreshCells(cellRefreshParams) - Gets the grid to refresh all cells. Change detection will be used to refresh only cells who's display cell values are out of sync with the actual value.

How do you refresh Columndefs on ag-Grid?

To update existing column definitions, we first call the ag-Grid API method getColumnDefs() to get a reference to the grid's current columns. We then map over the columns, changing any desired properties before calling setColumns and updating our columns state variable.


1 Answers

When an option is changed (not in refesh() as you have now), run api.refreshCells with the option force set to true. api.refreshCells({ force: true });

If you can, also put the desired rows/columns in the call to avoid redrawing the entire grid.

like image 151
Spikolynn Avatar answered Oct 24 '22 19:10

Spikolynn