Jett
Jett

Reputation: 880

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

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 - https://next.plnkr.co/edit/pS6575GNn6MliLBD )

Upvotes: 3

Views: 6607

Answers (1)

wentjun
wentjun

Reputation: 42526

I am afraid in the current way of getting all data from the AG-Grid is rather unintuitive. 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! On your component.html

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

And on your 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);

Upvotes: 7

Related Questions