Reputation: 67
I want to load ag-grid inside a button click. I have tried two approaches but none is working
My code : First Method
onBtnClick(){
this.gridOptions ={
onGridReady : function(){
console.log("print");
}
}
}
Second method:
onBtnClick(){
this.onGridReady();
}
onGridReady(){
this.gridApi = params.api;
console.log("print");
}
First method not working. Second method says api is not defined
Upvotes: 3
Views: 17051
Reputation: 878
You should share more of you code, it's unclear what you are doing.
You must make sure your gridOptions are fully set up BEFORE the grid initializes. (Doing this on a button is too late)
ngOnInit(): void {
this.gridOptions.onGridReady = gridReadyHandler;
}
gridReadyHandler = (params: GridReadyEvent) => {
this.gridApi = params.api;
console.log("grid is done loading", this.gridApi);
}
Upvotes: 0
Reputation: 6808
Have you checked if ag-grid
is connected to onGridReady
?
It should look something like that:
<ag-grid-angular
...
(gridReady)="onGridReady($event)"
>
</ag-grid-angular>
Upvotes: 0
Reputation: 11588
You can have this approach.
*ngIf
This way, the template having ag-grid
won't be rendered until the flag is set. When it's set, the template will be rendered and onGridReady
will be called.
<button (click)="btnClick()">Display grid</button>
<ag-grid-angular *ngIf="displayGrid"
#agGrid
......
></ag-grid-angular>
btnClick(){
this.displayGrid = true;
}
Check this working example: ag-grid display on button click
Upvotes: 3