如何从API React表中创建动态标头

发布于 2025-01-23 15:22:57 字数 304 浏览 0 评论 0原文

如何根据API响应为我的React表创建标头?

例如,我不想为表手动定义列的数据,因此如何从对象的键中动态创建标题,

let testData = [

{
  "id": "1",

  "name": "test",
  "namespace": "test",
  "description": "test",
},
{
  "id": "2",
  "name": "test2",
  "namespace": "test2",
  "description": "test2",
} ]

请参见下面的答案

How can I create a header for my react table based on the API response?

for example the data I don't want to define columns manually for the table, so how can I create the headers dynamically from the keys of the object

let testData = [

{
  "id": "1",

  "name": "test",
  "namespace": "test",
  "description": "test",
},
{
  "id": "2",
  "name": "test2",
  "namespace": "test2",
  "description": "test2",
} ]

see answer below

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咽泪装欢 2025-01-30 15:22:57

通常,您想使表标头静态,因为您无法控制返回端点的内容。一种方法可能是:

const App = () => {
  let testData = [

    {
      "id": "1",
    
      "name": "test",
      "namespace": "test",
      "description": "test",
    },
    {
      "id": "2",
      "name": "test2",
      "namespace": "test2",
      "description": "test2",
    } ]

  let headers = Object.keys(testData[0]);
  return <table>
    <thead>
      <tr>
       {
         headers.map(header => {
          return <th>{header}</th>
        })
        }
      </tr>
    </thead>
    <tbody>
      { 
        testData.map(({id, name, namespace, description}) => {
          return <tr>
            <td>{id}</td>
            <td>{name}</td>
            <td>{namespace}</td>
            <td>{description}</td>
        </tr>
      })
      }
      
    </tbody>
  </table> 
}

您也可以应用相同的逻辑以避免手动插入身体。

Normally, you would like to make the table header static, because you can not control what's returning the endpoint. An approach to this might be this one:

const App = () => {
  let testData = [

    {
      "id": "1",
    
      "name": "test",
      "namespace": "test",
      "description": "test",
    },
    {
      "id": "2",
      "name": "test2",
      "namespace": "test2",
      "description": "test2",
    } ]

  let headers = Object.keys(testData[0]);
  return <table>
    <thead>
      <tr>
       {
         headers.map(header => {
          return <th>{header}</th>
        })
        }
      </tr>
    </thead>
    <tbody>
      { 
        testData.map(({id, name, namespace, description}) => {
          return <tr>
            <td>{id}</td>
            <td>{name}</td>
            <td>{namespace}</td>
            <td>{description}</td>
        </tr>
      })
      }
      
    </tbody>
  </table> 
}

You could also apply the same logic to avoid inserting the body manually.

土豪 2025-01-30 15:22:57

我能够为此

let testData = [

{
  "id": "1",

  "name": "test",
  "namespace": "test",
  "description": "test",
},
{
  "id": "2",
  "name": "test2",
  "namespace": "test2",
  "description": "test2",
} ]

//

let columns = Object.keys(testData[0]).map(key =>{

return {
//set header and accessor

Header: key,
accessor: key

}

})

提出

  <Table data={tableData} columns={columns} />

一个解决方案基于数据而不是手动定义标题。

I was able to come up with a solution for this

let testData = [

{
  "id": "1",

  "name": "test",
  "namespace": "test",
  "description": "test",
},
{
  "id": "2",
  "name": "test2",
  "namespace": "test2",
  "description": "test2",
} ]

//so define the columns

let columns = Object.keys(testData[0]).map(key =>{

return {
//set header and accessor

Header: key,
accessor: key

}

})

//then pass in data and columns variable to table component

  <Table data={tableData} columns={columns} />

this is how you create dynamic headers based on data instead of manually defining headers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文