如何使用Angular 6在单个组件中渲染多个Ag-Grid

时间:2020-01-13 16:27:43

标签: angular ag-grid-angular

我在html页面中有多个ag-grid,我想获取组件中ag-grid的特定行值。

html

DevicePolicyManager

component.ts

 <ag-grid-angular #agGrid1 style="width: 1250px; margin-left: 5px ; height:120px;background-color: #CFD8DC;"
    class="ag-theme-balham" [rowData]="rowsData1" [columnDefs]="cols" rowSelection="multiple"
    [enableCellChangeFlash]="true">
  </ag-grid-angular>

 <ag-grid-angular #agGrid2 style="width: 1250px; margin-left: 5px ; height:120px;background-color: #CFD8DC;"
    class="ag-theme-balham" [rowData]="rowsData2" [columnDefs]="cols2" rowSelection="multiple"
    [enableCellChangeFlash]="true">
  </ag-grid-angular>

在这里,我试图获取两个ag-grid行的值,但是我得到了:

错误TypeError:无法读取未定义错误的属性“ api”。

2 个答案:

答案 0 :(得分:1)

完成Ag-Grid设置

我尝试使用下面的代码,以正确的方式绑定网格

在您的HTML文件中

<ag-grid-angular style="width: 500px; height: 500px;" class="ag-theme-balham" [rowData]="rowData"
    [columnDefs]="columnDefs" [rowSelection]="rowSelection" (selectionChanged)="onSelectionChanged($event)"
          (gridReady)="onGridReady($event)"
>
</ag-grid-angular>

<ag-grid-angular style="width: 500px; height: 500px;" class="ag-theme-balham" [rowData]="rowData"
    [columnDefs]="columnDefs" [rowSelection]="rowSelection" (selectionChanged)="onSelectionChanged($event)"
          (gridReady)="onGridReady1($event)"
>
</ag-grid-angular>

和在app.component.ts文件中

private gridApi;
  private gridColumnApi;
  private gridApi1;
  private gridColumnApi1;
  rowSelection = "single";
  columnDefs = [
    { headerName: "Make", field: "make" },
    { headerName: "Model", field: "model" },
    { headerName: "Price", field: "price" }
  ];
  rowData = [
    { make: "Toyota", model: "Celica", price: 35000 },
    { make: "Ford", model: "Mondeo", price: 32000 },
    { make: "Porsche", model: "Boxter", price: 72000 }
  ];
  onSelectionChanged() {
    var selectedRows = this.gridApi.getSelectedRows();
    var selectedRows1 = this.gridApi1.getSelectedRows();
    console.log(selectedRows);
    console.log(selectedRows1);
  }
  onGridReady(params) {
    this.gridApi = params.api;
    this.gridColumnApi = params.columnApi;
  }
  onGridReady1(params) {
    this.gridApi1 = params.api;
    this.gridColumnApi1 = params.columnApi;
  }

我能够通过onSelectionChanged()方法获得网格的两个值

这是输出屏幕

enter image description here 希望这对您有帮助

让我知道您是否对以上问题

谢谢

答案 1 :(得分:0)

您的代码看起来不错。您刚刚输入了一个错误:将@ViewChild('agGrid'...替换为@ViewChild('agGrid1'...,因为这是您在HTML模板中输入的名称。