【问题标题】:Rendering columns dynamically动态呈现列
【发布时间】:2021-11-11 00:05:00
【问题描述】:

我在我的 ReactJS 页面上从 Rest API 调用接收 JSON(具有产品/类别相关信息)。我在 ag-grid 上呈现这些信息。

类别水平显示,并且针对每个类别,产品显示在列中。

---------|-----  |----------|------------
         Product1| Product2 | Product3
---------|-------------------------------
Category1|abc1   | abc2     | abc3
-----------------------------------------
Category2|xyz1   | xyz2     | xyz3
-----------------------------------------

JSON 可以包含任意数量的类别/产品。 我需要能够根据此信息动态呈现行/列。 不知道该怎么做?

产品数量最多为 5 个。 我是否应该在我的 reactJS 页面中定义这五个产品的标题定义,然后根据 JSON 中存在的产品动态地将 hidden 属性设置为 true 或 false?

[{
    headerName: "Product1",
    resizable: true,
    wrapText: true,
    cellStyle: {
        'white-space': 'normal'
    },
    autoHeight: true,
    hide: true
},
{
    headerName: "Product2",
    resizable: true,
    wrapText: true,
    cellStyle: {
        'white-space': 'normal'
    },
    autoHeight: true,
    hide: false
}]

JSON: 示例1:

"raw_message":
[
{
"category": "category1"
[
{
    "product": "Product1"
},
{
    "product": "Product2"
},
]

}

]

示例2:

"raw_message":
[
{
"category": "category1"
[
{
    "product": "Product1"
},
{
    "product": "Product2"
},
{
    "product": "Product3"
},
]

}

]

【问题讨论】:

  • 你能分享你的json格式的产品和货物相关信息吗?
  • 嗨,Mahi,我添加了 json。谢谢。
  • 你确定 json 格式是正确的,这是你得到的数据吗?

标签: reactjs ag-grid


【解决方案1】:

感谢您的帮助。我设法通过使用 ag-grid 的 onRowDataChanged 事件处理程序和 columnAPi 的 SetColumnVisible 属性解决了这个问题。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2023-03-22
  • 1970-01-01
  • 2020-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多