I have two different grid configurations (represented as two gridOption
objects). One is loading local data, where the other one is using an elasticsearch datasource with the infinite row model.
If I wire them in the template [gridOptions]="localOptions"
, [gridOptions]="elasticGridOptions"
both work perfectly well.
This is my current component and (schematically) what I want to achieve:
@Component({
selector: 'app-gridtest',
styleUrls: ['./gridtest.component.css'],
template: `
<ag-grid-angular
id="grid"
style="width: 100vw;"
[style.height] = 'height'
class="ag-theme-balham ag-grid"
[columnDefs]="columnDefs"
[gridOptions]="gridOptions"
(gridReady)="onGridReady($event)"
></ag-grid-angular>
<button (click)="switchOptions()"></button>
`
})
export class GridtestComponent implements OnInit{
constructor(
private esDs: ElasticDatasource
) {}
height = '0px';
gridApi: any;
columnsApi: any;
gridOptions: {};
columnDefs = [
{headerName: 'Id', field: 'id'},
{headerName: 'Title', field: 'title'}
];
elasticGridOptions = {
rowModelType: 'infinite'
};
localOptions = {
rowData: [
{id: '1', title: 'foo'},
{id: '2', title: 'bar'},
{id: '3', title: 'baz'}
]
};
@HostListener('window:resize', ['$event'])
resizeGrid(){
this.height = `${$(window).innerHeight()-60}px`;
}
ngOnInit(): void {
this.elasticGridOptions['datasource'] = this.esDs;
}
onGridReady(params){
this.resizeGrid();
this.gridApi = params.api;
this.columnsApi = params.columnApi;
}
isElastic = false;
switchOptions(){
if(this.isElastic){
this.gridOptions = this.localOptions;
this.isElastic = false;
}else{
this.gridOptions = this.elasticGridOptions;
this.isElastic = true;
}
//reinitialize / update options or grid??
}
}
I wire [gridOptions]
to a generic options object and then on a button click I try to switch between the two. The interesting thin is, even if I put this.gridOptions = this.localOptions;
as a last line into onGridReady
it also doesn't work. I have the feeling I need to somehow tell the grid to actually reload the options, or to reinitialize itself, but I couldn't find it in the documentation.
Cheers
Currently,
gridOptions
can't be reloaded dynamically. Check here:Dynamically Setting Properties for ag-Grid
Repopulate ag-grid with data
You need to re-render your page. Anyway, you can handle requirement's on
resolver
part to prepare youroptions
and then use it incomponent
.GridOptions Javascript - Mutating gridOptions after the grid has been created will no longer be picked up by the grid. Instead use api.setGridOption (property, newValue) to update grid options.
Since v31 its possible to update
gridOptions
via new API methods:setGridOption
andupdateGridOptions