【问题标题】:Ag-Grid Set Column and Row Data DynamicallyAg-Grid 动态设置列和行数据
【发布时间】:2020-02-20 17:07:59
【问题描述】:

我有一个 TypeScript 类,我在其中调用一个方法,该方法调用一个后端 api 从服务器获取数据以放入网格中。目前我有硬编码的列和行只是为了测试我将如何做到这一点。当我按照下面的示例放置列和行设置代码时,我得到了正确填充的网格。

export class FooComponent {
  private columnDefs = [];
  private rowData = [];
  private gridOptions: GridOptions;
  private data: any;

  constructor(private http:HttpClient ) { 
    this.getFoo();

this.gridOptions = <GridOptions>{};
          this.gridOptions.columnDefs = [
            {headerName: 'Make', field: 'make' },
            {headerName: 'Model', field: 'model' },
            {headerName: 'Price', field: 'price'}
          ];
          this.gridOptions.rowData = [
            { make: 'Toyota', model: 'Celica', price: 35000 },
            { make: 'Ford', model: 'Mondeo', price: 32000 },
            { make: 'Porsche', model: 'Boxter', price: 72000 }
          ];
  }

  getFoo(){
    this.http.get(`https://xxx`).subscribe(response => {
      this.data = response;
    }, error => {
      console.log(error);
    });
  }

但是,当我将列和行设置代码放入我想要的 getFoo 方法中时,我的网格数据没有被设置并且我得到一个加载框。

export class FooComponent {
  private columnDefs = [];
  private rowData = [];
  private gridOptions: GridOptions;
  private data: any;

  constructor(private http:HttpClient ) { 
    this.getFoo();
  }

  getFoo(){
    this.http.get(`https://xxx`).subscribe(response => {
      this.data = response;

      this.gridOptions = <GridOptions>{};
          this.gridOptions.columnDefs = [
            {headerName: 'Make', field: 'make' },
            {headerName: 'Model', field: 'model' },
            {headerName: 'Price', field: 'price'}
          ];
          this.gridOptions.rowData = [
            { make: 'Toyota', model: 'Celica', price: 35000 },
            { make: 'Ford', model: 'Mondeo', price: 32000 },
            { make: 'Porsche', model: 'Boxter', price: 72000 }
          ];
    }, error => {
      console.log(error);
    });
  }

我试图通过从 getFoo 返回一个 promise 来解决这个问题,然后在 promise 的“then”中设置列和行数据,但得到了相同的加载框。

这是我的标记。

<ag-grid-angular 
    style="width: 700px; height: 500px;" 
    class="ag-theme-balham"
    [gridOptions]="gridOptions"
    >
</ag-grid-angular>

有什么想法吗?

【问题讨论】:

    标签: javascript angular typescript ag-grid


    【解决方案1】:

    你可以这样做:

      export class FooComponent {
      private columnDefs = [];
      private rowData = [];
      private gridOptions$: Observable<GridOptions>;
    
      constructor(private http:HttpClient ) { 
     }
     ngOnInit() { this.getFoo(); } // call it here, not in constructor
    
     getFoo(){ 
       this.gridOptions$ = this.http.get(`https://xxx`);
     }
    

    您可以通过对其执行 .map 并执行您的业务逻辑来转换 http 请求,并且该响应将是可观察的。

    然后在 html 中这样调用它:

    <ag-grid-angular 
    style="width: 700px; height: 500px;" 
    class="ag-theme-balham"
    [gridOptions]="gridOptions$ | async"
    >
    

    这将为您异步更新模型并触发更改检测。

    【讨论】:

    • api调用返回我认为不能转成gridoptions的对象列表。
    • 事实上,我按照您的描述编写了代码,但出现此错误:“对象”类型可分配给极少数其他类型。您的意思是改用“任何”类型吗? “Object”类型缺少“Observable”类型中的以下属性:_isScalar、source、operator、lift 和另外 6 个。
    • 然后您可以在您的 ag-grid-angular 周围编写一个包装器组件以将您的数据传递给它,并且在输入中您将有 不是可观察的:D
    猜你喜欢
    • 2016-06-23
    • 2020-10-22
    • 2019-02-24
    • 2020-06-03
    • 2019-11-18
    • 2021-10-09
    • 2020-09-16
    • 2017-08-06
    • 2016-06-05
    相关资源
    最近更新 更多