0

我正在调用 web api 来获取行,然后在 ag-grid 中加载这些数据。但是网格在返回 json 数据之前被初始化,所以无法显示。这是我的代码。

myComponent.component.html

<ag-grid-ng2 style="width: 400px; height: 115px;"
             class="ag-fresh"
             [gridOptions] ="gridOptions"                 
             [columnDefs]="columnDefs"
             [rowData]="rowData">
</ag-grid-ng2>

myComponent.component.ts

import { Component, OnInit } from '@angular/core';
import  { AgGridModule, AgGridNg2 } from 'ag-grid-ng2/main';
import { GridOptions } from 'ag-grid/main';
import { AccessService } from './access/access.service';
import { Observable } from 'rxjs/Observable';
import { Http, Response } from '@angular/http';

@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{

//gridOptions: GridOptions;
gridOptions:any;
columnDefs: any[];
rowData: any[];

ngOnInit() {
     this.gridOptions = {
        enableSorting: true,
        rowData: this.rowData,
        columnDefs: this.columnDefs,
        OnInit: () =>{
            console.log('dfd');
            this.gridOptions.api.rowData = this.rowData;
        },
        onReady: () => {
            this.gridOptions.api.sizeColumnsToFit();                
        }
    }
 }

constructor(private accessService: AccessService) {        
    this.rowData = this.returnRows();

    this.gridOptions = <GridOptions>{};
    //this.gridOptions = {};
    this.columnDefs = [
        {headerName: "Role Id", field: "RoleId"},
        {headerName: "Role Name", field: "RoleName"}
    ];      

    console.log(this.rowData); 

 }

 returnRows() {    
 var rowData: any;
 // rowData = this.accessService.getRoles();
 this.accessService.getRoles().subscribe(
    (res: any)=>{
        console.log("res 1: " + res._body);
        rowData=res._body;
    }
  );        
   return rowData;
  }
 }

returnRows() 方法通过调用服务返回行,然后进行 http 调用。

访问服务.ts

import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';

@Injectable()
 export class AccessService {
  constructor(private http: Http){        
}

private getRolesUrl = "http://localhost:52800/api/Event/GetUserRoles/3579";

getRoles () {
return this.http.get(this.getRolesUrl)
                .map(res => {
                  // console.log(res.json()); 
                   const roles = res;
                   return roles;
                }
                )
                .catch(this.handleError);
  }

  private handleError (error: Response) {
    console.error("error: "+error);
    return Observable.throw(error.json().error || 'Server error');
  }

 }
4

1 回答 1

0

我删除了 ngOnInit() 中的任何内容。在构造函数中,我刚刚分配如下。

this.accessService.getRoles().subscribe(
                (res: any)=>{
                    this.rowData = res;                       
                    }
); 

因为已经在网格中分配了属性值,所以设置 this.rowData=response,设置行值。

这解决了我的问题..

于 2017-08-12T08:51:45.107 回答