默认定义columnDefs

时间:2019-01-24 18:19:00

标签: ag-grid

默认情况下,我想使用rowData中的所有数据,但我不想使用特定的值。您知道如何统一两个示例的columnDefs吗?

第一个示例:

columnDefs = [
    {headerName: 'make', field: 'make' },
    {headerName: 'model', field: 'model' },
    {headerName: 'price', field: 'price'}
];

rowData = [
    { make: 'Toyota', model: 'Celica', price: 35000 },
    { make: 'Ford', model: 'Mondeo', price: 32000 },
    { make: 'Porsche', model: 'Boxter', price: 72000 }
];

第二个示例:

columnDefs = [
    {headerName: 'name', field: 'name' },
    {headerName: 'phone', field: 'phone' },
];

rowData = [
    { name: 'Manuel', phone: 35000 },
    { name: 'Maria', phone: 32000 },
    { name: 'John', phone: 72000 }
];

1 个答案:

答案 0 :(得分:1)

我有一个主意,但不完全知道它是否更优雅。

var columnDefs = [
  { headerName: "make", field: "make" },
  { headerName: "model", field: "model" },
  { headerName: "price", field: "price" }
];

var rowData = [
  { make: "Toyota", model: "Celica", price: 35000 },
  { make: "Ford", model: "Mondeo", price: 32000 },
  { make: "Porsche", model: "Boxter", price: 72000 }
];

 genColums() {
  let columsDef = [];
  for (var prop in rowData[0]) {
    columsDef.push({
      headerName: prop,
      field: prop
    });
  }
  console.log(columsDef);
}

this.genColums();

PS。,如果所有对象中的所有数据都相同,则可以使用此功能