简体   繁体   English

使用带有嵌套对象的 object 的 Ag-grid

[英]Using Ag-grid with object of nested objects

I am trying to use ag-grid with an api that gives the following code我正在尝试将 ag-grid 与 api 一起使用,它提供以下代码

{
  "rates": {
    "btc": {
      "name": "Bitcoin",
      "unit": "BTC",
      "value": 1,
      "type": "crypto"
    },
(snip) 
}

And my ag-grid is set up in the following way我的 ag-grid 是按以下方式设置的

 <AgGridReact
            rowSelection="multiple"
                rowData={rowData}>
                <AgGridColumn field="btc"  filter={true} checkboxSelection={true} sortable={true}></AgGridColumn>
                <AgGridColumn field="eth" filter={true} checkboxSelection={true} sortable={true}></AgGridColumn>
                <AgGridColumn field="ltc" filter={true} checkboxSelection={true} sortable={true}></AgGridColumn>
            </AgGridReact>

So far that is giving me an error.到目前为止,这给了我一个错误。 I am not understanding why, because the code is working fine when I use a different api.我不明白为什么,因为当我使用不同的 api 时,代码运行良好。 The other api returns the following另一个 api 返回以下

{
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }

And my working grid is set up in the following way我的工作网格是按以下方式设置的

<AgGridReact
            rowSelection="multiple"
                rowData={rowData}>
                <AgGridColumn field="name"  filter={true} checkboxSelection={true} sortable={true}></AgGridColumn>
                <AgGridColumn field="phone" filter={true} checkboxSelection={true} sortable={true}></AgGridColumn>
                <AgGridColumn field="email" filter={true} checkboxSelection={true} sortable={true}></AgGridColumn>
            </AgGridReact>

I've noticed that the difference is the working api is an array of objects, while the one that is not working is an object with nested objects.我注意到不同之处在于工作的 api 是一个对象数组,而不工作的是一个带有嵌套对象的 object。
The full code is here for anyone to view完整的代码在这里供任何人查看

thanks in advance for your help.在此先感谢您的帮助。

I've put a demo of your code here with a few changes:我在 此处放置了您的代码演示,并进行了一些更改:

  • "btc", "eth", and "ltc" are objects themselves rather than simple fields, so having them as columns wouldn't make much sense. “btc”、“eth”和“ltc”是对象本身而不是简单的字段,因此将它们作为列没有多大意义。 Maybe you meant to iterate on these objects and view their "name", "unit", "value", and "type" instead.也许您打算迭代这些对象并查看它们的“名称”、“单位”、“值”和“类型”。 I've replaced the columns with the latter 4.我已经用后4个替换了这些列。

  • These objects are inside of an object with a single field: "rates".这些对象位于 object 内部,具有单个字段:“rates”。 Therefore, in your last .then() you'd have to因此,在最后一个.then()中,您必须

    let rates = rowData.rates

    But rates is still an object, not an array.但 rate 仍然是rates ,而不是数组。 So you'll also have to所以你还必须

    let newRowData = Object.values(rates)

    This newRowData variable is an array of objects consisting of the values of "btc", "eth", "ltc", etc., like so:这个newRowData变量是一个对象数组,由“btc”、“eth”、“ltc”等值组成,如下所示:

     [{ "name": "Bitcoin", "unit": "BTC", "value": 1.0, "type": "crypto" }, { "name": "Ether", "unit": "ETH", "value": 33.13, "type": "crypto" }, ... }]
  • Finally, I've put the console.log() inside the last .then() , since fetch() is asynchronous, and there is no guarantee that console.log(rowData) would print the newly fetched data on the console.最后,我将console.log()放在最后一个.then()中,因为fetch()是异步的,并且不能保证console.log(rowData)会在控制台上打印新获取的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM