How to Init CellEditor Dynamically with the result

2020-05-10 14:45发布

问题:

I'm working on an app that uses Angular and Ag-Grid.

I have a column defined like following:

  columnDefs = [
        ...
        { 
          headerName: 'LANG', field:'lang', 
          autoHeight: true,cellClass: 'cell-wrap-text',sortable: false, filter: true, editable: true,
          cellEditor : 'agSelectCellEditor',
          cellEditorParams : ['English', 'Spanish', 'French', 'Portuguese', '(other)'];


        },
       ...

        ];

So everything works fine and in edit mode, I get the Combobox with the different options ('English', 'Spanish', 'French', 'Portuguese', '(other)'). My problem is that I need to get those options calling a REST WS.

So I have tried to define a variable in my Component (optionValues) and populating it in the "ngOnInit" method like this:

optionValues :  any;

columnDefs = [
        ...
        { 
          headerName: 'LANG', field:'lang', 
          autoHeight: true,cellClass: 'cell-wrap-text',sortable: false, filter: true, editable: true,
          cellEditor : 'agSelectCellEditor',
          cellEditorParams : this.optionValues,


        },
       ...

        ];

ngOnInit(){
  this.optionValues = this.http.get('http://localhost:8002/myservice');

}

But it didn't work, what is wrong here? Do I have to use a different approach?

Can you please help me?

回答1:

you can initialize grid settings inside http,

this.http.get(...).subscribe(v=>{
  this.gridOptions = {
    columnDefs: [...]
  };
  this.optionValues=v;
});