最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - My ag-grid doesnt show up any Data - Stack Overflow

programmeradmin0浏览0评论

Im fairly new to Angular/Typescript. Im kinda learning and developing at the same time. Im trying to build a grid with data loaded from a Json file and Im having some issues with showing my Data. I'd love if you guys could point to my error, cuz my code piles with no errors and Im kinda helpless at the moment.

Ill provide my code below. Thanks in advance.

my-grid-applicationponent.ts

@Component({
  selector: 'app-my-grid-application',
  templateUrl: './my-grid-applicationponent.html'
})
export class MyGridApplicationComponent {
  private gridOptions: GridOptions;
  things:Things[];

  getThings(){
    this.myGridApplicationService.getThings().subscribe( things => 
this.things = things)
  }

constructor( private myGridApplicationService: MyGridApplicationService) {
    this.gridOptions = <GridOptions>{};
    var gridOptions = {
        onGridReady: function() {
    this.gridOptions.api.setRowData(this.things);
        }
    }
    this.gridOptions.columnDefs = [
        {
            headerName: "ID",
            field: "id",
            width: 100
        },
        {
            headerName: "Value",
            field: "value",
            cellRendererFramework: RedComponentComponent,
            width: 100
        },

    ]; 
  } 
}

my-grid-application.service.ts

export class Things{

}

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

  getThings(){
    return this.http.get('src/assets/data.json')
        .map((response:Response)=> <Things[]>response.json().data)
  }
}

data.json

{
"data" :[
    {
        "id": "red",
        "value": "#f00"
    },
    {
        "id": "green",
        "value": "#0f0"
    }
]
}

my-grid-applicationponent.html

<div style="width: 200px;">
  <ag-grid-angular #agGrid style="width: 100%; height: 200px;" class="ag-
theme-fresh"
           [gridOptions]="gridOptions">

Im fairly new to Angular/Typescript. Im kinda learning and developing at the same time. Im trying to build a grid with data loaded from a Json file and Im having some issues with showing my Data. I'd love if you guys could point to my error, cuz my code piles with no errors and Im kinda helpless at the moment.

Ill provide my code below. Thanks in advance.

my-grid-application.ponent.ts

@Component({
  selector: 'app-my-grid-application',
  templateUrl: './my-grid-application.ponent.html'
})
export class MyGridApplicationComponent {
  private gridOptions: GridOptions;
  things:Things[];

  getThings(){
    this.myGridApplicationService.getThings().subscribe( things => 
this.things = things)
  }

constructor( private myGridApplicationService: MyGridApplicationService) {
    this.gridOptions = <GridOptions>{};
    var gridOptions = {
        onGridReady: function() {
    this.gridOptions.api.setRowData(this.things);
        }
    }
    this.gridOptions.columnDefs = [
        {
            headerName: "ID",
            field: "id",
            width: 100
        },
        {
            headerName: "Value",
            field: "value",
            cellRendererFramework: RedComponentComponent,
            width: 100
        },

    ]; 
  } 
}

my-grid-application.service.ts

export class Things{

}

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

  getThings(){
    return this.http.get('src/assets/data.json')
        .map((response:Response)=> <Things[]>response.json().data)
  }
}

data.json

{
"data" :[
    {
        "id": "red",
        "value": "#f00"
    },
    {
        "id": "green",
        "value": "#0f0"
    }
]
}

my-grid-application.ponent.html

<div style="width: 200px;">
  <ag-grid-angular #agGrid style="width: 100%; height: 200px;" class="ag-
theme-fresh"
           [gridOptions]="gridOptions">

Share Improve this question edited Feb 27, 2018 at 17:25 Vasco Oliveira asked Feb 27, 2018 at 15:22 Vasco OliveiraVasco Oliveira 531 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I'm not an Ag-Grid expert, but why are you re-declaring gridOptions with var gridOptions in the constructor. This is the obvious mistake and should be corrected:

this.gridOptions = {
    onGridReady: function() {
        this.gridOptions.api.setRowData(this.things);
    }
}

because that is the property you're accessing in the template.

Check this StackBlitz from my Github

//MyGridApplication
constructor( private myGridApplicationService: MyGridApplicationService) {
    myGridApplicationService.getThings()
        .subscribe( things => this.things = things);

    this.gridOptions = <GridOptions>{};
    this.gridOptions = {
        onGridReady: () => {
            this.gridOptions.api.setRowData(this.things);
        }
    };

    this.gridOptions.columnDefs = [
        {
            headerName: "ID",
            field: "id",
            width: 100
        },
        {
            headerName: "Value",
            field: "value",
            cellRendererFramework: RedComponentComponent,
            width: 100
        },

    ]; 
} 

When onGridReady() is fired, "this.things" is not populated yet. try this:

var gridOptions = {
    rowData: this.things
}
发布评论

评论列表(0)

  1. 暂无评论