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 - 如何修复 'TypeError: Cannot read property ' keycloak-token' of undefined' javascript 中的错误?

javascript - 单击索引中的链接后更改不同页面上的事件类 onClick?

javascript - 在表单中查找最大 tabindex

c++ - 为 std::map 定义一个使用值而不是键的比较函数

reactjs - 如何在 redux 的 DIspatch 中使用回调?

javascript - typescript 2 : Import statement generates TS2532: "Object is possibly ' undefined'.“

Ruby 数组数组和 map 方法

ruby-on-rails - 在 Rails 显示 View 中映射和链接模型实例

javascript - 在reactjs中使用.filter输出item的长度

reactjs - Kotlin JS 中的单元测试