Search code examples
angularasp.net-web-apirowsag-grid-ng2

ag-grid not loading data rows from web api output in angular2


I am calling web api to get rows and then load this data in ag-grid. But grid gets initialized before the json data is returned, so not able to show. Here is my code.

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() methode returns rows by calling service which then make http call.

access.service.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');
  }

 }

Solution

  • I removed whatever inside the ngOnInit(). In constructor I just assigned as follows.

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

    as there is already property value assigned in grid, setting this.rowData=response, sets the row values.

    This solved my issue..