Angular 6和Ag-grid

jos*_*des 1 ag-grid

我正在使用Angular 6和Ag-Grid进行测试。我已经做了一个示例,并将其绘制出来,我的意思是css等。

但是通过执行以下示例并从我的后端输入真实数据不会绘制表格,并且总是“加载”

// package.json

"dependencies": {
  "ag-grid-angular": "^19.0.0",
  "ag-grid-community": "^19.0.0",
Run Code Online (Sandbox Code Playgroud)

// HTML

<div class="container-fluid">
 Competencias
</div>
<div class="jumbotron text-center">
<ag-grid-angular #agGrid style="width: 100%; height: 200px;" class="ag-theme-balham" [gridOptions]="gridOptions">
 </ag-grid-angular>  
</div>
Run Code Online (Sandbox Code Playgroud)

// 零件

import { Component, OnInit } from '@angular/core';
import { environment } from '@env/environment';
import { CompetenceService } from '@app/services/competence.service';
import { GridOptions } from 'ag-grid-community';

@Component({
 selector: 'app-competence',
 templateUrl: './competence.component.html',
 styleUrls: ['./competence.component.scss'],
 providers: [CompetenceService],
})
export class CompetenceComponent implements OnInit {
version: string = environment.version;
title = 'app';
rowData: any;
columnDefs: any;
competences: any[];
gridOptions: GridOptions;

constructor(private competenceService: CompetenceService) { }

ngOnInit() {

this.gridOptions = <GridOptions>{};
this.gridOptions.columnDefs = new Array;
this.gridOptions.columnDefs = [
  {
    headerName: 'ID',
    field: 'id',
    width: 100
  },
  {
    headerName: 'Nombre',
    field: 'name',
    width: 200
  }];

this.competenceService.competences().subscribe(response => {
  this.competences = response;
  this.gridOptions.rowData = new Array;
  this.competences.forEach((competence) => {
    this.gridOptions.rowData.push({
      id: competence.id, name: competence.desc
    });
  });
  console.log(this.gridOptions);
});
}
}
Run Code Online (Sandbox Code Playgroud)

un.*_*ike 5

首先,您需要了解流程

rowData- 是不可变的 -您无法像使用array那样进行操作,只能重新创建它。更多信息

您需要避免使用gridOptions任何操作-仅用于init-configuration,用于其他任何操作-您需要使用gridApi-可以在onGridReady函数上访问的操作

(gridReady)="onGridReady($event)"
...
onGridReady(params) {
    this.gridApi = params.api;
    this.gridColumnApi = params.columnApi;
    let youData = [];
    this.competences.forEach((competence) => {
        youData.push({id: competence.id, name: competence.desc});
    });
    this.gridApi.setData(youData);
}
Run Code Online (Sandbox Code Playgroud)