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

时间:2019-03-11 14:07:59

标签: javascript arrays angularjs ag-grid

我是AG网格的新手,非常感谢您的帮助。

我正在从ag网格站点(https://next.plnkr.co/edit/pS6575GNn6MliLBD)看一个示例

我的列具有以下值:

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

}

如何将“价格”列中的值存储到数组中?

为澄清起见,我想有一个数组,其中包含“价格”中的值-像这样:

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

(完整的示例,包括预览,可以在我提供的插件链接中看到)

1 个答案:

答案 0 :(得分:1)

恐怕以目前的方式从AG-Grid获取所有数据非常不直观。如果要从农业电网获取数据,则必须执行类似的操作。不要忘记在组件中初始化gridApi! 在您的component.html

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

在您的component.ts上,

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

为了获得一系列价格,您可以在下一步执行以下操作:

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