Inside my grid, I have this column:
{
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!
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.
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.
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.
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