简体   繁体   English

如何使用ag网格中列的值创建数组?

[英]How to create an array using values from columns in ag grid?

I am new to ag grid and would appreciate the help. 我是AG网格的新手,希望能提供帮助。

I am looking at an example from the ag grid site ( https://next.plnkr.co/edit/pS6575GNn6MliLBD ) 我正在从ag网格站点( https://next.plnkr.co/edit/pS6575GNn6MliLBD )查看示例

I have columns with the following values: 我有以下值的列:

export class AppComponent {
private gridApi;
private gridColumnApi;

private columnDefs;
private rowData;
private defaultColDef;
private getRowNodeId;

constructor() {
this.columnDefs = [
  {
    headerName: "Make",
    field: "make"
  },
  {
    headerName: "Model",
    field: "model"
  },
  {
    headerName: "Price",
    field: "price",
    filter: "agNumberColumnFilter"
  }
];
this.rowData = [
  {
    id: "aa",
    make: "Toyota",
    model: "Celica",
    price: 35000
  },
  {
    id: "bb",
    make: "Ford",
    model: "Mondeo",
    price: 32000
  },
  {
    id: "cc",
    make: "Porsche",
    model: "Boxter",
    price: 72000
  },
  {
    id: "dd",
    make: "BMW",
    model: "5 Series",
    price: 59000
  },
  {
    id: "ee",
    make: "Dodge",
    model: "Challanger",
    price: 35000
  },
  {
    id: "ff",
    make: "Mazda",
    model: "MX5",
    price: 28000
  },
  {
    id: "gg",
    make: "Horse",
    model: "Outside",
    price: 99000
  }
];
this.defaultColDef = {
  editable: true,
  sortable: true,
  filter: true
};
this.getRowNodeId = function(data) {
  return data.id;
};

} }

How do I store the values in the 'Price' column into an array? 如何将“价格”列中的值存储到数组中?

To clarify, I would like to have an array with the values from 'Price' - sort of like this: 为了澄清,我想有一个数组,其中包含“价格”中的值-像这样:

var exampleArray = [35000,32000,72000,59000,35000,28000,99000]

(The full example, including the preview can be seen at the plunker link I provided) (完整的示例,包括预览,可以在我提供的插件链接中看到)

I am afraid in the current way of getting all data from the AG-Grid is rather unintuitive. 恐怕以目前的方式从AG-Grid获取所有数据非常不直观。 You have to do something like this if you wanna fetch the data from your ag-grid. 如果要从农业电网获取数据,则必须执行类似的操作。 Do not forget to initialise your gridApi within your component! 不要忘记在组件中初始化gridApi! On your component.html 在您的component.html上

 <ag-grid-angular (gridReady)="onGridReady($event)" ... ></ag-grid-angular>

And on your component.ts, 在您的component.ts上,

onGridReady(params) {
  this.gridApi = params.api;
}
 .
 .
getRowData() {
  let rowData = [];
  this.gridApi.forEachNode(node => rowData.push(node.data));
  console.log(rowData)
}

In order to get an array of the prices, you can do this next: 为了获得一系列价格,您可以接下来执行以下操作:

const pricesArray = rowData.map(row => row['price']);
console.log(pricesArray);

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

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