简体   繁体   English

Ag-Grid 动态设置列和行数据

[英]Ag-Grid Set Column and Row Data Dynamically

I have a TypeScript class where I call a method that calls a back-end api to get data from the server to put into a grid.我有一个 TypeScript class ,我在其中调用了一个方法,该方法调用后端 api 从服务器获取数据以放入网格。 At the moment I have hard-coded columns and rows just to test how I will do this.目前我有硬编码的列和行只是为了测试我将如何做到这一点。 When I put the column and row setting code as in the example below I get the grid correctly populated.当我按照下面的示例放置列和行设置代码时,我得到了正确填充的网格。

export class FooComponent {
  private columnDefs = [];
  private rowData = [];
  private gridOptions: GridOptions;
  private data: any;

  constructor(private http:HttpClient ) { 
    this.getFoo();

this.gridOptions = <GridOptions>{};
          this.gridOptions.columnDefs = [
            {headerName: 'Make', field: 'make' },
            {headerName: 'Model', field: 'model' },
            {headerName: 'Price', field: 'price'}
          ];
          this.gridOptions.rowData = [
            { make: 'Toyota', model: 'Celica', price: 35000 },
            { make: 'Ford', model: 'Mondeo', price: 32000 },
            { make: 'Porsche', model: 'Boxter', price: 72000 }
          ];
  }

  getFoo(){
    this.http.get(`https://xxx`).subscribe(response => {
      this.data = response;
    }, error => {
      console.log(error);
    });
  }

But when I put the column and row setting code inside the getFoo method, which is where I want it, my grid's data does not get set and I get a loading box.但是当我将列和行设置代码放在我想要的 getFoo 方法中时,我的网格数据没有设置,我得到一个加载框。

export class FooComponent {
  private columnDefs = [];
  private rowData = [];
  private gridOptions: GridOptions;
  private data: any;

  constructor(private http:HttpClient ) { 
    this.getFoo();
  }

  getFoo(){
    this.http.get(`https://xxx`).subscribe(response => {
      this.data = response;

      this.gridOptions = <GridOptions>{};
          this.gridOptions.columnDefs = [
            {headerName: 'Make', field: 'make' },
            {headerName: 'Model', field: 'model' },
            {headerName: 'Price', field: 'price'}
          ];
          this.gridOptions.rowData = [
            { make: 'Toyota', model: 'Celica', price: 35000 },
            { make: 'Ford', model: 'Mondeo', price: 32000 },
            { make: 'Porsche', model: 'Boxter', price: 72000 }
          ];
    }, error => {
      console.log(error);
    });
  }

I tried to resolve this by returning a promise from getFoo and then setting the column and row data inside the 'then' of the promise but got the same loading box.我试图通过从 getFoo 返回 promise 然后在 promise 的“then”内设置列和行数据来解决这个问题,但得到了相同的加载框。

Here is my markup.这是我的标记。

<ag-grid-angular 
    style="width: 700px; height: 500px;" 
    class="ag-theme-balham"
    [gridOptions]="gridOptions"
    >
</ag-grid-angular>

Any ideas?有任何想法吗?

You can do it like this:你可以这样做:

  export class FooComponent {
  private columnDefs = [];
  private rowData = [];
  private gridOptions$: Observable<GridOptions>;

  constructor(private http:HttpClient ) { 
 }
 ngOnInit() { this.getFoo(); } // call it here, not in constructor

 getFoo(){ 
   this.gridOptions$ = this.http.get(`https://xxx`);
 }

You can transform the http request by doing.map on it and do your business logic, and that response will be observable.您可以通过在其上执行 map 来转换 http 请求并执行您的业务逻辑,并且该响应将是可观察的。

After that just in html call it like this:之后在 html 中这样调用它:

<ag-grid-angular 
style="width: 700px; height: 500px;" 
class="ag-theme-balham"
[gridOptions]="gridOptions$ | async"
>

This will asynchronously update the model and trigger change detection for you.这将异步更新 model 并为您触发更改检测。

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

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