如何使用 ag 网格中列的值创建数组?
How to create an array using values from columns in ag grid?
我有具有以下值的列:
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;
};
}
如何将 'Price' 列中的值存储到数组中?
澄清一下,我想要一个包含 'Price' 值的数组 - 有点像这样:
var exampleArray = [35000,32000,72000,59000,35000,28000,99000]
(完整示例,包括预览可以在 plunker link - https://next.plnkr.co/edit/pS6575GNn6MliLBD 看到)
恐怕目前从 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);
我有具有以下值的列:
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;
};
}
如何将 'Price' 列中的值存储到数组中?
澄清一下,我想要一个包含 'Price' 值的数组 - 有点像这样:
var exampleArray = [35000,32000,72000,59000,35000,28000,99000]
(完整示例,包括预览可以在 plunker link - https://next.plnkr.co/edit/pS6575GNn6MliLBD 看到)
恐怕目前从 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);