gpt4 book ai didi

angular - 如何使用 Angular 6 在单个组件中渲染多个 ag-grid

转载 作者:行者123 更新时间:2023-12-04 15:36:00 24 4
gpt4 key购买 nike

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

html

 <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>

组件.ts
  @ViewChild('agGrid1', { static: true }) agGrid: AgGridAngular;
@ViewChild('agGrid2', { static: true }) agGrid2: AgGridAngular;

getRowsData() {

this.selectedRows1 = this.agGrid.api.getSelectedRows();
this.selectedRows2 = this.agGrid2.api.getSelectedRows();
console.log( this.selectedRows1, this.selectedRows2)

}

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

ERROR TypeError: Cannot read property 'api' of undefined error.

最佳答案

完成 Ag-Grid 设置

我已经尝试过下面的代码,绑定(bind)网格正确的方式

在您的 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
希望能帮到你

如果您对上述有任何问题,请告诉我

谢谢

关于angular - 如何使用 Angular 6 在单个组件中渲染多个 ag-grid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59720458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com