【问题标题】:How to Init CellEditor Dynamically with the result of a REST call如何使用 REST 调用的结果动态初始化 CellEditor
【发布时间】:2019-02-05 23:55:44
【问题描述】:

我正在开发一个使用 Angular 和 Ag-Grid 的应用程序。

我有一个如下定义的列:

  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)'];


        },
       ...

        ];

所以一切正常,在编辑模式下,我得到了带有不同选项的组合框('English'、'Spanish'、'French'、'Portuguese'、'(other)')。 我的问题是我需要获得调用 REST WS 的那些选项。

所以我尝试在我的组件(optionValues)中定义一个变量,并像这样在“ngOnInit”方法中填充它:

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');

}

但它不起作用,这里有什么问题?我必须使用其他方法吗?

你能帮帮我吗?

【问题讨论】:

  • this.http.get 不返回数组,需要订阅
  • 谢谢您的回复,可以举个例子吗?
  • this.http.get('http://localhost:8002/myservice').subscribe(v=>this.optionValues=v)
  • 没有任何变化,它看起来方法 ngOnInit() 没有效果,在控制台中我看到这个警报:“ag-Grid: no values found for select cellEditor”
  • 你可以为你的问题创建一个堆栈闪电战吗?

标签: angular ag-grid


【解决方案1】:

您可以在http中初始化网格设置,

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

【讨论】:

  • 我试过了,它对我有用。我不知道你是怎么做到的,所以如果你能创建一个stackblitz,我会看看
  • 顺便说一句,cellEditorParams: { values: [...]} 适用于当前的 ag-grid
  • 使用 arr.map(v=>v.description)
  • arr = [ { "id" : 0, "code" : "E", "description" : "English" }, { "id" : 1, "code" : "F", "description" : "Frenh" } ]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多