【问题标题】:react material ui populate table from API response从 API 响应中反应材料 ui 填充表
【发布时间】:2018-09-09 17:30:31
【问题描述】:

Material ui 表接受这种格式的数据。

 rows: [
      createData(1, "dashboard", "details"),
      createData(2, "product", "product details"),
    ].sort((a, b) => (a.id < b.id ? -1 : 1))

我存储在状态中的 API 响应就像

rows: Array(2)
 0: {id: 1, category_name: "dashboard", category_details: "details"}
 1: {id: 2, category_name: "product", category_details: "product details"}
 length: 2
__proto__: Array(0)

那么react中如何将json数组的response分配给material ui表呢?

【问题讨论】:

    标签: javascript reactjs material-ui


    【解决方案1】:

    执行以下操作

     const dataRows = [];
     this.state.rows.forEach((item, i) => {
        dataRows.push(createData(item.id, item.category_name, item.category_details));
     });
    

    console.log(“行”, dataRows);

    //上面的控制台日志会打印出来

      [
      createData(1, "dashboard", "details"),
      createData(2, "product", "product details"),
     ]
    

    只需将 dataRows 传递给 material-UI 表

    【讨论】:

      猜你喜欢
      • 2020-06-04
      • 1970-01-01
      • 2020-05-04
      • 2022-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      相关资源
      最近更新 更多