问题
I am struggling to refresh ag-grid on subscription of an observable.
I have following piece of code which works well.
this.marketConfigs = this._regionProductConfigService.getMarketConfig();
this.gridOptions.columnDefs = this.createColumnDefs();
this.gridOptions.rowData = this.marketConfigs;
But since I am trying to put a drop-down in columns of ag-grid, I wanted the column config to be created once we receive data. So I changed the code to following :
this._refDataService.getAllCurrencies().subscribe(
(data: ICurrency[]) => {
this.financingCurrencies = data;
this.marketConfigs = this._regionProductConfigService.getMarketConfig();
this.gridOptions.columnDefs = this.createColumnDefs();
this.gridOptions.rowData = this.marketConfigs;
this.gridOptions.enableColResize = true;
this.gridOptions.api.refreshView();
},
err => console.log(err)
);
But it doesnt show any thing in grid. Can someone help?
回答1:
gridOptions.colDefs and gridOptions.rowData are "read once" properties - they're read on Grid initialization and not looked at again.
To do dynamic post-init setting of row or columns, you need to use the API.
Change
this.gridOptions.columnDefs = this.createColumnDefs();
this.gridOptions.rowData = this.marketConfigs;
To this:
this.gridOptions.api.setColumnDefs(this.createColumnDefs());
this.gridOptions.setRowData(this.marketConfigs);
And it should work as expected. Note if you use the API as per the above you won't need to call refreshView - the methods above will do it for you.
来源:https://stackoverflow.com/questions/42271497/refresh-ag-grid-on-observable-subscribe