Refresh ag-grid on Observable subscribe

ε祈祈猫儿з 提交于 2019-12-13 17:59:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!