javascript - How to create an array using values from columns in ag grid? - Stack Overflow

I have columns with the following values:export class AppComponent {private gridApi;private gridColu

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

Share Improve this question edited Jan 7, 2022 at 15:17 Jett asked Mar 11, 2019 at 14:07 JettJett 8802 gold badges17 silver badges36 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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 ponent! On your ponent.html

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742356267a4428497.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信