javascript - 如何在 React 中正确编写表格映射列表?

标签 javascript dictionary reactjs

我已经阅读并遵循了教程,但在盯着代码看了几个小时后,我似乎还没有完全理解它。有人能给我一个正确的解释,说明如何在 React 中使用 map 正确编写表格吗?

示例 map 数据:

[{"location_id":1,"location":"HKG","description":"Hong Kong","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":2,"location":"KUL","description":"Kuala Lumpur","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0}]

我希望它看起来像这样: enter image description here

希望你们能帮我举一个正确的例子,并对每个部分进行很好的解释,因为我目前仍然对文档感到困惑。谢谢。

最佳答案

输出表格的开头,然后映射内容的数组,然后输出结尾。

function MyComponent(props) {
  return (
    <table>
      <thead>
        <tr>
          <th>ID</th> <th>Location</th> <th>Description</th>
        </tr>
      </thead>
      <tbody>
        { 
          props.data.map(row => (
            <tr>
              <td>{row.location_id}</td>
              <td>{row.location}</td>
              <td>{row.description}</td>
            </tr>
          )) 
        }
      </tbody>
    </table>
  );
}

关于javascript - 如何在 React 中正确编写表格映射列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38341723/

相关文章:

javascript - 如何使用 Immutability Helper 更新多个状态 | react JS

javascript - 下载 pdf/word 按钮 javascript/jquery

javascript - 在 openlayers3 javascript 中单击 map 获取功能信息

javascript - DRY - 创建表的循环

python - 为什么 key 会是:value pair not be added to a Dictionary?

java - Map<String, String>,如何同时打印 "key string"和 "value string"

javascript - 选中时使元素 float 到 div 中心

python - 如何使用列表中的键和分隔空列表的值创建字典?

reactjs - React Native 回调类型仅允许从 native 代码进行单次调用

javascript - 在 React.js 中读取 .md