简体   繁体   English

在ag-grid中显示来自服务器的数据

[英]Show data from the server in ag-grid

I use ag-grid in my Angular 4 project. 我在Angular 4项目中使用ag-grid

I bring the data from the server and then try to display it in a table, but the table remains empty (although the data exists). 我从服务器中提取数据,然后尝试将其显示在表中,但表保持为空(尽管数据存在)。

Just to be sure where the problem is, I have an *ngFor which generates the same data and I can see it displayed on the screen. 为了确定问题出在哪里,我有一个*ngFor ,它生成相同的数据,我可以看到它显示在屏幕上。

I also manage to display the data in the ag-grid if I write it manually (hard-coded) and it's parsed when the page is loaded, without any ajax request. 我还设法在ag-grid中显示数据,如果我手动编写(硬编码)并在页面加载时解析它,没有任何ajax请求。

html: HTML:

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


<table class="table">
  <tr>
    <th>
      Foo
    </th>
    <th>
      Bar
    </th>
  </tr>
  <tr *ngFor="let item of gridOptions.rowData; let myIndex = index">
    <td>
      {{item.foo}}
    </td>
    <td>
      {{item.bar}}
    </td>
  </tr>
</table>

ts: TS:

export class HomeComponent implements OnInit {
private gridOptions: GridOptions;

constructor(
  private adsService: AdsService
) {
  this.gridOptions = {};

  this.gridOptions.columnDefs = [
    {
      headerName: "Foo",
      field: "foo",
      width: 100
    },
    {
      headerName: "Bar",
      field: "bar",
      width: 100
    },
  ];

  this.gridOptions.rowData = [];
}

  ngOnInit(){
    this.myService
       .getAll()
       .subscribe(item => this.gridOptions.rowData = item);
  }
}

So I probably need to somehow let ag-grid know that the data has changed, I just don't know how to do it. 所以我可能需要以某种方式让ag-grid知道数据已经改变,我只是不知道该怎么做。

Working plunker: https://embed.plnkr.co/HJd1xI/ 工作人员: https ://embed.plnkr.co/HJd1xI/

Instead of passing the gridOptions to the template, I ended up with passing separated columnRefs and rowData as below. 我没有将gridOptions传递给模板,而是最终传递了分离的columnRefsrowData ,如下所示。

full-width-renderer.component.html 全宽renderer.component.html

<ag-grid-angular #agGrid style="width: 100%; height: 350px;" class="ag-fresh"
             [gridOptions]="gridOptions"
             [columnDefs]="columnDefs"
             [rowData]="rowData">
</ag-grid-angular>

And on the component, I assign the value to rowData directly. 在组件上,我直接将值赋给rowData。

full-width-renderer.component.ts 全宽renderer.component.ts

this.createRowData().subscribe((result) => {
    this.rowData = result;
});

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM