根据API响应来反应材料ui填充表

时间:2018-09-09 17:30:31

标签: javascript reactjs material-ui

材料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数组的响应分配给材料ui表呢?

1 个答案:

答案 0 :(得分:1)

执行以下操作

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

console.log(“ rows”,dataRows);

//以上控制台日志将打印

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

只需将dataRows传递到material-UI表